So I'm trying to write tests on my React Native app with Jest and TypeScript. While using old babel version everything worked fine, but because of some project problems we had to upgrade babel to 7.0.0. After that I couldn't make it work. Any help is appreciated
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".
/home/levan/work/vabaco-dhp-frontend/packages/dhp-mobile-app/node_modules/react-native-calendar-events/index.ios.js:3 import { NativeModules } from 'react-native';
SyntaxError: Unexpected token import
package.json:
"dependencies": {
"react": "16.4.1",
"react-native": "0.56.0",
"react-native-calendar-events": "^1.6.1",
"react-redux": "^5.0.7",
"react-router": "^4.3.1",
"react-router-native": "^4.3.0",
"react-router-redux": "5.0.0-alpha.9",
"redux": "^4.0.0",
"redux-api-middleware": "^2.3.0",
"redux-form": "^7.4.2",
"redux-thunk": "^2.3.0",
"urijs": "^1.19.1"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@types/history": "^4.7.0",
"@types/jest": "^23.3.1",
"@types/react": "^16.4.13",
"@types/react-native": "^0.56.17",
"@types/react-router": "^4.0.30",
"@types/react-router-native": "^4.2.3",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.2",
"babel-preset-react-native": "^5.0.0",
"jest": "^23.5.0",
"react-native-typescript-transformer": "^1.2.10",
"react-test-renderer": "^16.4.2",
"ts-jest": "^23.1.4",
"typescript": "^3.0.3"
},
"jest": {
"preset": "react-native",
"transform": {
"^.+\\.ts?$": "ts-jest",
"^.+\\.tsx?$": "ts-jest",
"^.+\\.js$": "babel-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"moduleNameMapper": {
"^.+\\.(css|less|scss)$": "identity-obj-proxy"
},
"transformIgnorePatterns": [
"/node_modules/(?!(react-native|my-project|react-native-button|react-native-calendar-events)/)"
]
}
babelrc:
{
"presets": ["react-native", ["@babel/preset-env", {"modules": "commonjs"}]],
"env": {
"test": {
"presets": ["react-native", ["@babel/preset-env"]]
}
}
}
Found the solution. In transform you should use react-native/jest/preprocessor.js instead of babel-jest.
"jest": {
"preset": "react-native",
"moduleDirectories": [
"node_modules",
"src"
],
"transform": {
"^.+\\.ts?$": "ts-jest",
"^.+\\.tsx?$": "ts-jest",
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" <--- over here
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"moduleNameMapper": {
"^.+\\.(css|less|scss)$": "identity-obj-proxy"
},
"transformIgnorePatterns": []
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With