Jest: testMatch on Windows

Created on 16 Feb 2019  路  13Comments  路  Source: facebook/jest

馃悰 Bug Report

The following testMatch works on unix systems while does not on Windows machines. It is required to narrow the folders where to look for tests.

  testMatch: [
    '<rootDir>/src/**/?(*.)+(spec|test).(ts|tsx|mjs|js|jsx)',
  ],

Error on Windows:

  636 files checked.
  testMatch: C:/Users/IEUser/Fusion-UI/packages/infodesk/src/**\?(*.)+(spec|test).(ts|tsx|mjs|js|jsx) - 0 matches
  testPathIgnorePatterns: \\node_modules\\ - 636 matches
  testRegex:  - 636 matches
Pattern:  - 0 matches
error Command failed with exit code 1.

To Reproduce

Set a testMatch that starts with <rootDir>/src/... in place of the default **/*/....
See in the error message src/**\?(*.) here the / was replaced by \ and it is a lonely case.

Expected behavior

testMatch should work cross OS as in v23.

Link to repl or repo (highly encouraged)

this repro work in v22 while fails in v24 with Windows: https://repl.it/@artola/jest-bug

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS 10.14.3
    CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
  Binaries:
    Node: 11.9.0 - /usr/local/bin/node
    Yarn: 1.13.0 - /usr/local/bin/yarn
    npm: 6.5.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^24.1.0 => 24.1.0 
Bug

Most helpful comment

@SimenB @thymikee When I create a config using jest --init shows this block:

  // The glob patterns Jest uses to detect test files
  // testMatch: [
  //   "**/__tests__/**/*.[jt]s?(x)",
  //   "**/?(*.)+(spec|test).[tj]s?(x)"
  // ],

Then I just uncommented this block and tweaked a bit to support <rootDir>/src/.
This change does not play well in Windows while in Unix is ok as reported above.

Now what I did is slightly change the glob and works in both OS, following the doc in micromatch.

  // The glob patterns Jest uses to detect test files
  testMatch: [
     "<rootDir>/src/**/__tests__/**/*.[jt]s?(x)",
     "<rootDir>/src/**/*(*.)@(spec|test).[tj]s?(x)"
  ],

Here the more important changes:

  • ?(*.) => *(*.) ... from 0 or 1 occurrence to 0 or more occurrences
  • +(spec|test) => @(spec|test) ... from 1 or more occurrences to matches 1

Could be that "just" the default for testMatch shown in the documentation need some update?

All 13 comments

We moved to micromatch@3 and it seems like your glib is not valid anymore. You'll need to adjust it to be compliant. We're sorry this happens but it was announced as a breaking change

I wonder if this is #7814? Seeing as OP says it works on unix

@thymikee I know about the breaking change and we changed the glob, mainly I just use the default one shown in jest --init with the prefix <rootDir>/src/. While this works on Mac/Unix, does not on Windows.

Oh, that may be the case. Reopening then

I wonder if this is #7814? Seeing as OP says it works on unix

@SimenB I am using v24.1 but if I add the slash wrapper as shown here (https://github.com/facebook/jest/blob/d81c4cb5443e529a5c0572e5b9e4f231249c3609/packages/jest-config/src/utils.js#L70) the testMatch works on unix and also on Windows.

@SimenB @thymikee When I create a config using jest --init shows this block:

  // The glob patterns Jest uses to detect test files
  // testMatch: [
  //   "**/__tests__/**/*.[jt]s?(x)",
  //   "**/?(*.)+(spec|test).[tj]s?(x)"
  // ],

Then I just uncommented this block and tweaked a bit to support <rootDir>/src/.
This change does not play well in Windows while in Unix is ok as reported above.

Now what I did is slightly change the glob and works in both OS, following the doc in micromatch.

  // The glob patterns Jest uses to detect test files
  testMatch: [
     "<rootDir>/src/**/__tests__/**/*.[jt]s?(x)",
     "<rootDir>/src/**/*(*.)@(spec|test).[tj]s?(x)"
  ],

Here the more important changes:

  • ?(*.) => *(*.) ... from 0 or 1 occurrence to 0 or more occurrences
  • +(spec|test) => @(spec|test) ... from 1 or more occurrences to matches 1

Could be that "just" the default for testMatch shown in the documentation need some update?

@artola That solves my issues after upgrading multiple of my projects to latest Jest. Befeorehand I had to add a bunch of ignore entries to stop it from picking up files that Jest thought had tests but didn't actually, resulting in fake fails. One such example is the Create React App's test.js script which actually executes Jest.

Can we count on fix?

Not sure what a fix would be? Is it a documentation or implementation issue? A PR for either is very much welcome as none on the core team uses windows. We do test windows on CI though, so if it's a code issue we can add a test as well

@SimenB It is a code issue, see my comment above, a solution is wrap this return with slash.

https://github.com/facebook/jest/blob/16930281c466ef3ac7a07d9777d47b2d208ce16e/packages/jest-config/src/utils.ts#L66

It will be great if some Windows expert verifies and creates the PR.

In the meantime the solution was change the micromatch as explained.

Aight, fair enough! Looking forward to that PR 馃槉

yup, the focus is the path slash

In windows default slash like tests\rules\indent\indent.test.ts, but jest does not match this, must change like tests/rules/indent/indent.test.ts

Hello!
I'm not sure if it's the same bug,
I've found the testMatch glob like ...__tests__/+(unit|integration)... is internally converted to ...__tests__\+(unit|integration)... on Windows so it does not match. Other slashes in my pattern are not converted. It works on linux.
Workaround for me was ...__tests__+(/unit|/integration)....

Was this page helpful?
0 / 5 - 0 ratings