简体   繁体   中英

Ignore a specific folder pattern in testMatch with Jest

I'm trying to ignore a specific folder in the testMatch pattern of Jest.

I have my tests in a __tests__ folder and I want to be able to have a folder __config__ into it with all files inside which should be ignored.

/__tests__
  /__config__
    someConfig.jsx
  /MyComponent.jsx
/MyComponent.jsx

My tests in the __tests__ folder will import the files from the __config__ folder so I don't want to ignore them with the transformIgnorePatterns .

But if I don't ignore the folder, Jest tries to run in the folder and returns an error:

FAIL src/components/__tests__/__config__/someConfig.jsx

Your test suite must contain at least one test.

I've tried a few different patterns but I can't find a way to ignore the __config__ folder

testMatch: [
  "**/__tests__/!(__config__)/**/*.(js)?(x)", // Doesn't work
  "**/__tests__/(**|!__config__)/*.(js)?(x)", // Doesn't work
  "**/__tests__/(**|?!__config__)/*.(js)?(x)", // Doesn't work
  "**/__tests__/{**,!__config__}/*.(js)?(x)", // Doesn't work
],

Any idea how I can ignore the sub-folder like this?

Try to use testPathIgnorePatterns , instead.

If the test path matches any of the patterns, it will be skipped.

Use the string token to include the path to your project's root.

You can also exclude files in the testMatch array using negation ! . For example:

'testMatch': [
    '**/__tests__/**/*.+(ts|tsx|js)',
    '!**/excludeMe.ts'
  ],

Note that the order matters.

Each glob pattern is applied in the order they are specified in the config. For example ["! / fixtures / ", " / tests / / .js"] will not exclude fixtures because the negation is overwritten with the second pattern. In order to make the negated glob work in this example it has to come after / tests / / .js.

https://jestjs.io/docs/configuration#testmatch-arraystring

Try This pattern:

testMatch: ['**/--tests--/?(*.)(test).js?(x)'],

It is working for me. My folder structure is: --tests-- auth.test.js user.test.js Routes - - Anotherfolders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM