Skip to content

Vue testing library depends on vuex and vue-router types #187

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
sapagat opened this issue Nov 28, 2020 · 12 comments · Fixed by #246
Closed

Vue testing library depends on vuex and vue-router types #187

sapagat opened this issue Nov 28, 2020 · 12 comments · Fixed by #246

Comments

@sapagat
Copy link

sapagat commented Nov 28, 2020

Hi there! For my surprise importing the @testing-library/vue module raises a compilation error due to a dependency with vue-router and vuex.

 ERROR  Failed to compile with 2 errors

These dependencies were not found:

* vue-router in ./node_modules/@testing-library/vue/dist/vue-testing-library.js
* vuex in ./node_modules/@testing-library/vue/dist/vue-testing-library.js

To install them, you can run: npm install --save vue-router vuex
  [=========================] 100% (completed)

 WEBPACK  Failed to compile with 2 error(s)

Error in ./node_modules/@testing-library/vue/dist/vue-testing-library.js

  Module not found: 'vue-router' in '/home/sam/sandbox/opencocos/app/node_modules/@testing-library/vue/dist'

Error in ./node_modules/@testing-library/vue/dist/vue-testing-library.js

  Module not found: 'vuex' in '/home/sam/sandbox/opencocos/app/node_modules/@testing-library/vue/dist'

I've tried installing both dependencies and test pass, but I believe it shouldn't be mandatory in order to run tests, right?

Notice: I'm not using Jest. I'm running tests with Mocha.

@afontcu
Copy link
Member

afontcu commented Nov 28, 2020

Hi! Yes it does, because VTL accepts a routes and store objects, which autoloads Router and Vuex: https://github.com/testing-library/vue-testing-library/blob/master/src/vue-testing-library.js#L36-L49

I once thought that they should be listed as peerDeps, but since they are only required if these keys are there… 

@afontcu afontcu closed this as completed Dec 3, 2020
@sapagat
Copy link
Author

sapagat commented Dec 3, 2020

I've double checked and no routes nor store are passed to the render method. It may be a webpack issue that detects the packages being required?

I've bypassed the issue by requiring both packages in order to use VTL. I believe it should be optional, and currently it isn't. Don't know how to solve it... =(

@afontcu
Copy link
Member

afontcu commented Dec 4, 2020

Hm, can you share the test that makes both deps fail?

@vfritzon
Copy link

vfritzon commented Mar 9, 2021

Hi!

I think I've run into this issue as well.

I get

ERROR in [...]/node_modules/@testing-library/vue/types/index.d.ts(5,35):

5:35 Cannot find module 'vuex' or its corresponding type declarations.

    3 | import Vue, {ComponentOptions} from 'vue'

    4 | import {ThisTypedMountOptions, VueClass} from '@vue/test-utils'

  > 5 | import {Store, StoreOptions} from 'vuex'

      |                                   ^

    6 | import Router, {RouteConfig} from 'vue-router'

    7 | // eslint-disable-next-line import/no-extraneous-dependencies

    8 | import {

in a ts project that does not have a dependency on vuex. It builds if I add vuex as dep.

@K-Schaeffer-LOG
Copy link

K-Schaeffer-LOG commented Jul 14, 2021

Having similtar issue with vue-router. The project doesnt have a dependency on it, still the following error happens on build:

6:35 Cannot find module 'vue-router' or its corresponding type declarations.
    4 | import {ThisTypedMountOptions, VueClass} from '@vue/test-utils'
    5 | import {Store, StoreOptions} from 'vuex'
  > 6 | import Router, {RouteConfig} from 'vue-router'
      |                                   ^
    7 | // eslint-disable-next-line import/no-extraneous-dependencies
    8 | import {
    9 |   queries,

@K-Schaeffer-LOG
Copy link

I just added vue-router as a development dependency and the build went fine.
This is a workaround, but It's not a good one.. We might need a fix to the root cause.

@afontcu
Copy link
Member

afontcu commented Jul 15, 2021

Looks like it is due to VTL types making use of TS definitions from both Router and Vuex. Not sure how to fix this one – maybe simply copy&pasting these types from both libraries into VTL, so that the dependency can go?

https://github.com/testing-library/vue-testing-library/blob/master/types/index.d.ts#L3-L6

@afontcu afontcu reopened this Jul 15, 2021
@afontcu
Copy link
Member

afontcu commented Jul 26, 2021

Took a look, and types we're getting from Vuex and Vue Router are quite complex and verbose. We'd be copy&pasting a huge chunk of Router/Vuex types… not entirely sure this is benefitial over installing two deps (that could be defined as devDep).

thoughts?

@K-Schaeffer
Copy link

I'm not sure If all types are needed, we might add a few to stop the error and that's It.

But I'm not sure, as far as I see VTL shouldn't rely on those types in any way.

@afontcu afontcu changed the title Vue testing library depends on vuex and vue-router Vue testing library depends on vuex and vue-router types Jul 29, 2021
@afontcu
Copy link
Member

afontcu commented Jul 30, 2021

I'm not sure If all types are needed, we might add a few to stop the error and that's It.

Wouldn't that generate several warnings on loose types if we change the complex ones to any, for instance? no-unsafe-assignment or no-unsafe-call being some of them.

as far as I see VTL shouldn't rely on those types in any way

Would you define store and routes as any (or anything similar) and call it a day?

@afontcu
Copy link
Member

afontcu commented Jul 30, 2021

Is there a best way? I really don't want to copy and maintain a ton of Router/Vuex types, and loosen them up doesn't feel right, either: #246

@KushibikiMashu
Copy link

KushibikiMashu commented Dec 8, 2021

I use @testing-library/vue@^5.8.2, but this error still occurs when I build storybook with @storybookjs/testing-vue.

ModuleNotFoundError: Module not found: Error: Can't resolve 'vuex' in '/Users/kushibiki/src/github.com/frontend-base-kit/node_modules/@testing-library/vue/dist'
ERR!     at /Users/kushibiki/src/github.com/frontend-base-kit/node_modules/@storybook/builder-webpack5/node_modules/webpack/lib/Compilation.js:1935:28

I installed vuex and vue-router as devDependencies and the error was fixed.

Although PR was already merged, I'm not sure why this still happens.

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 a pull request may close this issue.

6 participants