Jest: testMatch doesn't glob as expected

Created on 6 Oct 2018  路  11Comments  路  Source: facebook/jest

i'm setting up jest's testMatch config, but it doesn't glob as expected. my sanity-check command looks into the test folder relative to the project, goes to any depth, and selects any file that ends in .js:

ls ./test/**/*.js | wc -l

running that returns 92. which makes sense, i have 92 files in that directory structure (some tests and some test utils). but when i run jest with the same glob it says there are no tests found:

yarn run v1.9.4
$ jest --config jest.config.json
No tests found
In /Users/<user>/<repo>
  460 files checked.
  testMatch: ./test/**/*.js - 0 matches
  testPathIgnorePatterns: /node_modules/ - 460 matches
Pattern:  - 0 matches
error Command failed with exit code 1.

is this an issue with jest? with micromatch?

Bug Confirmed

Most helpful comment

Thanks!

We can look into why those globs don't work - just wanted to verify that it's the relative pathing

All 11 comments

Same issue here: --testMatch just doesn't work. Combined with #3793, this makes it impossible to even attempt testing .mjs files (let alone the plethora of issues with .mjs) using CLI arguments.

% node_modules/.bin/jest --testMatch '*.test.mjs'
No tests found
In /home/dandv/jesttest
  2 files checked.
  testMatch: *.test.mjs - 0 matches
  testPathIgnorePatterns: /node_modules/ - 2 matches
Pattern:  - 0 matches

% ls *.test.mjs
index.test.mjs

% node_modules/.bin/jest --version
23.6.0

Same behaviour here with src/**/*.test.ts. its a pretty simple glob to be honest..

Can you try this with **/test/**/*.js or **/src/**/*.test.ts?

My guess is that it's the relative pathing

This indeed works, but what if you don't want to match potentially deeper src children?

It is still unexpected behaviour that such a simple glob wouldn't work, the workaround isn't really equivalent unfortunately (over-matching).

Thanks!

We can look into why those globs don't work - just wanted to verify that it's the relative pathing

We're also seeing a similar issue. However, the globbing file expansion is inconsistent between different machines.

looking back, this bug would've been in jest 23.6.0. today, trying jest again, and the same bug shows up for v24.5.0. the bug seems largely the same, but jest is now at least showing which files are being picked up by which matchers. though it still is reporting that zero tests are found.

at first it looked like there was progress here, those tests were originally written for mocha, so jest is _technically_ correct, there are no jest tests in those files. i converted them over, and reran the command, but still ended up with files matching, but no tests found.

$ yarn run jest --testMatch ./src/components/search/*.test.js
yarn run v1.15.2
$ /Users/user/repos/repo/node_modules/.bin/jest --testMatch ./src/components/search/jest.test.js ./src/components/search/levenshtein-distance.test.js ./src/components/search/search.test.js
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/user/repos/repo
  1235 files checked.
  testMatch: ./src/components/search/jest.test.js, ./src/components/search/levenshtein-distance.test.js, ./src/components/search/search.test.js - 0 matches
  testPathIgnorePatterns: /node_modules/ - 1235 matches
  testRegex:  - 1235 matches
Pattern:  - 0 matches
error Command failed with exit code 1.

Jest is using an older version of micromatch; perhaps that could be part of the issue here. See also #8510

I too have problems with the testMatch globbing.

Changing the built in:

testMatch: [
  "**/__tests__/**/*.[jt]s?(x)",
  "**/?(*.)+(spec|test).[tj]s?(x)"
],

to

testMatch: [
  "src/**/__tests__/**/*.[jt]s?(x)",
  "src/**/?(*.)+(spec|test).[tj]s?(x)"
],

will cause no files to be matches. To resolve this i have to:

testMatch: [
  path.join(__dirname, 'src/**/__tests__/**/*.[jt]s?(x)'),
  path.join(__dirname, 'src/**/?(*.)+(spec|test).[tj]s?(x)'),
],

I also have to:

    "test:e2e": "jest --testMatch \"$(pwd)/tests/**/*\"",

instead of

    "test:e2e": "jest --testMatch \"tests/**/*\"",

in my package.json scripts

If Jest upgrades to the latest version of micromatch, it might fix this issue completely.

Was this page helpful?
0 / 5 - 0 ratings