【问题标题】:Jest, how to ignore test coverage of specific folders?开玩笑,如何忽略特定文件夹的测试覆盖率?
【发布时间】:2019-09-01 02:34:18
【问题描述】:

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

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

package.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/"
  ]
}

babelrc

{
  "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"]
      ]
    }
  }
}

【问题讨论】:

    标签: javascript testing jestjs


    【解决方案1】:

    我只需要 package.json 中的这个

    "coveragePathIgnorePatterns": [
      "<rootDir>/styles/"
    ],
    

    并从"testPathIgnorePatterns": 中删除它coveragePathIgnorePatterns 中都有它导致我的测试永远运行并且样式仍然出现。

    我也从.babelrc 中删除了这个:

    ["istanbul",{"exclude": ["styles/*.js"]}]
    

    【讨论】:

      【解决方案2】:
      1. 将此添加到您的配置 (package.json):

      modulePathIgnorePatterns: ["directoryNameToIgnore"]

      或:

      modulePathIgnorePatterns: ["/dist/"]

      1. 和:

      coveragePathIgnorePatterns: ["/styles/"]

      【讨论】:

      • 那行不通,只是想通了......现在发布
      猜你喜欢
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      相关资源
      最近更新 更多