简体   繁体   English

为 create-react-app 项目添加 Jest 覆盖阈值

[英]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 :这是最初使用create-react-app生成的package.json文件的"scripts"部分:

"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 配置为具有这样的覆盖阈值

"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.但是,当我运行yarn test它看起来不像是在执行"jest"部分。 Is there something extra I need to add b/c this project was built with create-react-app ?我需要添加一些额外的东西,这个项目是用create-react-app构建的吗?

Thanks!谢谢!

package.json

  1. Add a new npm script to coverage将新的npm script添加到覆盖范围
"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 config
"jest": {
    "coverageThreshold": {
      "global": {
        "statements": 100,
        "branches": 50,
        "functions": 100,
        "lines": 100
      }
    }
  },
  1. npm run coverage

在此处输入图片说明

You can override certain coverage reporting metrics as indicated by create-react-app#coverage-reporting .可以覆盖由create-react-app#coverage-reporting指示的某些覆盖率报告指标。 Mind you, this has to be configured inside your package.json because that is where create-react-app looks for you overrides (aka, not inside .jestrc or jest.config.js files).请注意,这必须在您的package.json进行配置,因为这是 create-react-app 查找您覆盖的地方(也就是,不在.jestrcjest.config.js文件中)。 But as @Andreas mentioned, if you want full control, eject or create your own config.但正如@Andreas 提到的,如果你想完全控制,弹出或创建你自己的配置。

As a side notes, you may be able to pull out the jest configuration from create-react-app by using the --config flag with test script (from jest) and just copy that into your own config with your updates.作为旁注,您可以通过使用带有test脚本的--config标志(来自 jest)从 create-react-app 中提取 jest 配置,然后将其复制到您自己的配置中并进行更新。 May be easier than figuring out what create-react-app is doing.可能比弄清楚 create-react-app 正在做什么更容易。

Now, react-app-rewired could be a new path to take.现在, react-app-rewired可能是一条新的途径。

You can config Jest in package.json in a jest section mentioned here: https://github.com/timarney/react-app-rewired#2-jest-configuration---testing您可以在此处提到的 jest 部分中的package.json中配置 Jest: https : //github.com/timarney/react-app-rewired#2-jest-configuration---testing

The problem is that create-react-app uses its own settings here .问题是create-react-app 在这里使用自己的设置。 The easiest thing would be to eject: npm run eject , to have the full controll over the settings.最简单的方法是弹出: npm run eject ,以完全控制设置。

Another way would be to copy over all the settings to your own settings and start the test with the additional parameter --config=<pathToYourSettings>另一种方法是将所有设置复制到您自己的设置中,并使用附加参数--config=<pathToYourSettings>开始测试

You can refer to this latest link: Configuring threshold limit您可以参考这个最新链接:配置阈值限制

By adding this to your package.json:通过将其添加到您的 package.json 中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM