6

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

  • node: 10.15.0
  • npm: 6.4.1
  • react-scripts: 2.1.3
  • Operating system: Windows 10 1809 17763.316

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?$'
  ],
};
Marco Lackovic
  • 6,077
  • 7
  • 55
  • 56

2 Answers2

5

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.

Marco Lackovic
  • 6,077
  • 7
  • 55
  • 56
3

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' ]
};
Community
  • 1
  • 1
gombosg
  • 893
  • 10
  • 11
  • Thanks! Yes, I have tried with `/` with no success. I have updated my questions with other patterns I have tried. – Marco Lackovic Mar 08 '19 at 09:08
  • From here I can't really help because I'm not on windows... is `jest.config.js` in the same folder as `src`? – gombosg Mar 08 '19 at 17:42
  • It is not in the same folder, it is one folder below it, in the root of the project. – Marco Lackovic Mar 09 '19 at 18:37
  • @MarcoLackovic have you tried running without setting the matches? The [defaults](https://jestjs.io/docs/en/configuration#testmatch-array-string) seem to be fine for you. – gombosg Mar 11 '19 at 01:56
  • same result with the default: `testMatch: **/__tests__/**/*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 matches` – Marco Lackovic Mar 11 '19 at 07:56
  • 1
    Then maybe raising a GitHub issue is the way to go if it doesn't work with the defaults. – gombosg Mar 11 '19 at 18:24