Jest still skips my node_modules package after transformIgnorePatterns
A package under node_modules fails to parse in a Jest test (import or JSX syntax not converted)Short answer
Jest skips any file whose path matches any entry in transformIgnorePatterns. So an extra pattern can undo your exception, a pnpm symlink path can dodge your pattern, or a custom transform setting can drop babel-jest. Print which package fails, fix the one pattern that matches it, and clear the cache.
Fix it fast
1Find out which cause you have
Note which node_modules file the Jest error names. That is the file Jest did not transform. Then print the config Jest actually uses, because a second config file or a package.json key can win.
npx jest --showConfig | grep -A6 transformIgnorePatterns npx jest --clearCache2Match what you see, then jump to its fix
- 1
You allowed the package in one pattern, but the array has another entry such as /bar/ or the default /node_modules/A second pattern still matches the packageFix - 2
The same pattern works with npm or yarn but not with pnpmpnpm stores packages under node_modules/.pnpmFix - 3
The package is allowed through, but its import or JSX syntax is still not convertedA custom transform replaced babel-jestFix
- 1
What does the error look like?
A package under node_modules fails to parse in a Jest test (import or JSX syntax not converted)transformIgnorePatterns edited, but the same package still fails
Cause 1 of 3
A second pattern still matches the package
1Check
A file is skipped if it matches any pattern in the array. Your exception in one entry does nothing when another entry matches the same path.
npx jest --showConfig | grep -A6 transformIgnorePatterns2Fix
Keep one pattern that ignores node_modules except the packages you list, and remove or narrow the others.
// jest.config.js
module.exports = {
transformIgnorePatterns: ['/node_modules/(?!(foo|bar)/)'],
};3Verify
Run the failing test again with a clean cache.
npx jest --clearCache && npx jest path/to/failing.test.jsConfirmed by jestjs.io
Cause 2 of 3
pnpm stores packages under node_modules/.pnpm
1Check
With pnpm, packages in node_modules are symlinks to paths under node_modules/.pnpm/. A pattern like node_modules/(?!(package-a)/) therefore does not match the real path.
ls -l node_modules | head2Fix
Match the .pnpm folder name, which is the package name plus @ and its version. The folder name uses + in place of / for scoped packages.
// jest.config.js
module.exports = {
transformIgnorePatterns: [
'<rootDir>/node_modules/.pnpm/(?!(package-a|@scope\\+pkg-b)@)',
],
};3Verify
Rerun the test suite.
npx jest --clearCache && npx jestConfirmed by jestjs.io
Cause 3 of 3
A custom transform replaced babel-jest
1Check
By default babel-jest handles .js, .jsx, .ts and .tsx files. If you add your own transform entry, babel-jest is no longer applied unless you list it.
npx jest --showConfig | grep -B1 -A8 '"transform"'2Fix
Add babel-jest back next to your other transformer.
// jest.config.js
module.exports = {
transform: {
'\\.[jt]sx?$': 'babel-jest',
'\\.css$': 'some-css-transformer',
},
};3Verify
Run the failing test; the syntax error should be gone.
npx jest path/to/failing.test.jsConfirmed by jestjs.io
How do I stop it coming back?
- Put transformIgnorePatterns in one config file only, and check it with --showConfig after every change.
- Use <rootDir> in patterns so they do not match another project on the machine.
- List only the packages that truly ship untranspiled code.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Jest fails to parse a React Native or Expo dependency
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax
Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source.
3 causes, each with a fixWeb apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fix