Search code examples
react-nativejestjsdetox

Jest tests broken after implementing detox


I'd like to be able to run my detox tests and my Jest unit tests separately. For example, run detox tests with detox build && detox test, and my Jest unit tests with npm test.

After implementing detox (using mocha as the test runner), running npm test results in immediate error, and looks like its trying to run my detox tests (not what I'd expect)! Here's the first error I get.

FAIL e2e/auth.spec.js

Not sure why its trying to run detox tests, when my package.json is pointing the test script to Jest. "scripts": { "start": "node node_modules/react-native/local-cli/cli.js start", "test": "jest" }

How do I run my jest tests now?


Solution

  • By default jest runs all files in your project directory, that have the .test. or .spec. extension to them. That's why it picks up your detox test files and fails to execute them.

    https://facebook.github.io/jest/docs/en/configuration.html#testmatch-array-string

    You have to override this default behavior in order for the two not to clash. Here's what we use in our package.json just for reference, you might want to change it:

    "jest": {
      "testMatch": [
        "<rootDir>/__tests__/**/*.test.js?(x)",
        "<rootDir>/src/**/*.test.js"
      ]
    }