How to get rid of SyntaxError: Unexpected Token { when trying to test a React-Native with a Mapbox in Jest? - react-native

I've tried quite a few variations on this with no luck.
Situation: I want to render a React Native app that has a Mapbox map in it inside my Jest tests, so I can test the logic we wrote around it.
I've managed to reproduce the error I'm seeing in a mini repo: https://github.com/JKowalsky/mapbox-error-test-repo/tree/master
The mini repo is a react-native init default project that includes Mapbox and sets an access token, just so we can play with the dependency.
import MapboxGL from '#react-native-mapbox-gl/maps';
MapboxGL.setAccessToken('fakeyfakeytokentoken');
I'd expect the default __test__/App-test.js to still run, but it fails on the Mapbox include. It looked like a Babel problem, so I set up the following babel.config.js;
module.exports = function(api) {
api.cache(true);
return {
presets: [
'module:metro-react-native-babel-preset',
"#babel/preset-env",
"#babel/preset-flow"
],
plugins: [
"#babel/plugin-proposal-class-properties",
"#babel/plugin-syntax-dynamic-import"
]
};
};
And here's the package.json to run that:
{
"name": "mapboxerror",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest --no-cache",
"lint": "eslint ."
},
"dependencies": {
"#mapbox/geo-viewport": "^0.4.0",
"#react-native-mapbox-gl/maps": "^7.0.6",
"jsdom": "^15.1.1",
"mapbox": "^1.0.0-beta10",
"react": "16.9.0",
"react-native": "0.61.1"
},
"devDependencies": {
"#babel/core": "^7.6.2",
"#babel/plugin-proposal-class-properties": "^7.5.5",
"#babel/plugin-syntax-dynamic-import": "^7.2.0",
"#babel/preset-env": "^7.6.2",
"#babel/preset-flow": "^7.0.0",
"#babel/runtime": "^7.6.2",
"#react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.9.0",
"eslint": "^6.4.0",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native"
}
}
I still get the unexpected token error:
/Users/jennifer/dev/mapbox-error-test-repo/node_modules/#react-native-mapbox-gl/maps/javascript/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import {Animated, NativeModules, PermissionsAndroid} from 'react-native';
SyntaxError: Unexpected token {
25 | } from 'react-native/Libraries/NewAppScreen';
26 |
> 27 | import MapboxGL from '#react-native-mapbox-gl/maps';
| ^
28 | MapboxGL.setAccessToken('fakeyfakeytokentoken');
29 |
30 | const App: () => React$Node = () => {
at ScriptTransformer._transformAndBuildScript (node_modules/#jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/#jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (App.js:27:1)
Final curiosity: I've added a transform inside the jest config in package.json, but it then gives me a type error (this is not a typescript project.)
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(mapbox-gl|mapbox|#react-native-mapbox-gl))/"
]
}
Doing that gives me this error which is even more inscrutable:
yarn run v1.16.0
$ jest --no-cache
FAIL __tests__/App-test.js
● Test suite failed to run
TypeError: (0 , _typeof4.default) is not a function
at _typeof2 (node_modules/#babel/runtime/helpers/typeof.js:8:63)
at _typeof (node_modules/#babel/runtime/helpers/typeof.js:22:39)
at new Promise (node_modules/promise/lib/core.js:44:31)
at valuePromise (node_modules/promise/lib/es6-extensions.js:18:11)
at Object.<anonymous> (node_modules/promise/lib/es6-extensions.js:10:12)
at Object.<anonymous> (node_modules/promise/lib/index.js:9:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.192s
Ran all test suites.
error Command failed with exit code 1.
Any hints? I've been circling this issue for a while, and there isn't a ton of testing infrastructure support for Mapbox.

Alright, I actually managed to get this working. I had to mock both the Mapbox-gl and react-native EventEmitter dependencies in addition to adding a ton of babel transforms. The linked repository has been updated with a now passing unit test.

Related

ReactNative - Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)

I face this error when I'm try to use Drawer Component.
And There is nothing in simualtor screen.
My error message is as follow
Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
// plugins: ['react-native-reanimated/plugin'],
};
package.json
{
"name": "rn220720",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"#react-navigation/native": "6.0.11",
"#react-navigation/native-stack": "6.7.0",
"react": "17.0.2",
"react-native": "0.65.1",
"react-native-safe-area-context": "4.3.1",
"react-native-screens": "3.15.0"
},
"devDependencies": {
"#babel/core": "^7.12.9",
"#babel/runtime": "^7.12.5",
"#react-native-community/eslint-config": "^2.0.0",
"babel-jest": "^26.6.3",
"eslint": "7.14.0",
"jest": "^26.6.3",
"metro-react-native-babel-preset": "^0.66.0",
"react-native-codegen": "^0.0.7",
"react-test-renderer": "17.0.2"
},
"jest": {
"preset": "react-native"
}
}
My Android Version is Pixel_4_XL_API_31
What I did for solving this code
Before running this project I deleted emulator in the android studio
In Cmd Window, I typed 'npm cache clean --force' to get rid of npm cache at the project root folder
In Cmd Window, I typed '.\gradlew clean' to delete things about compiled project in android folder
Restart Computer booting.
Additionally, I think there is dependency trouble with npm. So, I change package mangement tool as
yarn. But The problem is appeared again

how to Add #babel/plugin-transform-flow-strip-types (https://git.io/vb49g) to the 'plugins' section of your Babel config to enable transformation?

am using react native web and want to add react navigation 5 to the project. However when I try building the web version upon adding react-navigation I get
./node_modules/react-native-gesture-handler/DrawerLayout.js
SyntaxError: D:\Parentlane\ReactNativeWeb\node_modules\react-native-gesture-handler\DrawerLayout.js: Support for the experimental syntax 'flow' isn't currently enabled (30:8):
28 | const SETTLING = 'Settling';
29 |
> 30 | export type PropType = {
| ^
31 | children: any,
32 | drawerBackgroundColor?: string,
33 | drawerPosition: 'left' | 'right',
Add #babel/plugin-transform-flow-strip-types (https://git.io/vb49g) to the 'plugins' section of your Babel config to enable transformation.
I added plugin-transform-flow-strip-types as a dev dependency but that does not help.
below is my package.json
{
"name": "ReactNativeWeb",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"web": "react-scripts start",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"#react-native-community/masked-view": "^0.1.10",
"#react-navigation/native": "^5.7.6",
"axios": "^0.20.0",
"babel-loader": "^8.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"qs": "^6.9.4",
"react": "16.13.1",
"react-dom": "^16.13.1",
"react-native": "0.63.2",
"react-native-gesture-handler": "^1.8.0",
"react-native-reanimated": "^1.13.1",
"react-native-safe-area-context": "^3.1.8",
"react-native-screens": "^2.11.0",
"react-native-vector-icons": "^7.1.0",
"react-native-web": "^0.13.12"
},
"devDependencies": {
"#babel/core": "^7.11.6",
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/plugin-transform-flow-strip-types": "^7.10.4",
"#babel/preset-env": "^7.11.5",
"#babel/preset-react": "^7.10.4",
"#babel/runtime": "^7.11.2",
"#react-native-community/eslint-config": "^2.0.0",
"metro-react-native-babel-preset": "^0.63.0",
"react-scripts": "^3.4.3",
"react-test-renderer": "16.13.1",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
},
"jest": {
"preset": "react-native"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
babel.config.js is as follows:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
could anyone tell me how to add the plugin inside babel.config.js?or is there any other way to resolve the above error?
Any suggestions would be great and let me know if any other detail is required for better understanding.
You could add it inside your babel config file. Include it in your plugins.
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: ['#babel/plugin-transform-flow-strip-types'],
};
And install it with npm like this
npm install --save-dev #babel/plugin-transform-flow-strip-types
Check the docs here -> https://babeljs.io/docs/en/babel-plugin-transform-flow-strip-types
Apparently this "helpful" message is the default error message when "neither Flow nor Typescript are enabled in Babel".
Personally I came across it when using a Typescript project and attempting to import something from within a monorepo but outside the current scope - it's then throwing this error when it encounters the TS keyword "interface".
Fixing this will probably depend on your own project setup - however, if you're not using Flow it's unlikely that adding any of the recommended Babel/flow plugins will help.
Add plugin plugin-transform-flow-strip-types in file babel.config.js and enable allowDeclareFields
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
...
['#babel/plugin-transform-flow-strip-types', { allowDeclareFields: true }]
],
};
For install plugin:
npm install --save-dev #babel/plugin-transform-flow-strip-types
//or
yarn add --dev #babel/plugin-transform-flow-strip-types

