【问题标题】:Jest collectCoverageFrom and coverageThreshold not working开玩笑 collectCoverageFrom 和 coverageThreshold 不起作用
【发布时间】:2023-01-07 21:15:57
【问题描述】:

我有简单的创建反应应用程序。我创建了 jest.config.ts 文件,因为我的项目是打字稿。

import type { Config } from "@jest/types";

// Sync object
const config: Config.InitialOptions = {
    collectCoverageFrom: ["./src/modules/*.tsx", "./src/hooks/*.tsx", "./src/components/*.tsx"],
    coverageThreshold: {
        global: {
            statements: 100,
            branches: 100,
            functions: 100,
            lines: 100,
        },
    },
};
export default config;

但是当我运行 npm tnpm run test --watch 时,我的配置 coverageThresholdcollectCoverageFrom 不工作。我不知道出了什么问题。 以前我的配置文件有 js 扩展名,但我更改了它的类型,因为我认为这是它不起作用的原因。

我也试着写而不是这个collectCoverageFrom: ["./src/modules/*.tsx", "./src/hooks/*.tsx", "./src/components/*.tsx"],

这个collectCoverageFrom: ["**/src/modules/*.tsx", "**/src/hooks/*.tsx", "**/src/components/*.tsx"],

还有这个

collectCoverageFrom: ["**/src/**/*.tsx"],

但是jest.config 文件不起作用。

包.json

{
    "name": "ui",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "@emotion/react": "^11.5.0",
        "@testing-library/jest-dom": "^5.11.4",
        "@testing-library/react": "^11.1.0",
        "@types/node": "^12.0.0",
        "@types/react": "^17.0.34",
        "web-vitals": "^1.0.1"
        // other libs
    },
    "scripts": {
        "start": "react-scripts start",
        "proxy": "concurrently \"react-scripts start\" \"npm run mock\" ",
        "mock": "nodemon mock --watch mock",
        "build": "react-scripts build",
        "test": "react-scripts test --coverage",
        "eject": "react-scripts eject",
        "prettier": "prettier --ignore-path .gitignore --write \"**/*.+(js|json)\"",
        "lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx .",
        "format": "npm run prettier -- --write",
        "check-format": "npm run prettier -- --list-different",
        "validate": "npm-run-all --parallel lint check-format build"
    },
    "devDependencies": {
        "@testing-library/react-hooks": "^7.0.2",
        "@types/jest": "^26.0.24",
        "ts-jest": "^27.1.4",
        // any other libs
    }
}

覆盖文件夹中的 index.html 文件也显示这样的覆盖范围

60.6%报表20/33 40%分支机构4/10 46.66%职能7/15 58.62%线条17/29

这意味着当我运行npm t jest must show me error about global coverage does not 100%.但我在终端中没有看到任何错误。

【问题讨论】:

    标签: reactjs typescript jestjs


    【解决方案1】:

    修复了将 jest 配置添加到我的 package.json

      "devDependencies": {
          "@testing-library/react-hooks": "^7.0.2",
          "@types/jest": "^26.0.24",
          "ts-jest": "^27.1.4",
          // any other libs
      },
      "jest": {
        "collectCoverageFrom": [
          "src/**/*.tsx",
          "src/components/*.tsx",
          "!**/node_modules/**",
          "!**/src/test-utils/**",
          "!**/src/index.tsx"
        ],
        "coverageThreshold": {
            "global": {
                "statements": 100,
                "branches": 100,
                "functions": 100,
                "lines": 100
            }
        }
      }
    

    【讨论】:

      【解决方案2】:

      你删除了 jest.config 文件吗?并使用上述设置保留 package.json 文件?

      【讨论】:

        猜你喜欢
        • 2016-11-11
        • 2018-10-13
        • 2018-08-25
        • 1970-01-01
        • 2018-04-29
        • 2021-06-11
        • 2021-07-13
        • 2017-09-26
        • 1970-01-01
        相关资源
        最近更新 更多