Detox: Cannot import modules into detox test file

Description

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 19 (2 by maintainers)

Most upvoted comments

@DanielWFrancis if you replace your import with require statements, do the tests work? e.g.

const _ = require('lodash');

This is still an issue. None of the suggested solutions worked.

“detox”: “20.1.0”,

“react-native”: “0.70.2”,

“jest”: “29.3.1”,

What I did: install ts-jest yarn add --dev ts-jest created new tsconfig.spec.json with the same content of tsconfig.json and added "compilerOptions": { "allowJs": true, } and added this to the config of detox

"globals": {
    "ts-jest": {
      "tsconfig": "tsconfig.spec.json"
    }
  },
  "transform": {
    "\\.[jt]sx?$": "ts-jest"
},

What I did: install ts-jest yarn add --dev ts-jest created new tsconfig.spec.json with the same content of tsconfig.json and added "compilerOptions": { "allowJs": true, } and added this to the config of detox

"globals": {
    "ts-jest": {
      "tsconfig": "tsconfig.spec.json"
    }
  },
  "transform": {
    "\\.[jt]sx?$": "ts-jest"
},

Our problem was that even if we had .js tests, we have used ES6 import in the e2e/ testfiles.

The above was almost the solution, however, we just re-used the existing tsconfig.json file, and some change to avoid warnings on the changed configuration of globals. Our e2e/config.json file looks like this:

{
    "maxWorkers": 1,
    "testEnvironment": "./environment",
    "testRunner": "jest-circus/runner",
    "testTimeout": 270000,
    "testRegex": "\\.e2e\\.js$",
    "reporters": [
        "detox/runners/jest/streamlineReporter"
    ],
    "testSequencer": "./testSequencer.js",
    "transform": {
        "\\.[jt]sx?$": [
            "ts-jest",
            {
                "tsconfig": "tsconfig.json"
            }
        ]
    },
    "verbose": true
}

Hello, I’m getting the same issue in a project without Typescript. Any idea on how to tackle it?

EDIT: This only happens from Jest version 28.0.0 onwards. With Jest 27.5.1 the transforms work as expected.

Just bumping this so it doesn’t get closed. Seeing the same issue on Detox 19.12.1, Jest 28.1.3, ts-jest 28.0.8.

Hello everyone, I recently encountered a similar problem and I was missing a detox preset "preset": "ts-jest", in e2e/config.json . If you don’t already have it, I recommend giving it a shot and seeing if it works for you as well.

Here is the reference link