最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to ignore specific files with jest.config - Stack Overflow

programmeradmin0浏览0评论

Trying to ignore all index files, but specifically the src/index.jsx and src/reportWebVitals.js files, however my coverage mand still shows up covered lines.

My Github repo on the correct dev branch where this is an issue.

According to the docs, it should be as simple as adding the file to coveragePathIgnorePatterns and testPathIgnorePatterns.

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'],
};

Also tried with a much longer version here:

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'],
};

My package.json scripts

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

UPDATE: One interesting thing I noted, I removed all my ignore rules form the jest.config.js and the coverage is still the same, node_modules isn't a problem in the coverage... so now exploring if my project is even picking up the config.

module.exports = {
  testEnvironment: 'node',
  testEnvironmentOptions: {
    NODE_ENV: 'test',
  },
  restoreMocks: true,
  coverageReporters: ['text', 'lcov', 'clover', 'html'],
};

Trying to ignore all index files, but specifically the src/index.jsx and src/reportWebVitals.js files, however my coverage mand still shows up covered lines.

My Github repo on the correct dev branch where this is an issue.

According to the docs, it should be as simple as adding the file to coveragePathIgnorePatterns and testPathIgnorePatterns.

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'],
};

Also tried with a much longer version here:

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'],
};

My package.json scripts

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

UPDATE: One interesting thing I noted, I removed all my ignore rules form the jest.config.js and the coverage is still the same, node_modules isn't a problem in the coverage... so now exploring if my project is even picking up the config.

module.exports = {
  testEnvironment: 'node',
  testEnvironmentOptions: {
    NODE_ENV: 'test',
  },
  restoreMocks: true,
  coverageReporters: ['text', 'lcov', 'clover', 'html'],
};
Share Improve this question edited Jul 9, 2022 at 0:52 Leon Gaban asked Jul 6, 2022 at 15:18 Leon GabanLeon Gaban 39.1k122 gold badges349 silver badges550 bronze badges 2
  • I'm not sure but I think you can use glob pattern for these files in jest config. Try src/**/index.{js,jsx} to ignore all index.js and index.jsx files in the src directory. – h-sifat Commented Jul 7, 2022 at 0:11
  • @h-sifat for which rules? – Leon Gaban Commented Jul 9, 2022 at 0:49
Add a ment  | 

3 Answers 3

Reset to default 2 +250

You will have to explicitly mention your config file path in your test script. If you do so, it will lead to another issue which has been discussed here: https://stackoverflow./a/68912023/10055300. It would be ideal to have jest configs in package.json itself rather than having a separate file for it.

Since you are using an unejected create-react-app, the only way to override the jest configuration(without ejecting) is to add the allowed configuration keys in package.json under the parent key jest.

Something like this:

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

If you go through the source code for facebook/create-react-app/.../react-scripts/.../createJestConfig.js, you will see that react-scripts allows only a fixed set of jest configuration keys to be overridden, and only using the package.json method. It does not support adding your own jest.config.js file. CRA(react-scripts) creates its own jest configuration and uses that to run your tests.

The answer was my jest.config.js is being ignored by the project. Initially when I started working on this project this config was already included which why I kept it rather than using jest config inside of my package.json.

After using the jest rules inside package.json the ignores are working 100% now.

I am unsure as to why the jest.config.js did not work.

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

发布评论

评论列表(0)

  1. 暂无评论