Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore filename convention with Jest testPathIgnorePatterns (React, Jest, Regex)

Im trying to set up a convention to ignore any test that follows a pattern. The filename would look like this if you wanted jest to ignore it during npm test.

DISABLED.MyComponent.test.js

trying to use testPathIgnorePatterns

testPathIgnorePatterns: ['<rootDir>/src/**/DISABLED.{*}.js'],

The error is either it is still being run, or it throws a error that the regex is invalid.

like image 627
Kyle Treman Avatar asked Dec 10 '19 18:12

Kyle Treman


3 Answers

Jest uses glob patterns to match the test files, so prefixing the default entries in the testMatch configuration with !(DISABLED.) should do the job.

package.json
{
  // ... rest of the package
  "jest": {
    "testMatch": [
      "**/__tests__/**/!(DISABLED.)*.[jt]s?(x)",
      "**/!(DISABLED.)?(*.)+(spec|test).[tj]s?(x)"
    ]
  }
}

Side note: You can also "disable" the test file by renaming the main describe block to xdescribe or describe.skip, which would give visibility that there are skipped tests instead of completely ignore the file

Test Suites: 1 skipped, 11 passed, 11 of 12 total
Tests:       2 skipped, 112 passed, 114 total
like image 55
Teneff Avatar answered Nov 15 '22 06:11

Teneff


testPathIgnorePatterns is an array of regex patterns that ignores any path that matches the listed expressions and should contain the default "/node_modules/" as well. While it is not intended you may as well use testPathIgnorePatterns to exclude specific files:

So, your best bet would be to move the ignored test to s separate folder, e.g.

"jest": {
  "testPathIgnorePatterns": [
    "/node_modules/",
    "<rootDir>/ignore/this/path/" 
  ]
}
like image 22
wp78de Avatar answered Nov 15 '22 06:11

wp78de


The other answers here are helpful but miss the core fact:

testPathIgnorePatterns can be used to ignore your filenames. The problem is that testPathIgnorePatterns takes a RegExp, not a glob as you've specified.

The default setting for testPathIgnorePatterns is "/node_modules/", so it's probably useful to include it as well.

As an example, to ignore end to end tests that have an "e2e-" prefix, you could use:

"testPathIgnorePatterns": [
    "/node_modules/",
    "/e2e-"
]

In your specific case, this would be something like the following (note how the . is escaped in /DISABLED\\.):

"testPathIgnorePatterns": [
    "/node_modules/",
    "/DISABLED\\."
]
like image 29
rinogo Avatar answered Nov 15 '22 06:11

rinogo