【问题标题】:Unable to ignore specific files with jest.config无法使用 jest.config 忽略特定文件
【发布时间】:2022-10-02 17:28:48
【问题描述】:

试图忽略所有索引文件,特别是 src/index.jsxsrc/reportWebVitals.js 文件,但是我的覆盖命令仍然显示被覆盖的行。

My Github repo在正确的dev 分支上,这是一个问题。

根据文档,它应该像将文件添加到coveragePathIgnorePatternstestPathIgnorePatterns 一样简单。

jest.config

module.exports = {
  testEnvironment: \'node\',
  testEnvironmentOptions: {
    NODE_ENV: \'test\',
  },
  restoreMocks: true,
  coveragePathIgnorePatterns: [
    \'node_modules\',
    \'index.js\',
    \'index.jsx\',
    \'server/src/config\',
    \'server/src/app.js\',
    \'src/index.jsx\',
    \'src/reportWebVitals.js\',
    \'tests\',
  ],
  coverageReporters: [\'text\', \'lcov\', \'clover\', \'html\'],
  testPathIgnorePatterns: [\'index.js\', \'index.jsx\', \'src/index.jsx\', \'src/reportWebVitals.js\'],
  roots: [\'<rootDir>/server/tests\'],
};

还在这里尝试了更长的版本:

module.exports = {
  testEnvironment: \'node\',
  testEnvironmentOptions: {
    NODE_ENV: \'test\',
  },
  restoreMocks: true,
  collectCoverageFrom: [
    \'src/{!(index),}.jsx\',
    \'src/{!(reportWebVitals),}.js\',
    \'src/{!(store),}.js\'
  ],
  coveragePathIgnorePatterns: [
    \'node_modules\',
    \'index.js\',
    \'index.jsx\',
    \'server/src/config\',
    \'server/src/app.js\',
    \'index.jsx\',
    \'reportWebVitals.js\',
    \'store.js\',
    \'tests\',
  ],
  coverageReporters: [\'text\', \'lcov\', \'clover\', \'html\'],
  modulePathIgnorePatterns: [
    \'node_modules\',
    \'index.js\',
    \'index.jsx\',
    \'server/src/config\',
    \'server/src/app.js\',
    \'index.jsx\',
    \'reportWebVitals.js\',
    \'store.js\',
    \'tests\',
  ],
  watchPathIgnorePatterns: [
    \'node_modules\',
    \'index.js\',
    \'index.jsx\',
    \'server/src/config\',
    \'server/src/app.js\',
    \'index.jsx\',
    \'reportWebVitals.js\',
    \'store.js\',
    \'tests\',
  ],
  testPathIgnorePatterns: [
    \'node_modules\',
    \'index.js\',
    \'index.jsx\',
    \'server/src/config\',
    \'server/src/app.js\',
    \'index.jsx\',
    \'reportWebVitals.js\',
    \'store.js\',
    \'tests\',
  ],
  roots: [\'<rootDir>/server/tests\'],
};

我的 package.json 脚本

\"client-dev\": \"react-scripts start\",
\"client-build\": \"react-scripts build\",
\"client-test\": \"react-scripts test ./src\",
\"client-coverage\": \"react-scripts test ./src --coverage\",

更新:我注意到一件有趣的事情,我从 jest.config.js 中删除了所有忽略规则,并且覆盖范围仍然相同,node_modules 在覆盖范围中不是问题......所以现在探索我的项目是否正在挑选上配置。

module.exports = {
  testEnvironment: \'node\',
  testEnvironmentOptions: {
    NODE_ENV: \'test\',
  },
  restoreMocks: true,
  coverageReporters: [\'text\', \'lcov\', \'clover\', \'html\'],
};
  • 我不确定,但我认为您可以在 jest config 中对这些文件使用 glob 模式。尝试src/**/index.{js,jsx} 忽略src 目录中的所有index.jsindex.jsx 文件。
  • @h-sifat 有哪些规则?

标签: javascript reactjs jestjs


【解决方案1】:

您必须在测试脚本中明确提及您的配置文件路径。如果这样做,将导致此处讨论的另一个问题:https://stackoverflow.com/a/68912023/10055300。最好在 package.json 本身中拥有 jest 配置,而不是为其创建一个单独的文件。

【讨论】:

    【解决方案2】:

    由于您使用的是未弹出的 create-react-app,因此覆盖 jest 配置(不弹出)的唯一方法是在父键 jest 下的 package.json 中添加允许的配置键。

    像这样的东西:

    {
      "jest": {
        "coveragePathIgnorePatterns": [
          "index.jsx"
        ],
      }
    }
    

    如果你查看facebook/create-react-app/.../react-scripts/.../createJestConfig.js 的源代码,你会看到 react-scripts 只允许覆盖一组固定的 jest 配置键,并且只能使用 package.json 方法。它不支持添加您自己的 jest.config.js 文件。 CRA(react-scripts) 创建自己的 jest 配置并使用它来运行您的测试。

    【讨论】:

      【解决方案3】:

      答案是我的 jest.config.js 被项目忽略了。最初,当我开始从事这个项目时,这个配置已经包含在内,这就是为什么我保留它而不是在我的 package.json 中使用 jest 配置。

      在使用 package.json 中的笑话规则后,忽略现在 100% 工作。

      我不确定为什么 jest.config.js 不起作用。

      "jest": {
        "coveragePathIgnorePatterns": [
          "node_modules",
          "server/src/config",
          "store.js",
          "index.jsx",
          "index.js",
          "tests"
        ],
        "watchPathIgnorePatterns": [
          "node_modules",
          "server/src/config",
          "store.js",
          "index.jsx",
          "index.js",
          "tests"
        ]
      },
      

      【讨论】:

        猜你喜欢
        • 2021-06-04
        • 1970-01-01
        • 1970-01-01
        • 2011-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-18
        • 1970-01-01
        相关资源
        最近更新 更多