[Jest] Track project code coverage with Jest

Jest comes pre-packaged with the ability to track code coverage for the modules you're testing, but it takes a little extra work to make it track untested files as well. Let's look at what Jest can do for you, what you have to do yourself, and how to setup code coverage thresholds so you can work to improving the code coverage numbers for your projects.

//package.json

{
  ...
  "jest": {
    "testEnvironment": "node",
    "collectCoverageFrom": [
      "src/*.js"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 100,
        "functions": 50,
        "lines": 33,
        "statements": 25
      }
    }
  }
}

"collectCoverageFrom":  include all the js files, it will exclude *.test.js / *.spec.js files automatically

"coverageThreshold": limit the coverage, if below the coverage, it will report error

原文地址:https://www.cnblogs.com/Answer1215/p/5851667.html