Search code examples
javascriptreact-nativee2e-testingdetox

detox ReferenceError: before is not defined


I am using the Detox Test tool, and I am having difficulties.

I only installed Detox, I only ran the basic code for the ios test, and I get the following error:

Please help me.

Just iOS

Error Log

$ detox test --configuration ios.sim.debug --debug-synchronization --take-screenshots all --record-videos nonex --record-logs all
node_modules/.bin/jest e2e --config=e2e/config.json --maxWorkers=1 --testNamePattern='^((?!:android:).)*$'
 FAIL  e2e/firstTest.spec.js
  ● Test suite failed to run

    ReferenceError: before is not defined

      3 | const adapter = require('detox/runners/mocha/adapter');
      4 | 
    > 5 | before(async () => {
        | ^
      6 |   await detox.init(config);
      7 | });
      8 | 

      at Object.<anonymous> (init.js:5:1)

package.json

"script":{
   "e2e:ios": "detox test --configuration ios.sim.debug --debug-synchronization --take-screenshots all --record-videos nonex --record-logs all",
   "e2e:android": "detox test --configuration android.emu.debug --loglevel verbose --take-screenshots all --record-videos none --record-logs all"
        },
   dependencies": {
        "detox": "^8.0.0",
        "jest": "^23.1.0",
        "mocha": "^5.2.0",
    },
    "detox": {
        "configurations": {
          "ios.sim.debug": {
            "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/{app_name[enter image description here][1]}.app",
            "build": "xcodebuild -workspace ios/{workspace_Name}.xcworkspace -scheme {scheme_name} Dev -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build",
            "type": "ios.simulator",
            "name": "iPhone 7"
          },
          "android.emu.debug": {
            "binaryPath": "android/app/build/outputs/apk/dev/debug/{apk_name}.apk",
            "build": "react-native run-android --variant=devDebug --appId com.noahclient.dev",
            "type": "android.emulator",
            "name": "Nexus_5X_API_26"
          }
        },
        "test-runner": "jest"
      }

}


Solution

  • I looks like you are trying to run a mocha test on the jest runner. As your init.js is setup for mocha but the test runner that you are using is jest. This is confirmed by the error message node_modules/.bin/jest e2e... that you are getting.

    You should pick either one, jest or mocha and use it. Rather than trying to use both.

    #Jest If you are using jest your init.js should look like this:

    const detox = require('detox');
    const config = require('../package.json').detox;
    const adapter = require('detox/runners/jest/adapter');
    
    jest.setTimeout(120000);
    jasmine.getEnv().addReporter(adapter);
    
    beforeAll(async () => {
      await detox.init(config);
    });
    
    beforeEach(async () => {
      await adapter.beforeEach();
    });
    
    afterAll(async () => {
      await adapter.afterAll();
      await detox.cleanup();
    });
    

    and you should add "test-runner": "jest" to the detox object in your package.json.

    You should also have a config.json file in the same location as the init.js containing:

    {
      "setupFilesAfterEnv" : ["./init.js"]
    }
    

    #Mocha If you are using mocha then your init.js should look like this:

    const detox = require('detox');
    const config = require('../package.json').detox;
    const adapter = require('detox/runners/mocha/adapter');
    
    before(async () => {
      await detox.init(config);
    });
    
    beforeEach(async function () {
      await adapter.beforeEach(this);
    });
    
    afterEach(async function () {
      await adapter.afterEach(this);
    });
    
    after(async () => {
      await detox.cleanup();
    });
    

    and you should remove the "test-runner": "jest" from the detox object in your package.json as it is not required.

    Instead of a config.json file you should have a mocha.opts file beside your init.js and it should have something similar to:

    --recursive
    --timeout 120000
    --bail
    

    #Next steps

    1. Choose the test runner that you are wanting to run; either jest or mocha.
    2. Make sure you have the correct init.js file for the test runner.
    3. If using jest have a config.json file and add the test-runner to the detox object in the package.json.
    4. If using mocha have a mocha.opts file. No need to specify a test-runner in the detox object in the package.json.

    You can see the setup instructions here: https://github.com/wix/detox/blob/master/docs/Introduction.GettingStarted.md#step-3-create-your-first-test

    If you are still having issues let me know.