开玩笑,如何忽略特定文件夹的测试范围?

时间:2019-09-01 02:34:18

标签: javascript testing jestjs

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

我试图这样隐藏它们,但是当我运行npm run coverage时,它们仍然会显示。

enter image description here

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

2 个答案:

答案 0 :(得分:2)

我只需要在package.json中

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

并将其从"testPathIgnorePatterns":中删除<-在这里和在coveragePathIgnorePatterns中都使用它会导致我的测试永久运行并且样式仍然显示。

我还从.babelrc中删除了此内容:

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

答案 1 :(得分:1)

  1. 将此添加到您的配置(package.json):
  

modulePathIgnorePatterns:[“ directoryNameToIgnore”]

或:

  

modulePathIgnorePatterns:[“ / dist /”]

  1. 和:
  

coveragePathIgnorePatterns:[“ / styles /”]