When I run "npm test" locally, my tests all run fine without issue. However, when I try to run my tests on CircleCI, I am getting the error...
import React from 'react';
^^^^^
SyntaxError: Unexpected identifier
at Runtime._execModule (node_modules/jest-runtime/build/index.js:988:58)
I am not sure where the discrepancy between the two lies. Here is my package.json
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject",
"test": "jest"
},
"dependencies": {
"#aws-amplify/api": "^1.2.4",
"#aws-amplify/pubsub": "^1.2.4",
"aws-amplify": "^2.2.2",
"aws-amplify-react": "^2.5.4",
"aws-amplify-react-native": "^3.2.0",
"expo": "^35.0.0",
"react": "16.8.3",
"react-native": "https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz",
"react-native-gesture-handler": "~1.3.0",
"react-native-modal-dropdown": "^0.7.0",
"react-native-reanimated": "~1.2.0",
"react-native-screens": "^2.0.0-alpha.32",
"react-native-svg": "9.9.5",
"react-native-vector-icons": "^6.6.0",
"react-native-web": "^0.11.7",
"react-navigation": "^4.0.10",
"react-navigation-stack": "^1.10.3",
"react-navigation-tabs": "^2.6.0",
"victory-native": "^33.0.0"
},
"devDependencies": {
"babel-preset-expo": "^7.1.0",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-react-16-adapter-setup": "^0.1.0",
"enzyme-to-json": "^3.4.3",
"jest": "^24.9.0",
"jest-enzyme": "^7.1.2",
"react-dom": "^16.8.3"
},
"private": true,
"jest": {
"preset": "react-native",
"collectCoverage": true,
"moduleDirectories": [
"node_modules",
"src"
],
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
"setupFiles": [
"<rootDir>/jest/setup.js"
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|victory-.*)"
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"/jest"
]
}
}
and here is my CircleCI .yml file...
version: 2.2
jobs:
build:
docker:
- image: circleci/node:10.15
working_directory: ~/repo/my-app
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v2.2-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v2.2-dependencies-
- run: npm install
- save_cache:
paths:
- node_modules
key: v2.2-dependencies-{{ checksum "package.json" }}
# run tests!
- run: npm test
It seems like because I "npm install", all React and Jest elements should work fine. Any ideas on if my configuration seems off?
I see two things that might be the cause of the problem, but take this comment more as a hint rather than a solution.
First, you must be sure to run the same node version as circleci (10.15). You can achieve that with nvm (https://github.com/nvm-sh/nvm).
Then, you should cache package-lock.json as it contains a more fine graned representation of your project dependencies.
Finally remove node_modules/ and install again the dependencies.
Related
I'm trying to get Jest working with the TypeORM Expo-Example project
I've done a lot to try to get either ts-jest as well as expo-jest working. I have followed official guides for both Jest and Expo Jest. I was able to expo init a new empty project (not the TypeORM example) and get Expo-Jest working as expected, but when I to apply the changes to the TypeORM expo project, it ran into issues.
I think expo-jest is preferred. It will probably be easier to start from a clean clone of the TypeORM Expo-Example repo.
I've edited the package.json to a version that shouldn't have dependency issues if it is an easier starting point. (the jest stuff can be modified or removed in favor of separate config)
{
"name": "expo-example",
"version": "0.1.0",
"private": true,
"devDependencies": {
"#babel/core": "^7.18.0",
"#babel/preset-env": "^7.18.0",
"#types/jest": "^27.5.1",
"#types/node": "^10.1.3",
"#types/react": "~17.0.21",
"#types/react-native": "~0.67.6",
"#types/react-test-renderer": "^16.0.1",
"babel-plugin-transform-typescript-metadata": "^0.3.2",
"jest": "27",
"jest-expo": "^45.0.1",
"react-native-typescript-transformer": "^1.2.9",
"react-test-renderer": "17",
"typescript": "^4.6.4"
},
"scripts": {
"start": "expo start --dev-client",
"eject": "expo eject",
"android": "expo run:android",
"ios": "expo run:ios",
"test": "jest",
"postinstall": "patch-package"
},
"jest": {
"preset": "jest-expo",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
"\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"testPathIgnorePatterns": [
"\\.snap$",
"<rootDir>/node_modules/"
],
"cacheDirectory": ".jest/cache",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|#react-native(-community)?)|expo(nent)?|#expo(nent)?/.*|#expo-google-fonts/.*|react-navigation|#react-navigation/.*|#unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
]
},
"dependencies": {
"babel-jest": "27",
"expo": "^45.0.0",
"expo-splash-screen": "~0.15.1",
"expo-sqlite": "~10.2.0",
"expo-status-bar": "~1.3.0",
"patch-package": "^6.4.7",
"postinstall-postinstall": "^2.1.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-native": "0.68.1",
"react-native-sqlite-storage": "^4.1.0",
"react-native-web": "0.17.7",
"reflect-metadata": "^0.1.13",
"ts-jest": "27",
"tslib": "^1.9.1",
"typeorm": "^0.3.6"
}
}
I have the same issue, my problem is an error when i try test the return of getConnection() typeorm function or DataSource({...}).initialize() with expo-sqlite driver... to make it work i use slite3 lib instead expo-sqlite... so it work, see: https://github.com/EmanuelVogt/expo-sqlite-typeorm-jest
I've written some tests with Jest in my React Native expo app. When I run the tests they pass but I also always get this notification:
Expected react-native/jest-preset to define transform[^.+\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$]
react-native/jest-preset contained different transformIgnorePatterns than expected
this is my package.json file:
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject",
"test": "jest"
},
"jest": {
"preset": "jest-expo",
"verbose": true,
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-clone-referenced-element|#react-native-community|expo(nent)?|#expo(nent)?/.*|react-navigation|#react-navigation/.*|#unimodules/.*|unimodules|sentry-expo|native-base|#sentry/.*)"
]
},
"dependencies": {
"#react-native-community/masked-view": "0.1.6",
"#react-navigation/bottom-tabs": "^5.6.1",
"#react-navigation/drawer": "^5.8.4",
"#react-navigation/native": "^5.6.1",
"#react-navigation/stack": "^5.6.2",
"expo": "~37.0.3",
"expo-font": "^8.1.1",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
"react-native-gesture-handler": "~1.6.0",
"react-native-reanimated": "~1.7.0",
"react-native-safe-area-context": "0.7.3",
"react-native-screens": "~2.2.0",
"react-native-svg": "11.0.1",
"react-native-svg-transformer": "^0.14.3",
"react-native-web": "~0.11.7",
"readme-md-generator": "^1.0.0"
},
"devDependencies": {
"#babel/core": "^7.8.6",
"babel-preset-expo": "~8.1.0",
"jest-expo": "^38.0.2",
"react-native-testing-library": "^2.1.1",
"react-test-renderer": "^16.13.1"
},
"private": true
}
This is the jest.preset file:
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* #format
*/
'use strict';
module.exports = {
haste: {
defaultPlatform: 'ios',
platforms: ['android', 'ios', 'native'],
},
transform: {
'^.+\\.(js|ts|tsx)$': 'babel-jest',
'^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$': require.resolve(
'./jest/assetFileTransformer.js',
),
},
transformIgnorePatterns: [
'node_modules/(?!(jest-)?react-native|#react-native-community)',
],
setupFiles: [require.resolve('./jest/setup.js')],
testEnvironment: 'node',
};
As far as I can tell the from the jest preset file the right transform attributes should be in a assetFileTransformer.js file. I have this file and it seems to be in the right place but it can't be found. Any help would be appreciated! Thanks!
When I am running this command:
expo build:android
I am getting following errors:
Unable to resolve module `./index.css` from `node_modules\antd-mobile\lib\button\style\css.js`:
None of these files exist:
* node_modules\antd-mobile\lib\button\style\index.css(.native|.ios.expo.ts|.na
tive.expo.ts|.expo.ts|.ios.expo.tsx|.native.expo.tsx|.expo.tsx|.ios.expo.js|.nat
ive.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios.ts|.native.ts
|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|
.ios.json|.native.json|.json|.ios.wasm|.native.wasm|.wasm)
* node_modules\antd-mobile\lib\button\style\index.css\index(.native|.ios.expo.
ts|.native.expo.ts|.expo.ts|.ios.expo.tsx|.native.expo.tsx|.expo.tsx|.ios.expo.j
s|.native.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios.ts|.nat
ive.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx
|.jsx|.ios.json|.native.json|.json|.ios.wasm|.native.wasm|.wasm)
Packager URL http://127.0.0.1:19001/node_modules\expo\AppEntry.bundle?dev=false&
minify=true&hot=false&platform=ios returned unexpected code 500. Please open you
r project in the Expo app and see if there are any errors. Also scroll up and ma
ke sure there were no errors or warnings when opening your project.
I have already done following:
npm cache verify
expo -r c
package.json file is as given below:
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"#babel/runtime": "^7.7.7",
"antd-mobile": "^2.3.1",
"babel-plugin-import": "^1.13.0",
"expo": "~36.0.0",
"rc-form": "^2.4.11",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz",
"react-native-gesture-handler": "~1.5.0",
"react-native-maps": "0.26.1",
"react-native-reanimated": "~1.4.0",
"react-native-safe-area-context": "0.6.0",
"react-native-web": "~0.11.7",
"react-navigation": "^4.0.10",
"react-navigation-stack": "^2.0.12",
"react-navigation-tabs": "^2.7.0",
"react-redux": "^7.1.3",
"redux": "^4.0.5",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"babel-preset-expo": "~8.0.0",
"#babel/core": "^7.0.0"
},
"private": true
}
babel.config.js is as given below:
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: [
[
"import",{ libraryName: "antd-mobile", style: "css" }
]
]
};
};
I couldn't make Jest snapshot testing work in React native expo app. I am getting all kinds of errors depending on set of node modules and configuration.
Examples are:
* SyntaxError: Unexpected identifier import ... or about import React from another file,
* jest encountered an unexpected token import,
* Errors related to script_transformer .
Here is my package.json
{
"name": "empty-project-template",
"main": "node_modules/expo/AppEntry.js",
"private": true,
"scripts": {
"lint": "eslint .",
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject",
"test": "node_modules/.bin/jest"
},
"dependencies": {
"expo": "^31.0.0",
"moment": "^2.22.2",
"pouchdb-react-native": "^6.4.1",
"prop-types": "^15.6.2",
"react": "16.3.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-31.0.0.tar.gz",
"react-native-easy-toast": "^1.1.0",
"react-native-range-datepicker": "^1.8.2",
"react-navigation": "^2.18.2",
"react-redux": "^5.1.1",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"babel-eslint": "^10.0.1",
"babel-plugin-syntax-class-properties": "^6.13.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"eslint": "^5.9.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-react": "^7.11.1",
"jest-expo": "^31.0.0",
"react-test-renderer": "^16.7.0"
},
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!(pouchdb-react-native|pouchdb-adapter-asyncstorage))"
]
}
}
Can someone point me to a working Repo of a React-native Expo app with Jest snapshot tests.
jest-expo has better snapshots supoport now,
check https://docs.expo.io/versions/latest/guides/testing-with-jest/#snapshot-test
Im using expo.
here's my package.json file
{
"name": "emmunize",
"version": "0.1.0",
"private": true,
"devDependencies": {
"jest-expo": "27.0.0",
"react-native-scripts": "^1.14.1",
"react-test-renderer": "16.3.0-alpha.1"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "jest"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"#expo/vector-icons": "^6.3.1",
"expo": "^27.0.0",
"firebase": "^5.0.2",
"moment": "^2.22.2",
"native-base": "^2.5.2",
"react": "16.3.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-27.0.0.tar.gz",
"react-native-datepicker": "^1.7.2",
"react-native-router-flux": "^4.0.0-beta.32",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"redux-thunk": "^2.2.0"
}
}
I dont know what happened, when I try to exp start and exp android this error appeaars. I've followed the latest exp version 27 https://blog.expo.io/expo-sdk-v27-0-0-is-now-available-898bf1e5b0e4 but I think react-native-router-flux has an issue with the latest expo version?
I had the same problem when i was create a new project. As this is the new beta we should wait to the group can solve the bug.
Provisional Solution
Open terminal
Go to project path... Example: user/Documents/ReactNative/(name your project)/
run the command: npm remove react-native-router-flux
run the command: npm install react-native-router-flux#4.0.0-beta.28
run project again to try
react-native-router-flux#4.0.0-beta.28 is a version that i used in the last project and work fine.