React Native tests failing, missing babel transforms

I want to test a React Native application that I've created based on the default guides, so it's very simple and has not had any changes w.r.t. the initial test configuration. The only difference seems to be that it's using npm instead of yarn (so it has a package-lock.json).
Here is the package.json:
{
"name": "foo",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-gesture-handler": "^1.5.2",
"react-navigation": "^4.0.10",
"react-navigation-stack": "^1.10.3"
},
"devDependencies": {
"#babel/core": "^7.7.4",
"#babel/runtime": "^7.7.4",
"#react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.9.0",
"eslint": "^6.7.1",
"jest": "^24.9.0",
"license-checker": "^25.0.1",
"metro-react-native-babel-preset": "^0.57.0",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native"
}
}
The test file __tests__/App-test.js is very simple (and auto-generated):
import 'react-native';
import React from 'react';
import App from '../App';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
it('renders correctly', () => {
renderer.create(<App />);
});
I get this error:
➜ npm test
> jest
FAIL __tests__/App-test.js
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/node_modules/#foo/bar/src/main.js:24
export function milliseconds(timeoutInMilliseconds) {
^^^^^^
SyntaxError: Unexpected token 'export'
Now, I don't really understand why it wouldn't work "out of the box". Other guides mention the same steps, too.
Adding a .babelrc with:
{
"presets": ["react-native"]
}
produces the error:
Cannot find module 'babel-preset-react-native' from '/'
- If you want to resolve "react-native", use "module:react-native"
at Function.module.exports [as sync] (node_modules/resolve/lib/sync.js:74:15)
at resolveStandardizedName (node_modules/#babel/core/lib/config/files/plugins.js:101:31)
at resolvePreset (node_modules/#babel/core/lib/config/files/plugins.js:58:10)
at loadPreset (node_modules/#babel/core/lib/config/files/plugins.js:77:20)
at createDescriptor (node_modules/#babel/core/lib/config/config-descriptors.js:154:9)
at node_modules/#babel/core/lib/config/config-descriptors.js:109:50
at Array.map (<anonymous>)
at createDescriptors (node_modules/#babel/core/lib/config/config-descriptors.js:109:29)
at createPresetDescriptors (node_modules/#babel/core/lib/config/config-descriptors.js:101:10)
at presets (node_modules/#babel/core/lib/config/config-descriptors.js:47:19)
There is already a babel.config.js file with the following contents (as suggested here):
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
What should I do? I've seen this question but the React App has not been upgraded and is not missing anything, AFAICT. Note that when I create a completely new app with the init script, the tests run fine.
You are using "react-navigation": "^4.0.10" which uses react-native-gesture-handler. Jest does not know about it. You have to mock react-native-gesture-handler and add react-navigation to transformIgnorePatterns or use react-native-jest-mocks.
Some examples of mocking react-native-gesture-handler can be found here: https://github.com/software-mansion/react-native-gesture-handler/issues/344
Add "files": ["jest/setup.js"]," inside package.json. Also transformIgnorePatterns must include react-navigation-stack, #react-native-community, react-native-gesture-handler and react-navigation.
package.json:
"files": [
"jest/setup.js"
],
"jest": {
"preset": "react-native",
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
"setupFiles": ["./jest/setup.js"],
"testPathIgnorePatterns": [
"/node_modules/"
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-navigation-stack|#react-native-community|react-native-gesture-handler|react-navigation|#react-navigation/.*)"
]
}
jest/setup.js:
import { NativeModules as RNNativeModules } from "react-native";
RNNativeModules.UIManager = RNNativeModules.UIManager || {};
RNNativeModules.UIManager.RCTView = RNNativeModules.UIManager.RCTView || {};
RNNativeModules.RNGestureHandlerModule = RNNativeModules.RNGestureHandlerModule || {
State: { BEGAN: "BEGAN", FAILED: "FAILED", ACTIVE: "ACTIVE", END: "END" },
attachGestureHandler: jest.fn(),
createGestureHandler: jest.fn(),
dropGestureHandler: jest.fn(),
updateGestureHandler: jest.fn(),
};
RNNativeModules.PlatformConstants = RNNativeModules.PlatformConstants || {
forceTouchAvailable: false
};
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
Now run jest and it should pass the test. I have created a repo with an example RN 0.61 app using react-navigation that has jest test passing: https://github.com/clytras/RNJestTest
Or you can try react-native-jest-mocks but I haven't tried it with RN 0.61 yet.

Jest: Test suite failed to run (The Expo SDK requires Expo to run)

I’m having a really hard time getting Jest up and running with my RN expo project (so I can play around and learn it).
FAIL screens/HomeScreen.test.js
● Test suite failed to run
The Expo SDK requires Expo to run. It appears the native Expo modules are unavailable and this code is not running on Expo. Visit https://docs.expo.io to learn more about developing an Expo project.
at Object.<anonymous> (node_modules/expo/src/environment/validate.ts:11:9)
at Object.require (node_modules/expo/build/Expo.js:278:1)
FAIL __tests__/App-test.js
● Test suite failed to run
The Expo SDK requires Expo to run. It appears the native Expo modules are unavailable and this code is not running on Expo. Visit https://docs.expo.io to learn more about developing an Expo project.
at Object.<anonymous> (node_modules/expo/src/environment/validate.ts:11:9)
at Object.require (node_modules/expo/build/Expo.js:278:1)
PASS components/__tests__/StyledText-test.js (7.769s)
Test Suites: 2 failed, 1 passed, 3 total
Tests: 1 passed, 1 total
Snapshots: 1 passed, 1 total
Time: 10.477s
Ran all test suites.
package.json
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject",
"test": "jest --watchAll"
},
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|react-clone-referenced-element|expo(nent)?|#expo(nent)?/.*|react-navigation|sentry-expo))"
]
},
"dependencies": {
"#expo/samples": "2.1.1",
"expo": "^32.0.0",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"react-navigation": "^3.0.9"
},
"devDependencies": {
"babel-preset-expo": "^5.0.0",
"jest": "^24.7.1",
"react-test-renderer": "^16.8.6"
},
"private": true
}
.babelrc
{
"presets": ["react-native"]
}
I think your error is in the jest key in your package.json file and your Babel configuration preset is also wrong.
First add "jest-expo": "^32.0.0" to your devDependencies(since you're using SDK 32), then you must change your jest key in your package.json to:
"jest": {
"preset": "jest-expo",
}
Either change your .babelrc preset key to: presets: ['babel-preset-expo']. Or delete it and create a babel.config.js file (recommended) with the following content:
module.exports = api => {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};
If this still doesn't work check out jest-expo project, it should point you in the right direction. Alternatively leave a comment and I'll edit the answer to help you.

react-native cannot read property 'bindings' of null

I tried to run a react-native simulator.
However, this error came out in the simulator:
Failed to load bundle(http://localhost:8081/index.bundle?
platform=ios&dev=true&minify=false)
with error:(/Users/sugawarasyuta/Desktop/albums/index.js:
Cannot read property 'bindings' of null(null))
I double‐checked my code syntax. But I feel like this is not about that. Do you know how to solve this error?
Here are my files:
index.js
import React from 'react';
import { AppRegistry,View } from 'react-native';
import Header from './src/components/header';
import AlbumList from './src/components/AlbumList';
//Create a component
const App = () => (
<View>
<Header headerText= {'albums'} />
<AlbumList />
</View>
);
AppRegistry.registerComponent('albums', () => App );
AlbumList.js
import React,{Component}from 'react';
import {View, Text} from 'react-native';
import axios from 'axios';
class AlbumList extends Component {
componentWillMount(){
axios.get('https://rallycoding.herokuapp.com/api/music_albums')
.then(response => console.log(response));
}
render(){
return(
<View>
<Text>AlbumList!!!</Text>
</View>
);
}
}
export default AlbumList;
These are terminal statements:
/usr/bin/codesign --force --sign - --entitlements /Users/sugawarasyuta/Desktop/albums/ios/build/Build/Intermediates.noindex/albums.build/Debug-iphonesimulator/albums.build/albums.app.xcent --timestamp=none /Users/sugawarasyuta/Desktop/albums/ios/build/Build/Products/Debug-iphonesimulator/albums.app
/Users/sugawarasyuta/Desktop/albums/ios/build/Build/Products/Debug-iphonesimulator/albums.app: resource fork, Finder information, or similar detritus not allowed
Command /usr/bin/codesign failed with exit code 1
** BUILD FAILED **
The following build commands failed:
CodeSign build/Build/Products/Debug-iphonesimulator/albums.app
(1 failure)
Installing build/Build/Products/Debug-iphonesimulator/albums.app
Launching org.reactjs.native.example.albums
org.reactjs.native.example.albums: 8385
This is my package.json. Are those dependencies correct?
{
"name": "albums",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"axios": "^0.18.0",
"babel-plugin-transform-es2015-arrow-functions": "^6.22.0",
"react": "16.3.1",
"react-native": "^0.56.0"
},
"devDependencies": {
"#babel/preset-env": "^7.0.0-beta.53",
"babel-jest": "23.0.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react-native": "4.0.0",
"eslint-config-rallycoding": "^3.2.0",
"jest": "23.1.0",
"react-test-renderer": "16.3.1"
},
"jest": {
"preset": "react-native"
}
}
I upgraded "babel-preset-react-native" from "4.0.0" to "^5.0.1" and another error came out:
error: bundling failed: SyntaxError:
/Users/sugawarasyuta/Desktop/albums/index.js: Unexpected token (11:3)
10 | const App = () => {
> 11 | <Header headerText= {'albums'} />
| ^
12 |
13 | };
at Parser.raise
(/Users/sugawarasyuta/Desktop/albums/node_modules
/#babel/core/node_modules/babylon/lib/index.js:776:15)
at Parser.unexpected
(/Users/sugawarasyuta/Desktop/albums/node_modules/
#babel/core/node_modules/babylon/lib/index.js:2079:16)
at Parser.parseExprAtom (/Users/sugawarasyuta/Desktop/albums/node_modules/#babel/core/node_modules/babylon/lib/index.js:3157:20)
at Parser.parseExprSubscripts (/Users/sugawarasyuta/Desktop/albums/node_modules/#babel/core/node_modules/babylon/lib/index.js:2757:21)
at Parser.parseMaybeUnary (/Users/sugawarasyuta/Desktop/albums/node_modules/#babel/core/node_modules/babylon/lib/index.js:2736:21)
at Parser.parseExprOps (/Users/sugawarasyuta/Desktop/albums/node_modules/#babel/core/node_modules/babylon/lib/index.js:2643:21)
at Parser.parseMaybeConditional (/Users/sugawarasyuta/Desktop/albums/node_modules/#babel/core/node_modules/babylon/lib/index.js:2615:21)
at Parser.parseMaybeAssign (/Users/sugawarasyuta/Desktop/albums/node_modules/#babel/core/node_modules/babylon/lib/index.js:2562:21)
at Parser.parseExpression (/Users/sugawarasyuta/Desktop/albums/node_modules/#babel/core/node_modules/babylon/lib/index.js:2515:21)
at Parser.parseStatementContent (/Users/sugawarasyuta/Desktop/albums/node_modules/#babel/core/node_modules/babylon/lib/index.js:4076:21)
BUNDLE [ios, dev] ../../index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1), failed.
I feel like this is not because of syntax error simply, but cause of dependencies. Do you know how can I fix it?
Without any further information, I suspect you need to update babel-preset-react-native.
When upgrading to 0.56, make sure to bump your
babel-preset-react-native package.json dependency to ^5.0.1 or newer.
https://github.com/facebook/react-native/releases
For React Native 0.57
When upgrading to React Native 0.57, follow these steps to fix this issue:
Upgrade the version of React in the package.json to 16.5
Change the babel-preset dependency from "babel-preset-react-native": "^5" to "metro-react-native-babel-preset": "^0.45.0"
Change the .babelrc configuration to:
{
"presets": ["module:metro-react-native-babel-preset"]
}
Run rm -rf node_modules/ to remove your old modules
Run npm install to get a fresh node_modules/
Run react-native upgrade to upgrade your config files
Run rm -rf android/build/ ios/build/ to get rid of your old compiled apps
Run your app again, it should work!
Source for the first steps
REACT NATIVE 0.57 FRESH INSTALLS iOS ANDROID
I did a fresh install of react native from react-native init projectName
The package.json
{
"name": "JesteeTestee",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "^16.5.2",
"react-native": "0.57.4"
},
devDependencies": {
"babel-jest": "23.6.0",
"jest": "23.6.0",
"metro-react-native-babel-preset": "^0.47.0",
"react-test-renderer": "16.6.0-alpha.8af6728"
},
"jest": {
"preset": "react-native",
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
}
}
}
As you can see it is the latest, current version of RN#0.57.4 and metro-react-native-babel-preset#0.47.0.
Also the default .babelrc
{
"presets": ["module:metro-react-native-babel-preset"]
}
Simply added the following to the Jest part of the package.json;
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
}
Given the hours I spent finding this today, if you are doing a fresh install and hit a snag just delete the project, start again and try this simple step only.
Tested for react-native run-ios and run-android.
FOR REACT-NATIVE VERSION 0.57 OR MORE
On babel.config.js, simply change presets as below
"presets": ["module:metro-react-native-babel-preset"]
This worked for me after trying many different solutions