Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Jest still skips my node_modules package after transformIgnorePatterns

ErrorA 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.

  • Applies to Jest 30, babel-jest (bundled with Jest), Node.js 22, pnpm
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 --clearCache
  2. 2Match what you see, then jump to its fix

    1. 1You 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. 2The same pattern works with npm or yarn but not with pnpmpnpm stores packages under node_modules/.pnpmFix
    3. 3The package is allowed through, but its import or JSX syntax is still not convertedA custom transform replaced babel-jestFix

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 transformIgnorePatterns

2Fix

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.js

Confirmed by jestjs.io

Did this fix it?

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 | head

2Fix

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 jest

Confirmed by jestjs.io

Did this fix it?

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.js

Confirmed by jestjs.io

Did this fix it?

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 fix

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.