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 syntaxShort 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.
Fix it fast
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 302Match what you see, then jump to its fix
- 1
The failing file is your own code, or a file inside react-native or expo itselfThe project has no React Native or Expo presetFix - 2
The failing path is node_modules/<package>/... and the token is import, export or JSXA dependency in node_modules is published as untranspiled sourceFix - 3
You added a second pattern for a package and nothing changedTwo transformIgnorePatterns entries cancel each otherFix
- 1
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/null2Fix
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 jestConfirmed by docs.expo.dev
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_modules2Fix
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-cacheConfirmed by docs.expo.dev
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/null2Fix
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 jestConfirmed by jestjs.io
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web 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 fixWeb apps
A React component crashes while destructuring props or state
TypeError: Cannot destructure property 'b' of 'null' as it is null.
JavaScript throws this when the right side of a destructuring pattern is undefined or null.
3 causes, each with a fixWeb apps
yarn is not recognized when running a React Native project
'yarn' is not recognized as an internal or external command, operable program or batch file.
The shell cannot find a yarn launcher on your PATH.
3 causes, each with a fixWeb apps
Sentry: Non-Error promise rejection captured with value in a React app
Non-Error exception (or promise rejection) captured with keys: x, y, z
Sentry raises this event when something that is not an Error object is thrown or used to reject a promise, for example a plain object from an API.
3 causes, each with a fixWeb 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 fixWeb apps
TypeError: undefined is not iterable when destructuring a hook or array
TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
Array destructuring, spread, for...of and new Set all need an iterable.
4 causes, each with a fix