create-react-native-app: npm test always fails
Description
I want to run tests.
Expected Behavior
Tests succeed.
Observed Behavior
Tests always fail. With a variety of syntax errors.
Environment
Please run these commands in the project folder and fill in their results:
npm ls react-native-scripts: 0.0.28 & 0.0.29npm ls react-native: 0.43.4npm ls expo: 16.0.0node -v: 6.10.3npm -v: 3.10.10
Also specify:
- Operating system: Xubuntu 17.4
- Phone/emulator/simulator & version: Jest 19.0.2
Reproducible Demo
I got only one test, it’s the default test CRNA creates.
I try to run it with npm run test
I get SyntaxError: Unexpected token import in my own source files when I use this config:
"jest": {
"preset": "jest-expo"
},
I get SyntaxError: Unexpected identifier on type ModuleConfig = [ in the react-native files when I use this config:
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"/node_modules/",
"/src/"
]
},
I get SyntaxError: Unexpected token import on a react-native import in the expo package when I use this config:
"jest": {
"preset": "react-native"
},
I get SyntaxError: Unexpected token ) in the react-native packages jest setup when I use this config:
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"/node_modules/",
"/src/"
]
},
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 8
- Comments: 23 (11 by maintainers)
try this:
Oh sorry I should have read the original post more carefully, I jumped in half way.
What you’re seeing here is a quirk of how babel and jest work with react-native.
.babelrcis present, whereas jest just assumes no babel config. Currently create-react-native-app deletes the.babelrcwhen you eject because it was assumed that it wasn’t needed due to the stage-0 default, but it should not do that because jest needs it.So, you’ll need to install
babel-preset-react-nativeand re-create.babelrcon an ejected project like so:This is a bug that needs to be resolved, see #243
I was receiving a
Syntax Error: Unexpected token importoriginating from react-navigation while attempting to run tests. After addingto the jest config in “package.json”, the react-navigation error was no longer reported. Instead, jest complained with an error similar to the one mentioned here (involving
expomodules). This leads me to believe configuration settings in “package.json” override those in “jest-expo/jest-preset.json”. Is this what is happening?I ended up having to copy the setting from “jest-preset.json” and include
react-navigation:Is there a more appropriate way to accomplish this?
it should work if you just remove the following entirely:
unless you are including another package that needs to be transpiled (see https://github.com/react-community/create-react-native-app/issues/199#issuecomment-307890700). nonetheless, I’m glad you found something that works
I would love some help on this too.
Jest keeps failing and displays this:
I’ve tried the various methods listed here, but nothing works.
My package.json file:
I’ve tried with the jest preset set to jest-expo as well, but no dice.
And my .babelrc file:
A funny thing that happens when I type
npm ls --depth=1into the console is that I get a huge list of missing dependencies. But my project runs just fine. I’m not sure what’s up with that.@brentvatne my babelrc is:
I was able to fix the problem using a variation of @bang88 's approach: