代码之家  ›  专栏  ›  技术社区  ›  Leon Gaban

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

  •  2
  • Leon Gaban  · 技术社区  · 5 年前

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

    我试着把他们藏起来,但当我跑的时候 npm run coverage 他们仍然会出现。

    enter image description here

    包裹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/"
      ]
    }
    

    巴别塔

    {
      "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 回复  |  直到 4 年前
        1
  •  6
  •   Leon Gaban    5 年前

    我所需要的就是这个包裹。json

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

    然后把它从 "testPathIgnorePatterns": <-这里和里面都有 coveragePathIgnorePatterns 使我的测试永远运行,样式仍然出现。

    我还将其从 .babelrc :

    ["istanbul",{"exclude": ["styles/*.js"]}]
    
        2
  •  0
  •   user9677330 user9677330    5 年前
    1. 将其添加到配置(package.json)中:

    modulePathIgnorePatterns:[“directoryNameToIgnore”]

    或者:

    modulePathIgnorePatterns:[“/dist/”]

    1. 以及:

    coveragePathIgnorePatterns:[“/样式/”]