Jest: How to ignore or skip some test files ?

Created on 28 Sep 2016  路  18Comments  路  Source: facebook/jest

I've used Mocha, chai, enzyme testing my test files of redux-saga. Recently, I started trying to use Jest to test my components. After looking through Jest documentation and examples and spending much time to try, I can't find the way to avoid Jest test my test files of redux-saga.

Any advice and suggestions will be greatly appreciated :)

my project structure is shown below
http://imgur.com/a/yZsSE

Most helpful comment

I was searching for a way to skip files when calling from command line, finally resorted to using testPathIgnorePatterns option:

jest --testPathIgnorePatterns file.js
# or, when from npm
npm test -- --testPathIgnorePatterns file.js

All 18 comments

@cpojer link is broken

@SimenB thanks

I was searching for a way to skip files when calling from command line, finally resorted to using testPathIgnorePatterns option:

jest --testPathIgnorePatterns file.js
# or, when from npm
npm test -- --testPathIgnorePatterns file.js

Thanks @aularon, annoyingly this isn't documented as a CLI option! https://jestjs.io/docs/en/cli

@aularon can we use regex there?

@Qurbaaan jest --testPathIgnorePatterns="api.*" works for me

Is there a filename pattern that can be used without a config change, such as something.skip.test.js ?

No, you can configure testMatch to not match those

Here is an example how to ignor an entire folder
"test-once": "CI=true react-scripts test --testPathIgnorePatterns=src/app/components/Alpha"

this works for me to ignore all snapshot test for example:

jest --coverage --testPathIgnorePatterns=./test/jest/snapshot/*

can i ignore functions like componentDidMount() ?

How can I ignore from the cli an array of directories? trying "test:coverage": "ek-web-react-test --coverage --watchAll --testPathIgnorePatterns='coverage/**, node_modules/**, **/**.test.js, **/**.stories.js, **/index.js'" on the scripts but I get an error

Is there some special _comment_ that can be placed inside a test file to let jest know to ignore it?

I would like to keep the ignore definition inside the ignored files and not as an external array, because file names/paths may change and the exclusion will break if defined externally.

jest ignore

// jest.config.js
const {defaults} = require('jest-config');

module.exports = {
  // ...
  moduleFileExtensions: [
    ...defaults.moduleFileExtensions,
    'js',
    'mjs',
    // 'jsx',
    // 'ts',
    // 'tsx',
  ],
  // ...
  // preset: [],
  modulePathIgnorePatterns: [
    "<rootDir>/dist/",
    "<rootDir>/000-xyz/",
    "<rootDir>/practices/",
  ],
  watchPathIgnorePatterns: [
    "<rootDir>/dist/",
    "<rootDir>/000-xyz/",
    "<rootDir>/practices/",
  ],
  testPathIgnorePatterns: [
    "<rootDir>/build/",
    "<rootDir>/node_modules/",
  ],
  coveragePathIgnorePatterns: [
    "<rootDir>/build/",
    "<rootDir>/node_modules/",
  ],
};

refs

https://www.cnblogs.com/xgqfrms/p/13598791.html

I was just scratching my head over the syntax of --testPathIgnorePatterns as well. After a bunch of experimenting, this _appears_ to work as desired, on Windows:

--testPathIgnorePatterns modules/a modules/b modules/c modules/d

that is apparently "array"-ish enough to work.

Was this page helpful?
0 / 5 - 0 ratings