Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 237
SeanPlusPlus Avatar asked Dec 14 '22 23:12

SeanPlusPlus


1 Answers

package.json

  1. Add a new npm script to coverage
"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "coverage": "npm test -- --coverage",
    "eject": "react-scripts eject"
}
  1. And add a jest config
"jest": {
    "coverageThreshold": {
      "global": {
        "statements": 100,
        "branches": 50,
        "functions": 100,
        "lines": 100
      }
    }
  },
  1. npm run coverage

enter image description here

like image 150
narcello Avatar answered Jan 01 '23 12:01

narcello