upgrade-support: jest fails with @react-native/polyfills/error-guard.js after upgrade to RN 0.64.0 and react 17.0.1
Environment
System: OS: Linux 5.4 Ubuntu 18.04.5 LTS (Bionic Beaver) CPU: (8) x64 Intel® Core™ i7-8550U CPU @ 1.80GHz Memory: 1.29 GB / 15.52 GB Shell: 4.4.20 - /bin/bash Binaries: Node: 14.5.0 - ~/.nvm/versions/node/v14.5.0/bin/node Yarn: 1.22.5 - /usr/bin/yarn npm: 6.14.9 - ~/.nvm/versions/node/v14.5.0/bin/npm Watchman: 20200920.192359.0 - /usr/local/bin/watchman SDKs: Android SDK: API Levels: 19, 24, 25, 27, 28, 29, 30 Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.0 System Images: android-22 | Google APIs Intel x86 Atom, android-25 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom Android NDK: Not Found IDEs: Android Studio: Not Found Languages: Java: 11.0.10 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 17.0.1 => 17.0.1 react-native: 0.64.0 => 0.64.0 npmGlobalPackages: react-native: Not Found
Things I’ve done to figure out my issue
did react-native upgrade which upgraded the appplication
Upgrading version
react-native 0.64.0 react 17.0.1
Description
After the upgrade, I did yarn test but all tests fail with error:
/node_modules/@react-native/polyfills/error-guard.js:14
type ErrorHandler = (error: mixed, isFatal: boolean) => void;
^^^^^^^^^^^^
SyntaxError: Unexpected identifier
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 113
- Comments: 56
Commits related to this issue
- downgraded RN version 0.64 -> 0.63 reason: unable to run jest in new RN version reference: https://github.com/react-native-community/upgrade-support/issues/152 — committed to srkela/GitAPI by srkela 5 years ago
- Add @react-native to transform ignore patterns https://github.com/react-native-community/upgrade-support/issues/152#issuecomment-830982226 — committed to watermarkchurch/clc-app-v2 by deleted user 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: https://github.com/react-native-community/upgrade-support/issues/152 Also, it's part of most libra... — committed to monholm/react-native-maps by monholm 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to monholm/react-native-maps by monholm 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to joshpeterson30489/maps-develop-with-react-native by joshpeterson30489 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to superstar1205/Map-ReactNative by superstar1205 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to GET-A-WAY/react-native-maps by monholm 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to johney6767/Map-ReactNative by deleted user 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to MMM-0121/react-native-maps by MMM-0121 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to PainStaker0331/react-native-maps by SmartBigGuru 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to Super-CodeKing/react_native_map by Super-CodeKing 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to fairskyDev0201/react-native-maps by fairsky0201 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to siuming0610/react-native-map by deleted user 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to DavidLee0501/React-Native-Map by DavidLee0501 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to PietroMorato/mobile-map by monholm 5 years ago
- test: cleanup jest setup babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way. — committed to pereira534/reactnative-google-maps by pereira534 5 years ago
Add
@react-nativeto yourtransformIgnorePatternslike this to yourjest.config.js:Also add the preset something like this in jest.config.js:
Check out for the libraries and update them accordingly to meet latest react version.
Any updates on this? Still facing the same issue.
Adding the @react-native to the
transformIgnorePatternsin the jest config solved it for meTry to create babel.config.js with next content
module.exports = { presets: ['module:metro-react-native-babel-preset'], };I found out the solve ✅ You should remove react-native from transformIgnorePatterns regexp array !
as Problem was in
typedefine into js file which dont transforming because we was disable it. React native 64 developers addtype ErrorHandlerand we need it transform with ts-loadermy self jest.config.js file in root (example)
Other folks in this discussion have mentioned TypeScript as the culprit, but these are actually Flow types. It seems like
react-nativeis now shipping untranspiled Flow code for some reason. To work around this, I installed@babel/plugin-transform-flow-strip-typesand added it to my Babel config. Combined with the defaulttransformIgnorePatternsvalue from the React Native Jest preset, my tests are now able to run again.I would encourage the maintainers to ship code that does not use untranspiled Flow, as that breaks the assumptions a lot of JavaScript tooling makes.
Wow it’s 2024 and it’s amazing this issue is still open years later with so many solutions and none that actually work for me. I’m in a bare react native project, no expo “for reasons”. And I just wanna write some test code. Amazing that it’s a multi-hour research project just to write some unit tests. It actually saeems quicker to write a new test runner than to spend time learning how to configure jest and babel and all this mess. Sigh…
I was having the same issue, what worked for me was giving an empty array
transformIgnorePatterns: [],All of this solutions do not work for me
I ran into the original issue using
react-native:0.64.1,jest:27.5.1andts-jest:27.1.4. Based on a combination of the answers above, I fixed the issue and trimmed myjest.config.jsdown to the minimum. This is what it looks like:I can confirm this transform works for me!
Try renaming your Babel config from
.babelrctobabel.config.jsI was able to fix this
starting with a
jest.config.jsfile that was producing the error:none of the answers above worked for me. no variation of
transformIgnorePatternsworked either, jest still went in and tried to transpile the bad polyfill file no matter what.i figured out my
babel.config.jsfile wasn’t getting picked up automatically… since I added aconsole.logand it wasn’t runningadding this transform fixed it:
also note that the
configFilemust be a relative path from your current directory that you are runningjestfrom.rootDirwill not be respectedthis was my
babel.config.js… no need for@babel/plugin-transform-flow-strip-typeseither…I fixed it by add this:
"transformIgnorePatterns": [ "/node_modules/(?!(@react-native|react-native)).*/" ]In my case, I’ve set
transformIgnorePatterns: []only. Works like a charm.Same here after upgrading ts-jest, jest, and react-native to latest.
After testing almost every solution posted above, the only combination that worked for me was the following:
Please note that I’m using EXPO 50 which recommends removing the Babel config file and use the Metro config file. I’m not sure if there is a way to solve this WITHOUT adding the Babel file.
babel.config.js ({ loose: true } is key for this to work, thanks @elenitaex5 🙏🏼)
jest.config.js (remove the “jest” entry from your package.json
Make sure you install this dev dependencies: @babel/plugin-transform-private-methods @babel/preset-typescript metro-react-native-babel-preset
I’m having a hell of time with this.
When I get what should be the right
transformIgnorePatternsthe error changes to this:I’ve added
"plugins": ["@babel/plugin-transform-flow-strip-types"]to mybabel.config.jsalso seems to have no effect.Related issue with a repository that I can’t make work here: https://github.com/JacopoPatroclo/nx-react-native-expo/issues/21
Same error by me and i just created new project with typescript and cant run even initial test…
My config: Babel.config.js
jest.config.ts
This one solved my problem. Great thanks to @interhub.
For my case,
does the trick.
Even if I put
"/node_modules/(?!(@react-native|react-native)/).*/"intransformIgnorePatterns, I still gottype ErrorHandler ... SyntaxError: Unexpected identifier. After digging inreact-native’sjest-preset.js(source code), the line ("/node_modules/(?!(@react-native|react-native)/).*/") was already there. There must be something else going wrong. Then, I saw @interhub 's linebabelConfig: true. Suddenly, I remembered some stuff written byreact-nativecontributors, they stated that they used Babel to transpile Typescript instead of compiling Typescript code usingtsc. Then, if we could build the app, but the tests failed, how about letting Babel to take care of the mess? (No offense, I am more used to Typescript than Flow.) Then, it works. After using this approach, aside from solving thetypein.jsproblem withjest, no need to repeat the path alias set inbabel.config.jsinjest.config.js - moduleNameMapperagain, those non-relative path imports will be taken care by Babel (I am usingbabel-plugin-module-resolver).I am using react-native@0.64.2, jest@^26.6.3, ts-jest@^26.5.6. The following is my
jest.config.js.For
tsconfig.spec.json, please refer to Using with React Native | ts-jest.any update on this? still test cases breaking on RN version 0.64.2
For anyone still coming here for this issue, it could be from jest upgrade breaking the location of your
babel.config.js. Basically jest 28 had a breaking change with regards to the location of the babel config.See: https://stackoverflow.com/questions/74785831/test-suite-failed-to-run-node-modules-react-native-polyfills-error-guard-js-m
[update] I found using
jest: "^27"works. Just usepreset: react-nativeand don’t need to overridetransformnortransformIgnorePatternsstill not working 😔 I’ve tried all methods mentioned above.
Here’s the repo which is just a new RN project. If possible, please someone to help. Thanks!
Same issue here, but I get this failure when using typescript.
What worked for me.
My error
node_modules/@react-native/polyfills/error-guard.js:14 type ErrorHandler = (error: mixed, isFatal: boolean) => void; ^^^^^^^^^^^^
jest.config.ts file below. I added @react-native to it
jest.config.tsAll above consolidated for Expo and TS
As detailed above, my project uses TypeScript. Initially, I focused only on handling TypeScript syntax and overlooked the Flow syntax within react-native (Flow syntax is officially used in js files). When running jest tests, the default configuration ignores node_modules. The first step is to add exceptions for the relevant react-native code (if you have other code to handle, you can follow the same strategy).
Next, we need to add support for Flow syntax by including the following settings.
Here is my complete configuration for your reference.
Including the above solutions, what DID work, was adding the
projectsto myjest.config.jsin my projects root directory. I am running a mono repo, so my frontend was a directory inside the root repo. I rannpx jestin my frontend directory as well.(note: change
project-nameto your projects name) Also, having thetransformIgnorePatternsand the others helpsMy issue also had a separate issue, which was looking at the root directory, and I had to tell it to only worry about this project.
So I had to solve the original issue above, AND tell it not to look in the root project directory.
I had my project with
EXPOanddetoxconfiguration still… I had this error too, but looking for a solution here I fix my code. Including config in package Json I had another error:So I added it into my devs and modify my
babel.config.jsMy
package.jsonin
babel.config.jsadd thisHope it helps to someone ❤️
Thanks @Eandalf,
babelConfig: true+ emptytransformIgnorePatternsdoes the trick.It’s working for me. Because I was moved metro config
(presets: ['module:metro-react-native-babel-preset'])to .babelrc and it was not working. Now I created an babel.config.js and put that into it and test working correctly.Same error ` C:\SourceCode\leyserkids-kp-rn\node_modules\react-native-config\index.js:6 import { NativeModules } from ‘react-native’; ^^^^^^
`
Same here, with RN 0.63.3