Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest ignore all node_modules except 1 package

I'm using jest for doing testing and currently I can ignore node_modules. The issue, however, is that theres 1 package in node_modules that I want to get transpiled with babel. Right now my ignored patterns look like this:

testPathIgnorePatterns: ['./node_modules/'],

if my package is called 'my-package', how can I make it so testPathIgnorePatterns doesn't ignore that inside node_modules?

like image 439
duxfox-- Avatar asked Sep 24 '19 18:09

duxfox--


1 Answers

TL;DR - use this config key with this regex:

transformIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']

This is a 2 part answer.

Part 1 - the regex

The testPathIgnorePatterns key takes an array of strings/regex, so I can pass a regex that will match all node_modules except the one I want with something like this:

// one way to do it
testpathIgnorePatterns: ['/node_modules\/(?!my-package).+\.js$']

// another way to do it - this regex seems simpler to me
testpathIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']

this basically says, ignore all node_modules except my-package. However, the thing to note here is, testPathIgnorePatterns does not determine whether a module should be transpiled by babel or not. This key is for jest to know where your test files are. Basically, using this key, you're telling jest 'dont look in node_modules for files with the .test.js extension', because you don't want to run the tests of imported node_module packages.

So, in reality, you want to ignore this setting completely because jest will automatically default it to ['/node_modules/'], or if you wanted to add other paths to ignore, you'd need:

testpathIgnorePatterns: ['other/file/path', '/node_modules/']

Part 2 - where to use the regex

Given the context from part 1, the correct place to put the regex to allow jest to transpile specific packages from node_modules is: transformIgnorePatterns. So it would look like this:

transformIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']

this way, jest will not transpile any node_modules packages except 'my-package'. I believe the default value for both of the keys mentioned above is 'node_modules' - meaning, node_modules is automatically ignored in both the testPath and the transform

Jest docs for these configuration settings

like image 144
duxfox-- Avatar answered Sep 19 '22 15:09

duxfox--