Search code examples
javascriptreactjsjestjspackage.jsoncreate-react-app

Add Jest coverage threshold to create-react-app project


This is the "scripts" section of the package.json file that was initially generated using create-react-app:

"scripts": {
  "start": "concurrently \"react-scripts start\" \"node server.js\"",
  "build": "react-scripts build",
  "eject": "react-scripts eject",
  "test": "react-scripts test --env=jsdom --coverage --watchAll",
  "start:server": "node server"
},

I would like to configure Jest to have a coverage threshold like this:

"jest": {
  "coverageThreshold": {
    "global": {
      "branches": 100,
      "functions": 100,
      "lines": 100,
      "statements": 100
    }
  }
}

However, when I run yarn test it does not look like the "jest" portion is being executed. Is there something extra I need to add b/c this project was built with create-react-app?

Thanks!


Solution

  • The problem is that create-react-app uses its own settings here. The easiest thing would be to eject: npm run eject, to have the full controll over the settings.

    Another way would be to copy over all the settings to your own settings and start the test with the additional parameter --config=<pathToYourSettings>