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 3 years ago
- Reactions: 100
- Comments: 49
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 3 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 gburgett 2 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 2 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 2 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 2 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 2 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 anthony0506/react-native-maps by anthony0506 2 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 2 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 2 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 osk-poita121/react-native-maps by osk-poita121 2 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 GoldenDragon0710/google-map-react-native by GoldenDragon0710 2 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 PainStaker0331 2 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 2 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 2 years ago
Add
@react-native
to yourtransformIgnorePatterns
like 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.
Try to create babel.config.js with next content
module.exports = { presets: ['module:metro-react-native-babel-preset'], };
Adding the @react-native to the
transformIgnorePatterns
in the jest config solved it for meI found out the solve ✅ You should remove react-native from transformIgnorePatterns regexp array !
as Problem was in
type
define into js file which dont transforming because we was disable it. React native 64 developers addtype ErrorHandler
and 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-native
is now shipping untranspiled Flow code for some reason. To work around this, I installed@babel/plugin-transform-flow-strip-types
and added it to my Babel config. Combined with the defaulttransformIgnorePatterns
value 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.
All of this solutions do not work for me
I ran into the original issue using
react-native:0.64.1
,jest:27.5.1
andts-jest:27.1.4
. Based on a combination of the answers above, I fixed the issue and trimmed myjest.config.js
down to the minimum. This is what it looks like:I can confirm this transform works for me!
I was able to fix this
starting with a
jest.config.js
file that was producing the error:none of the answers above worked for me. no variation of
transformIgnorePatterns
worked either, jest still went in and tried to transpile the bad polyfill file no matter what.i figured out my
babel.config.js
file wasn’t getting picked up automatically… since I added aconsole.log
and it wasn’t runningadding this transform fixed it:
also note that the
configFile
must be a relative path from your current directory that you are runningjest
from.rootDir
will not be respectedthis was my
babel.config.js
… no need for@babel/plugin-transform-flow-strip-types
either…In my case, I’ve set
transformIgnorePatterns: []
only. Works like a charm.Same here after upgrading ts-jest, jest, and react-native to latest.
I fixed it by add this:
"transformIgnorePatterns": [ "/node_modules/(?!(@react-native|react-native)).*/" ]
Try renaming your Babel config from
.babelrc
tobabel.config.js
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’m having a hell of time with this.
When I get what should be the right
transformIgnorePatterns
the error changes to this:I’ve added
"plugins": ["@babel/plugin-transform-flow-strip-types"]
to mybabel.config.js
also 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-native
contributors, 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 thetype
in.js
problem withjest
, no need to repeat the path alias set inbabel.config.js
injest.config.js - moduleNameMapper
again, 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
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
[update] I found using
jest: "^27"
works. Just usepreset: react-native
and don’t need to overridetransform
nortransformIgnorePatterns
still 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!
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.ts
All above consolidated for Expo and TS
Same issue here, but I get this failure when using typescript.
I had my project with
EXPO
anddetox
configuration 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.js
My
package.json
in
babel.config.js
add thisHope it helps to someone ❤️
Thanks @Eandalf,
babelConfig: true
+ emptytransformIgnorePatterns
does the trick.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