alexmngn
alexmngn

Reputation: 9597

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?

Upvotes: 19

Views: 16661

Answers (3)

FiveOFive
FiveOFive

Reputation: 441

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

Upvotes: 1

Nikita S
Nikita S

Reputation: 137

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.

Upvotes: -1

GibboK
GibboK

Reputation: 73918

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.

Upvotes: 30

Related Questions