My Jest config `testRegex` does not work

I am writing the test code for my React.js project. I'm using react-scripts and enzyme (which I believe implies jest as well) to run and manage the tests. Here is the package.json file:

  "name": "front",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "~15.5.4",
    "react-dom": "~15.5.4",
    "react-scripts": "0.9.5",
    "redux": "~3.6.0",
    "react-redux": "~5.0.4"
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  "devDependencies": {
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-15": "^1.0.6",
    "react-test-renderer": "~15.5.4"
  "jest": {
    "testRegex": "(/abc/.*|(\\.|/)(defg|spec))\\.jsx?$"

After npm install, I checked the versions:

  • react-scripts@0.9.5
  • enzyme@3.3.0
  • jest@18.1.0

(Yeah, I know I'm using an older version of React.js but that's what I can't change right now because it is not my personal project.)

What I don't understand is the testRegex of jest doesn't work as I expect. Currently, I put all my test files under __tests__ folder and name them as "something.test.js". However, if you look at the testRegex above, firstly, it doesn't look for __tests__ but abc as the folder name and, secondly, it doesn't look for test but defg as the file name.

Because of the configuration above, I was expecting my test files wouldn't be executed at all. However, when I ran npm test, the test files were still executed.

What did I do wrong?

Some of my thoughts:

  • jest@18.1.0 may not support the testRegex.
  • I may need to configure jest via react-scripts but its user guide seems to show that I can just configure jest the way I'm doing (putting a "jest" section in package.json).
  • I also tried to use the latest jest v23.3, but it still didn't work as I expected.
  • I am aware of testMatch, but the documentation says "note that you cannot specify both options", so I am assuming that if I have specified testRegex in package.json, testMatch is ignored. Is this true?

1 answer

  • answered 2018-07-11 23:55 zakariya harti

    try this: create a separate file named jest.config.js

    module.exports = {
      coverageDirectory: "coverage",
      moduleFileExtensions: [
      testEnvironment: "node",
      testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",