Vue-cli: VueCLI forces Jest's testMatch option

Created on 3 Apr 2018  ·  8Comments  ·  Source: vuejs/vue-cli

Version

3.0.0-beta.6

Reproduction link

https://github.com/Ldoppea/vue-cli-jest-testmatch

Steps to reproduce

Init project with following options :

Vue CLI v3.0.0-beta.6
? Please pick a preset: Manually select features
? Check the features needed for your project: Unit
? Pick a unit testing solution: Jest
? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In package.json
? Save this as a preset for future projects? (Y/n) n

Move /tests/unit/HelloWorld.spec.js in /src/components/

Add testMatch in package.json Jest category :

    "testMatch": [
      "test/**/*.spec.js", 
      "src/**/*.spec.js"
    ]

Run npm run test

What is expected?

HelloWorld.spec.js should be tested

What is actually happening?

No test is found by Jest.
Get the following error :

No tests found
In D:\Work\test\vue\testJest
  6 files checked.
  testMatch: D:\Work\test\vue\testJest\(tests\unit\**\*.spec.(ts|tsx|js)|**\__tests__\*.(ts|tsx|js)) - 0 matches
  testPathIgnorePatterns: \\node_modules\\ - 6 matches
Pattern:  - 0 matches
 ERROR  jest exited with code 1.

testMatch still has the value forced by vueCLI


I was able to run my tests from src folder and without using testMatch on [email protected]. This commit changed the test detection behavior : https://github.com/vuejs/vue-cli/commit/2c61d236d77dfcb22b4560afe4c957ddf45b4337#diff-17aae08cfc13cba07661681220ddd985 (I do not use typescript)

bug

Most helpful comment

It appears that the plugin always sets the testMatch to [`--testMatch`, `<rootDir>/(tests/unit/**/*.spec.(ts|tsx|js)|**/__tests__/*.(ts|tsx|js))`] if none is specified via a command line argument, instead of respecting anything set in package.json. If we would like to respect those configurations, it appears to be an easy enough tweak I would gladly contribute

All 8 comments

It appears that the plugin always sets the testMatch to [`--testMatch`, `<rootDir>/(tests/unit/**/*.spec.(ts|tsx|js)|**/__tests__/*.(ts|tsx|js))`] if none is specified via a command line argument, instead of respecting anything set in package.json. If we would like to respect those configurations, it appears to be an easy enough tweak I would gladly contribute

Just want to add that besides package.json, we should also respect options set in jest.config.js 👌

What makes this slightly worse is that the forced option isn't even a superset of Jests own default. For example, when testing with other Vue applications I've always just suppled a test.js file in the same directory as the component it's testing (less directory hopping, encourages testing at same time as component design, file name is self explanatory, etc.)

For example, I tend to use something like:

src/components/my-component
├── index.js
├── my-component.vue
└── test.js

With Vue CLI, this doesn't work at all, which I'd consider a regression. Is there any technical reason to force override Jests own default?

I think it's easier to just remove that code from the cli runtime and move it to the config generated by the cli

That sounds like a sensible solution to me 😇

With my current PR (#1069) that is what I ended up doing. I moved the testMatch pattern over to the generator so that it goes into the package.json or jest.config.js (depending on how the user wants their config files laid out)

Much better solution @dhensche ! 🤘
An added benefit is that other tooling (e.g. the Jest extension for VSCode) works out of the box.

Thank you @dhensche 👍

Was this page helpful?
0 / 5 - 0 ratings