Marco Lackovic
Marco Lackovic

Reputation: 6517

"No tests found" when running jest from command line

In a React project built with create-react-app where the test files resides in the same folder next to the code they need to test, like follows:

|- /src/path
|  |- List.tsx
|  |- List.test.tsx

when trying to run npx jest, or using the global jest, I get the following result:

No tests found
In C:\src\path
  34 files checked.
  testMatch: **\*test.tsx,**/*test.tsx,src\**\*.test.tsx,src/**/*.test.tsx,C:\src\path\src\**\*.test.tsx,C:\src\path\src\**\*.test.tsx - 0 matches
  testPathIgnorePatterns: \\node_modules\\ - 34 matches
Pattern:  - 0 matches

Running npm test - which in turns run the script react-scripts test from package.json - works fine and is able to find and run all the tests in the project (in watch mode).

Any idea how to solve this problem?

Environment

jest.config.js

module.exports = {
  testMatch: [
    '**\\*test.tsx',
    '**/*test.tsx',
    'src\\**\\*.test.tsx',
    'src/**/*.test.tsx',
    '<rootDir>\\src\\**\\*.test.tsx',
    '<rootDir>/src/**/*.test.tsx',
    'src/.*|(.|/)(.test).tsx?$'
  ],
};

Upvotes: 6

Views: 14375

Answers (2)

Marco Lackovic
Marco Lackovic

Reputation: 6517

According to this answer, projects built with create-react-app are not meant to be tested directly with jest.

react-scripts test should be used instead of jest to make use of the Jest configuration generated by the CRA setup.

Upvotes: 5

gombosg
gombosg

Reputation: 1033

Have you tried using / as a directory separator?

From Jest docs:

See the micromatch package for details of the patterns you can specify.

From Micromatch docs:

Micromatch exclusively and explicitly reserves backslashes for escaping characters in a glob pattern, even on windows. This is consistent with bash behavior.

...

In other words, since \\ is reserved as an escape character in globs, on windows path.join('foo', '*') would result in foo\\*, which tells micromatch to match * as a literal character. This is the same behavior as bash.

So I'd try:

module.exports = {
  testMatch: [ '**/*.test.tsx' ]
};

Upvotes: 3

Related Questions