Detox: Detox v20 breaks Cucumber tests integration

Description

Background: Hi guys! We are still relatively new to Detox, and I will try to explain our situation as much as I can. So please let me know if you need any additional details.

We are using Detox v19 currently with a Cucumber 8.4.0 setup as the test runner. We execute cucumber.js to kick off our test suite, and initiate detox.init as part of from Cucumber function beforeAll.

Here is an example of how it looks like in e2e/features/support/init.ts: 2022-11-23_11-57-49

Command to run the test: DETOX_CONFIGURATION=android:30:release:produs DETOX_RECORD_VIDEOS=failing node_modules/.bin/cucumber-js $1 --tags "$test" --require-module ts-node/register --require 'e2e/features/**/*.ts'

Issue: Since detox.init no longer exists in v20, it is no longer able to initiate detoxConfig and launch an emulators / simulators.

Here is what the adapter.js looks like: 2022-11-23_12-00-03

And detox.config.js: 2022-11-23_12-14-30

Is there any new way to initiate Detox as part of init.ts?

Your environment

Detox version: 19.13.0 React Native version: 0.69.6 Node version: 16.13.0 Device model: All simulators / emulators OS: Android & iOS Test-runner (select one): Cucumber

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (9 by maintainers)

Most upvoted comments

so to conclude and answer the OP question

  1. Replace import detox from 'detox' with import detox from 'detox/internals' then we can use for example await detox.init() in cucumber BeforeAll and Before function callback
  2. For screenshot and video, you can place
 await detox.onTestStart({
     title: 'test',
     fullName: 'test',
     status: 'running',
 })

Right after await detox.init()

Here is what your cucumber init file might looks like

import { BeforeAll, AfterAll } from '@cucumber/cucumber'
import detox from 'detox/internals'

BeforeAll({ timeout: 120 * 1000 }, async () => {

    await detox.init()
    await detox.onTestStart({
        title: 'test',
        fullName: 'test',
        status: 'running',
    })
    await device.launchApp()

})

AfterAll(async () => {
    await detox.onTestDone({
        title: 'test',
        fullName: 'test',
        status: 'failed',
    })
    await detox.cleanup()
})

Hey @jpoisch,

Thanks for this report!

Detox v20 has indeed came about with numerous breaking changes. We’ve actually changed mindset such that for these type of cases, it would make it easier to integrate Detox if we were to publicly introduce some internal API’s. Please try to require them from detox/internal and read about it in our documents.

Thanks

The improved version to catch the real status would look like this(Please read the documentation here Cucumber-JS API Reference)

import { Before, BeforeAll, AfterAll, After,  ITestCaseHookParameter } from '@cucumber/cucumber'
import detox from 'detox/internals'

BeforeAll({ timeout: 120 * 1000 }, async () => {
    await detox.init()
    await device.launchApp()
})

Before(async (message: ITestCaseHookParameter) => {
    const { pickle } = message
    await detox.onTestStart({
        title: pickle.uri,
        fullName: pickle.name,
        status: 'running',
    })
})
After(async (message: ITestCaseHookParameter) => {
    const { pickle, result } = message
    await detox.onTestDone({
        title: pickle.uri,
        fullName: pickle.name,
        status: result ? 'passed' : 'failed',
    })
})

AfterAll(async () => {    
    await detox.cleanup()
})

Please let me know if you have any questions. I am happy to help. @noomorph

@pacozaa, can you tell when status: 'failed' and when it is not? I see that you use a hardcoded value in your AfterAll hook. 🤔

I didn’t mean to lie haha.

Take it easy. 😃 And make sure it to share your work with the community. I’m enthusiastic to see external integrations with other test runners.

@pacozaa, I don’t have something specific as examples, here’s how we use it here for Jest:

https://github.com/wix/Detox/blob/master/detox/runners/jest/testEnvironment/listeners/DetoxCoreListener.js#L117

The docs contain sample payloads for these function calls too, please pay attention:

image

All good now, I put them in cucumber AfterAll and BeforeAll callback function

@pacozaa as for your trace file provided for #3850, I think the problem about artifacts is related to the fact you don’t use the optional lifecycle: https://wix.github.io/Detox/docs/next/api/internals#optional-lifecycle . Make use of onTestStart and onTestDone at the very least, and you’ll see artifacts appearing. Ideally, you should strive to implement all those calls in the optional lifecycle.

@pacozaa this is the most simplistic way to use the Internals API: see the file. I hope that helps.

The more advanced your runner/integration is, the more caveats you’ll find, but the minimal example is what it is.

I was mainly suggesting that cleanup and init should not be imported from detox, in Detox v20. Try to dive into the internal API’s, instead.