Maaz Zindani

Loading the index…

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

Jest fails to parse a React Native or Expo dependency

ErrorJest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax

Short answer

Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source. The error names the file it could not parse. Use the right preset (jest-expo for Expo) and add that package to the transformIgnorePatterns allowlist in one single pattern.

  • Applies to Jest 29, jest-expo, React Native 0.7x, Expo SDK 5x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the first lines under the error: Jest prints the path of the file it could not parse. If the path contains node_modules, a package needs transforming. If it is your own file, the preset or Babel config is missing.

    npx jest --no-cache 2>&1 | head -n 30
  2. 2Match what you see, then jump to its fix

    1. 1The failing file is your own code, or a file inside react-native or expo itselfThe project has no React Native or Expo presetFix
    2. 2The failing path is node_modules/<package>/... and the token is import, export or JSXA dependency in node_modules is published as untranspiled sourceFix
    3. 3You added a second pattern for a package and nothing changedTwo transformIgnorePatterns entries cancel each otherFix

What does the error look like?

  • Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax

Cause 1 of 3

The project has no React Native or Expo preset

1Check

Look for a preset in the Jest config. An Expo project should use jest-expo as the base.

grep -n "preset" package.json jest.config.* 2>/dev/null

2Fix

Install jest-expo and set it as the preset.

npx expo install jest-expo jest @types/jest --dev

// package.json
"jest": {
  "preset": "jest-expo"
}

3Verify

Run the tests again. The same file should now be transformed.

npx jest

Confirmed by docs.expo.dev

Did this fix it?

Cause 2 of 3

A dependency in node_modules is published as untranspiled source

1Check

Jest skips node_modules, and the React Native preset only transforms react-native itself plus your own files. Note the package name in the path.

npx jest 2>&1 | grep -m1 node_modules

2Fix

Add the package to the allowlist inside transformIgnorePatterns. Put every package in one pattern separated by |. Two separate patterns cancel each other out, because a file is skipped if it matches any pattern.

// package.json (Jest config)
"jest": {
  "preset": "jest-expo",
  "transformIgnorePatterns": [
    "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@sentry/react-native|native-base|react-native-svg|my-package)"
  ]
}

3Verify

Run Jest again. If another package now fails, add it to the same pattern.

npx jest --no-cache

Confirmed by docs.expo.dev

Did this fix it?

Cause 3 of 3

Two transformIgnorePatterns entries cancel each other

1Check

Count the entries in transformIgnorePatterns. Jest skips a file when its path matches any one entry.

grep -n -A4 transformIgnorePatterns package.json jest.config.* 2>/dev/null

2Fix

Merge them into one negative-lookahead group, with the package names separated by |.

"transformIgnorePatterns": ["node_modules/(?!((@)?react-native|my-project)/)"]

3Verify

The package that failed is now transformed and the suite runs.

npx jest

Confirmed by jestjs.io

Did this fix it?

How do I stop it coming back?

  • Start from the framework preset (jest-expo for Expo) instead of a hand-written config.
  • Keep one transformIgnorePatterns entry and add packages to it.
  • Run Jest with --no-cache after changing transform settings.

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

TypeScript says a JavaScript module implicitly has an any type

Could not find a declaration file for module 'react-materialize'. 'path/to/module-name.js' implicitly has an 'any' type.

TypeScript found the JavaScript file but no types for it, and noImplicitAny turns that into an error.

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.