Cannot import modules into detox test file
See original GitHub issueDescription
Detox throws an error when I try to import into my test file from a common JS file.
I have tried a variety of solutions on the internet and haven’t been able to get this working. Currently flying blind without tests so it’s kind of an emergency to get this up again.
This is the only solution I’ve seen related to the specific issue of importing into test specs but I have no idea how to implement it: https://github.com/wix/Detox/issues/1873#issuecomment-854704442
Error:
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/danny/max/rnMaxpower/e2e/tests/login.e2e.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { email } from '../utils';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)
at TestScheduler.scheduleTests (../node_modules/@jest/core/build/TestScheduler.js:317:13)
babel.config.js:
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['react-native-reanimated/plugin', "@babel/plugin-transform-runtime"]
};
e2e/config.json:
{
"maxWorkers": 1,
"testEnvironment": "./environment",
"testRunner": "jest-circus/runner",
"testTimeout": 120000,
"testRegex": "login\\.e2e\\.js$",
"reporters": [
"detox/runners/jest/streamlineReporter"
],
"verbose": true,
"transform": {
"\\.e2e\\.js$": "babel-jest"
},
"testPathIgnorePatterns": [
"<rootDir>/node_modules/"
],
"transformIgnorePatterns": [
"node_modules/(?!(react-native|react-native-.*|react-navigation|react-navigation-.*|@react-navigation|@react-native-community)/)"
]
}
package.json:
{
"name": "rnmaxpower",
"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": {
"@jsamr/counter-style": "^2.0.2",
"@jsamr/react-native-li": "^2.3.1",
"@logrocket/react-native": "^1.4.1",
"@ptomasroos/react-native-multi-slider": "^2.2.2",
"@react-native-async-storage/async-storage": "^1.17.4",
"@react-native-community/masked-view": "^0.1.11",
"@react-native-community/netinfo": "^8.3.0",
"@react-native-community/slider": "^4.2.2",
"@react-navigation/drawer": "^6.4.1",
"@react-navigation/native": "^6.0.10",
"@react-navigation/stack": "^6.2.1",
"@rneui/base": "^4.0.0-rc.3",
"@sayem314/react-native-keep-awake": "^1.1.0",
"amazon-cognito-identity-js": "^5.2.8",
"aws-amplify": "^4.3.22",
"babel-polyfill": "^6.26.0",
"babel-register": "^6.26.0",
"expo-asset": "^8.5.0",
"expo-splash-screen": "^0.15.1",
"jest-circus": "^28.1.0",
"jest-junit": "^13.2.0",
"lottie-react-native": "^5.1.3",
"mixpanel-react-native": "^1.4.1",
"moment": "^2.29.3",
"react": "17.0.2",
"react-compound-timer": "^1.2.0",
"react-native": "0.68.2",
"react-native-device-info": "^8.7.1",
"react-native-gesture-handler": "^2.4.2",
"react-native-idle-timer": "^2.1.7",
"react-native-paper": "^4.12.1",
"react-native-purchases": "^4.5.3",
"react-native-reanimated": "^2.8.0",
"react-native-safe-area-context": "^4.2.5",
"react-native-screens": "^3.13.1",
"react-native-snap-carousel": "^3.9.1",
"react-native-svg": "^12.3.0",
"react-native-vector-icons": "^9.1.0",
"react-native-video": "^5.2.0",
"react-navigation": "^4.4.4",
"react-redux": "^8.0.1",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.4.1",
"rn-sliding-up-panel": "^2.4.5",
"victory-native": "^36.4.0"
},
"devDependencies": {
"@babel/core": "^7.18.6",
"@babel/preset-env": "^7.18.6",
"@babel/runtime": "^7.18.6",
"@react-native-community/eslint-config": "^2.0.0",
"babel-jest": "^28.1.2",
"detox": "^19.7.1",
"eslint": "^7.32.0",
"jest": "^28.1.2",
"metro-react-native-babel-preset": "^0.71.3",
"react-test-renderer": "^18.2.0"
},
"jest": {
"preset": "react-native"
}
}
Your environment
Detox version: 19.7.1 React Native version: 0.68.2 Node version: 18.2.0 Device model: iPhone 12 OS: iOS Test-runner (select one): jest-circus
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:16 (1 by maintainers)
Top Results From Across the Web
How to import modules for use in detox - Stack Overflow
Describe the bug I'm struggling to import external packages to use with Detox test files. My specific use case is that a testID...
Read more >TypeScript Jest: Cannot use import statement outside module
The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure jest in a TypeScript project and run test files...
Read more >Configuring Jest
This option tells Jest that all imported modules in your tests should be mocked automatically. All modules used in your tests will have...
Read more >Dealing With Problems With Building the App & Detox
This page is about issues related to building the app, typically triggered when running detox build (and not detox test, for example).
Read more >React Native development tools - Part 3: Testing tools - Pusher
That means that you need to update the node_modules/detox/src/devices/android/ADB.js file. This file contains all the commands that Detox is executing to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
-_-
What I did: install ts-jest
yarn add --dev ts-jest
created newtsconfig.spec.json
with the same content of tsconfig.json and added"compilerOptions": { "allowJs": true, }
and added this to the config of detox