Sam
Sam

Reputation: 2821

Improving Jest testMatch pattern matcher

I am using Jest and using this 'testMatch' pattern to match my test files inside jest.config.js:

testMatch: ['**/?(*.)+(spec).[j|t]s?(x)']

But it is incorrectly picking up some files that end in spec.js. I would like it to only match .spec.js but anything I try results in no files being found.

This regular expression seems to do what I want /.*\.spec\.[j|t]sx?/ but I think these pattern matchers work slightly differently

What do I have to change to make it only pick up [something].spec.js files (or jsx/ts/tsx)?

Upvotes: 0

Views: 973

Answers (1)

acat
acat

Reputation: 664

As you noted, these patterns aren't actually regex. If you're looking for a keyword to start your search, they're a form of "glob patterns." Also I might be mistaken but I'm pretty sure you want either [jt] or (j|t), not [j|t], may not matter though.

That being said, it sounds like you prefer regex syntax to glob syntax. I can't say I blame you! Instead of trying to figure out the glob that produces the same behavior as the regex you're after, you may want to look at the --testRegex option instead (which is also the property name for your jest.config.js file).

take a look here: https://jestjs.io/docs/configuration#testregex-string--arraystring As a bonus, for reasons that make no sense, testMatch can only take an array of patterns, but testRegex can take either a string (if you have just one pattern to match) or an array

Upvotes: 1

Related Questions