2

I'm trying to add detox using jest and jest-circus to my ReactNative app but I'm currently struggling making it work. Detox, jest and jest-circus have been added from scratch at the latest version.

When launching my test, after a successful build, it launches the simulator but hangs at the first test and stops with a timeout and a is assigned to undefined error. It seems like it doesn't find the simulator but it's correctly running and the uninstall / install app process correctly worked too.

Here's the code.

environment.js

const {
  DetoxCircusEnvironment,
  SpecReporter,
  WorkerAssignReporter,
} = require('detox/runners/jest-circus')

class CustomDetoxEnvironment extends DetoxCircusEnvironment {
  constructor(config) {
    super(config)

    // Can be safely removed, if you are content with the default value (=300000ms)
    this.initTimeout = 30000

    // This takes care of generating status logs on a per-spec basis. By default, Jest only reports at file-level.
    // This is strictly optional.
    this.registerListeners({
      SpecReporter,
      WorkerAssignReporter,
    })
  }
}

module.exports = CustomDetoxEnvironment

config.json for jest

{
    "testEnvironment": "./environment",
    "testRunner": "jest-circus/runner",
    "testTimeout": 120000,
    "testRegex": "\\.spec\\.js$",
    "reporters": ["detox/runners/jest/streamlineReporter"],
    "verbose": true
}

.detoxrc.json

{
  "testRunner": "jest",
  "runnerConfig": "test/tdd/config.json",
  "specs": "test/tdd",
  "configurations": {
    "ios.sim.release": {
      "binaryPath": "ios/build/Build/Products/Release-iphonesimulator/BetaSeriesNative.app",
      "build": "export RCT_NO_LAUNCH_PACKAGER=true && xcodebuild -workspace ios/BetaSeriesNative.xcworkspace -UseNewBuildSystem=NO -scheme BetaSeriesNative -configuration Release -sdk iphonesimulator -derivedDataPath ios/build -quiet",
      "type": "ios.simulator",
      "device": {
        "type": "iPhone 8"
      },
      "artifacts": {
        "pathBuilder": "./test/tdd/detox.pathbuilder.ios.js"
      }
    }
  }
}

login.spec.js test

describe('When on the incentive page', () => {
  beforeEach(async () => {
    await device.reloadReactNative()
  })

  it('it should open the login view', async () => {
    await expect(element(by.id('LoginView'))).toBeVisible()
    await expect(element(by.id('LoginView_button'))).toBeVisible()
    await element(by.id('LoginView_button')).tap()

    await expect(element(by.id('LoginView_form'))).toBeVisible()
  })
})

Here's the error.

enter image description here

Thanks!

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Guillaume S.
  • 216
  • 2
  • 7

1 Answers1

1

Ok, it seems like I found where the problem was coming from.

I added await device.disableSynchronization() and removed await device.reloadReactNative() in the beforeEach hook.

I also commented a lot of code in my app and I ended returning null in my first homepage view render.

I tried of course to just return null in my render without adding/removing those lines in my test, but without it, it still doesn't work.

Weird thing though. Sometimes it still hangs when launching the test and I still got the same error as before: is assigned to undefined. And when I relaunch it, sometimes it works like the screenshot below. I'd say it's working now maybe 2 out of 3. Maybe there is still some code in my app that is hanging the test and then times out, so I'll keep looking.

Anyway, I think a better error or maybe a warning that it might come from elsewhere, or the app itself, would be better to understand that kind of error. It's pretty unclear for now to know where the source of the error is coming from, even with debug and verbose enabled.

Hopefully it will help some of you guys. Cheers.

P.S in the screenshot my test still fails because I didn't edit the test, but at least it's running the test alright :)

enter image description here

Guillaume S.
  • 216
  • 2
  • 7
  • I was facing this exact same issue yesterday. I don't have a perfect solution, but I was able to get my tests fully running by switching to Android. I had to change my targetSdkVersion to 29, and I used a Pixel 2 API 27 emulator successfully. My tests didn't run properly with any other API emulator. – user1231745 Jul 23 '20 at 16:01