react-nativedetox

Detox - Possible to Force Fail Test?


Is there a way to force a test to fail in Detox? I haven't found anything in the documentation that says this can be the case. I am comparing two IDs that are hidden in an element and screen of my app and if they don't match, I want to fail the test.

if (element.id === screen.id) {
    do
}
else {
    *fail test*
}

it(`should invite User`, async () => {}

Or is it as easy as just throwing an error? Thanks


Solution

  • Detox itself delegates test logic to a test runner.

    Detox delegates the actual JavaScript test-code execution to a dedicated test-runner. It supports the popular Jest and Mocha out of the box.

    If you are using jest as the underlying test runner, you could just use its fail method as follows.

    if (element.id === screen.id) {
        do
    }
    else {
        fail('test fails');
    }
    

    In mocha you could use the following function.

    assert.fail("actual", "expected", "Error message");
    

    We could also abuse the detox high level api to achieve the same (but less readable).

    await waitFor(element(by.id(notVisible.id))).toBeVisible().withTimeout(5);