开玩笑,如何忽略特定文件夹的测试覆盖率?

Leo*_*ban 7 javascript testing jestjs

在我的反应项目中,我有一个根./styles文件夹,其中包含样式组件的各种样式对象。我不希望这些文件出现在覆盖率测试中。

我试图像这样隐藏它们,但当我运行时npm run coverage它们仍然出现。

在此输入图像描述

包.json

"jest": {
  "setupFilesAfterEnv": [
    "<rootDir>/jest.setup.js"
  ],
  "coveragePathIgnorePatterns": [
    "<rootDir>/styles/",
    "./styles/"
  ],
  "testPathIgnorePatterns": [
    "<rootDir>/.next/",
    "<rootDir>/node_modules/",
    "<rootDir>/styles/",
    "./styles/"
  ],
  "transform": {
    "^.+\\.(js)$": "babel-jest",
    "^.+\\.js?$": "babel-jest",
    "^.+\\.ts?$": "babel-jest",
    "^.+\\.tsx?$": "babel-jest",
    "^.+\\.json5$": "json5-jest"
  },
  "moduleFileExtensions": [
    "js",
    "json",
    "json5",
    "ts",
    "tsx"
  ],
  "modulePaths": [
    "<rootDir>/components/",
    "<rootDir>/pages/",
    "<rootDir>/shared/"
  ]
}
Run Code Online (Sandbox Code Playgroud)

巴贝尔库

{
  "env": {
    "development": {
      "presets": [
        "next/babel",
        "@zeit/next-typescript/babel"
      ],
      "plugins": [
        ["styled-components", {"ssr": true, "displayName": true}],
        ["@babel/plugin-proposal-decorators", {"legacy": true}],
        ["istanbul",{"exclude": ["styles/*.js"]}]
      ]
    },
    "production": {
      "presets": [
        "next/babel",
        "@zeit/next-typescript/babel"
      ],
      "plugins": [
        ["styled-components", {"ssr": true, "displayName": true}],
        ["@babel/plugin-proposal-decorators", {"legacy": true}]
      ]
    },
    "test": {
      "presets": [
        "@babel/preset-typescript",
        ["next/babel", {"preset-env": { "modules": "commonjs" }}]
      ],
      "plugins": [
        ["styled-components", { "ssr": true, "displayName": true }],
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        ["babel-plugin-sass-vars"]
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Leo*_*ban 11

我所需要的就是 package.json 中的这个

"coveragePathIgnorePatterns": [
  "<rootDir>/styles/"
],
Run Code Online (Sandbox Code Playgroud)

并将其从"testPathIgnorePatterns":<- 在这里和 in 中删除coveragePathIgnorePatterns导致我的测试永远运行并且样式仍然出现。

我也从以下内容中删除了它.babelrc

["istanbul",{"exclude": ["styles/*.js"]}]
Run Code Online (Sandbox Code Playgroud)