简体   繁体   English

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

[英]Jest, how to ignore test coverage of specific folders?

In my react project I have a root ./styles folder which contains various style objects for styled-components.在我的反应项目中,我有一个根./styles文件夹,其中包含样式组件的各种样式对象。 I do not want these files to show up in the coverage test.我不希望这些文件出现在覆盖率测试中。

I've tried to hide them like so, but when I run npm run coverage they still show up.我试图像这样隐藏它们,但是当我运行npm run coverage它们仍然出现。

在此处输入图片说明

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

All I needed was this in package.json我所需要的只是 package.json 中的这个

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

And removed it from "testPathIgnorePatterns": <- having it both here and in coveragePathIgnorePatterns caused my tests to run forever and styles still showed up.并将其从"testPathIgnorePatterns":删除"testPathIgnorePatterns": <- 在这里和coveragePathIgnorePatterns都有它导致我的测试永远运行并且样式仍然出现。

I also removed this from the .babelrc :我还从.babelrc删除了.babelrc

["istanbul",{"exclude": ["styles/*.js"]}]
  1. Add this to your config (package.json):将此添加到您的配置(package.json)中:

modulePathIgnorePatterns: ["directoryNameToIgnore"] modulePathIgnorePatterns: ["directoryNameToIgnore"]

or :或者 :

modulePathIgnorePatterns: ["/dist/"] modulePathIgnorePatterns: ["/dist/"]

  1. and :和 :

coveragePathIgnorePatterns: ["/styles/"] coveragePathIgnorePatterns: ["/styles/"]

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

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