Skip to content

Fix: vue2.7 ShallowMount Composition API TS Error #2031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

Fix: vue2.7 ShallowMount Composition API TS Error #2031

wants to merge 3 commits into from

Conversation

mathiash98
Copy link

@mathiash98 mathiash98 commented Nov 14, 2022

What kind of change does this PR introduce? (check at least one)

  • Bugfix

Does this PR introduce a breaking change? (check one)

  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

Closes #1999
Fixes #1993
Fixes #2026

deraw and others added 3 commits August 9, 2022 16:06
replace createComponent with defineComponent and upgrade vue to vue 2.7.14.

Fixes TS typings when shallowMounting CompositionApi
@netlify
Copy link

netlify bot commented Nov 14, 2022

Deploy Preview for vue-test-utils-v1 failed.

Name Link
🔨 Latest commit a3d8847
🔍 Latest deploy log https://app.netlify.com/sites/vue-test-utils-v1/deploys/6372cabb8caec20008cfb360

@mathiash98
Copy link
Author

CircleCI works fine, seems like the netlify deploy script needs to update its node version from 10 to something more LTS

Copy link
Member

@haoqunjiang haoqunjiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait… This PR doesn't fix #2026

@mathiash98
Copy link
Author

Wait… This PR doesn't fix #2026

Hmm could you add the code for a test that fails and I'll add it to the branch to see if it works or not

@mathiash98
Copy link
Author

mathiash98 commented Nov 30, 2022

Not sure if this PR actually solves anything, anyhow I was able to run Vue 2.7 with jest 27 with following package.json:

{
"dependencies": {
    "@popperjs/core": "^2.11.6",
    "core-js": "^3.26.1",
    "lodash": "4.17.21",
    "v-tooltip": "2.1.3",
    "vue": "~2.7.14",
    "vue-router": "3.4.3",
    "vue2-leaflet": "2.7.1",
    "vuex": "3.5.1"
  },
  "devDependencies": {
    "@babel/core": "^7.20.5",
    "@types/jest": "^29.2.2",
    "@types/lodash": "4.14.188",
    "@typescript-eslint/eslint-plugin": "5.5.0",
    "@typescript-eslint/parser": "5.5.0",
    "@vue/cli-plugin-babel": "^5.0.8",
    "@vue/cli-plugin-eslint": "^5.0.8",
    "@vue/cli-plugin-pwa": "^5.0.8",
    "@vue/cli-plugin-router": "^5.0.8",
    "@vue/cli-plugin-typescript": "^5.0.8",
    "@vue/cli-plugin-unit-jest": "^5.0.8",
    "@vue/cli-plugin-vuex": "^5.0.8",
    "@vue/cli-service": "^5.0.8",
    "@vue/eslint-config-typescript": "7.0.0",
    "@vue/test-utils": "1.3.3",
    "@vue/vue2-jest": "^27.0.0",
    "babel-jest": "^27.5.1",
    "eslint": "7.32.0",
    "eslint-config-prettier": "8.3.0",
    "eslint-plugin-prettier": "4.0.0",
    "eslint-plugin-vue": "8.1.1",
    "flush-promises": "^1.0.2",
    "jest": "^27.1.5",
    "jest-junit": "15.0.0",
    "prettier": "2.1.2",
    "ts-jest": "^27.1.5",
    "typescript": "4.4.2",
    "vue-template-compiler": "^2.7.14"
  }
}

By setting vue-shims.d.ts to:

/**
 * Extends interfaces in Vue.js
 */

import Vue, { ComponentOptions } from "vue";
import { Store } from "./index";

declare module "vue/types/options" {
  interface ComponentOptions<V extends Vue> {
    store?: Store<any>;
  }
}

declare module "vue/types/vue" {
  interface Vue {
    $store: Store<any>;
  }
}

For some reason all the type errors come when I remove declare module "vue/types/vue" (...)...

@malykhinvi
Copy link

I managed to fix TS errors with the following type shim:

declare global {
  // With [email protected], DefineComponent is not considered a valid argument for mount and shallowMount
  declare module '@vue/test-utils' {
    import type Vue from 'vue'
    import type { Component } from 'vue'
    import type { ThisTypedShallowMountOptions, ThisTypedMountOptions } from '@vue/test-utils'

    export declare function shallowMount<C extends Component>(
      component: C,
      options?: ThisTypedShallowMountOptions<Vue>
    ): Wrapper<Vue>

    export declare function mount<C extends Component>(
      component: C,
      options?: ThisTypedMountOptions<Vue>
    ): Wrapper<Vue>
  }
}

@lmiller1990
Copy link
Member

Hi, sorry I did not look at this PR for a long time.

Do we still need this? It is just address typing issues?

TS is quite problematic in this library now we also need to support <script setup>.

@mathiash98
Copy link
Author

Hi, sorry I did not look at this PR for a long time.

Do we still need this? It is just address typing issues?

TS is quite problematic in this library now we also need to support <script setup>.

We can probably close it, as we have a workaround. At least for my case we will at some point migrate to Vue3 either way

@mathiash98 mathiash98 closed this Jan 18, 2023
@mathiash98 mathiash98 deleted the mathiash98/vue2.7-ts-lint-fix branch January 18, 2023 08:10
@lmiller1990
Copy link
Member

Vue 3 is great. I'm still working on cleaning up this repo and patching it, but at a lowered priority.

@ColinRosati
Copy link

ColinRosati commented Feb 27, 2023

I would say both shim solutions are not valid. Can we re-open this issue?

#2031 (comment)
Solution creates type issues importing components which are commonly declared via shim-vue.d.ts

#2031 (comment)
This doesnt work if you are using more then just mount, and shallowMount. This overwrites all other declerations.

Vue 2.7 is still being developed as the vue migration path is challenging especially with nuxt projects.

Extending shallowMount & mount with defineComponent is still needed!

@ColinRosati
Copy link

ColinRosati commented Feb 27, 2023

lmiller1990 RE: #2031 (comment)

Can you point out the <script setup> issue and which version TS makes incompatiblity?

@lmiller1990
Copy link
Member

Happy to accept a PR improving the types - I don't know the status of Vue 2.7 + script setup + TypeScript + this library - all my Vue 2 projects are written using JS.

What's the feature request exactly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type-checking fails with Vue 2.7.9+ mount and shallowMount do not type check with vue 2.7.x
6 participants