Detox icon indicating copy to clipboard operation
Detox copied to clipboard

Cannot import modules into detox test file

Open DanielWFrancis opened this issue 3 years ago • 7 comments
trafficstars

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

DanielWFrancis avatar Jul 06 '22 20:07 DanielWFrancis

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

const _ = require('lodash');

jonathanmos avatar Jul 10 '22 15:07 jonathanmos

-_-

mohamadmek avatar Jul 24 '22 20:07 mohamadmek

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"
},

mohamadmek avatar Jul 25 '22 11:07 mohamadmek

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back.

Thank you for your contributions!

For more information on bots in this reporsitory, read this discussion.

stale[bot] avatar Sep 08 '22 16:09 stale[bot]

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.

estrattonbailey avatar Sep 09 '22 17:09 estrattonbailey

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.

jpamarohorta avatar Sep 13 '22 15:09 jpamarohorta

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

MuhammadTayyabDev avatar Sep 16 '22 20:09 MuhammadTayyabDev

not working too 😔 I've tried all methods mentioned here

Here's the repo which is just a new RN project. If possible, please someone to help. Thanks!

    "react": "18.1.0",
    "react-native": "0.70.1"
    "@babel/core": "^7.19.3",
    "@testing-library/jest-native": "^5.0.0",
    "@testing-library/react-native": "^11.2.0",
    "@types/jest": "^29.1.1",
    "babel-jest": "^29.1.2",
    "babel-preset-expo": "^9.2.0",
    "detox": "^19.12.5",
    "jest": "^29.1.2",
    "metro-react-native-babel-preset": "^0.72.1",
    "ts-jest": "^29.0.3",
    "typescript": "^4.8.4"

CoSNaYe avatar Oct 04 '22 20:10 CoSNaYe

Same here with TS project and JS tests

Janaka-Steph avatar Nov 05 '22 11:11 Janaka-Steph

@Janaka-Steph basically you need to add a transformer and install ts-jest and typescript in your project:

https://github.com/wix/Detox/blob/next/examples/demo-react-native/e2e/jest.config.js#L11-L13

noomorph avatar Nov 05 '22 11:11 noomorph

@noomorph I tried ts-jest but without success. Can I keep JS tests if I have TS project or TS tests are mandatory?

Janaka-Steph avatar Nov 05 '22 11:11 Janaka-Steph

Yes but you need to configure your typescript project properly

noomorph avatar Nov 05 '22 12:11 noomorph

@noomorph I copied the exact same config and lib versions of /examples/demo-react-native on next branch, and still the same issue.

Janaka-Steph avatar Nov 07 '22 17:11 Janaka-Steph

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
}

huszzsolt avatar Nov 08 '22 16:11 huszzsolt

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back.

Thank you for your contributions!

For more information on bots in this repository, read this discussion.

stale[bot] avatar Dec 11 '22 13:12 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Dec 20 '22 18:12 stale[bot]

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

"detox": "20.1.0",

"react-native": "0.70.2",

"jest": "29.3.1",

owens-ben avatar Jan 20 '23 19:01 owens-ben

@owens-ben I need a reproduction repo. Usually this problem is outside of Detox scope, it is just about configuring Jest transformers.

noomorph avatar Jan 21 '23 13:01 noomorph

I believe this issue is a duplicate of https://github.com/wix/Detox/issues/1873

I was also experiencing the same issue after upgrading to Jest 27/28/29, and resolved it with this suggestion: https://github.com/wix/Detox/issues/1873#issuecomment-1443841477

hanno-jonlay avatar Feb 28 '23 17:02 hanno-jonlay