10

Running detox test runs all tests in the suite. Is there a way to run a subset of the tests? A single test file, or a single test case.

I've configured Detox to use Mocha as the test runner, as per the docs. I know that Mocha has a -g option to specify a pattern for the tests to run. But options passed to the detox command don't seem to be passed along to mocha:

# detox test -g mytestcase

error: unknown option `-g'

I'm attempting to use Detox for TDD, and the quicker the feedback I can get, the better. As my test suite grows, it would become less and less motivating to run tests as I develop if the time to run gets longer and longer.

Josh Justice
  • 20,212
  • 3
  • 24
  • 18

4 Answers4

10

Edit: Turns out you don't need the -f flag at all.

You can just do detox test /path/to/yourTest.spec.js and it works for single files (tested on v14.4.1).

Source

I ended up using the -f parameter for testing a specific test.

detox test -f /path/to/yourTest.spec.js

Came across it from the repo. Looks like it may have been updated.

Simon
  • 5,293
  • 6
  • 28
  • 51
5

Preface:
Detox is test runner agnostic, you can actually use which ever test runner you want. The only thing we did is try to make dev life easier by creating detox-cli, which currently only works with mocha and jest, but you can easily extend it to any other runner. While I agree the implementation of detox-cli is not perfect (params are not being passthrough from to test runner, and generally it needs a bit of work), its pretty easy to add whatever you want in detox-test.js.

Actual answer:
In the meantime, the fastest way to to run individual test is by using only to to mark the suites/tests you want to run: describe.only and it.only.

Rotemmiz
  • 7,625
  • 3
  • 32
  • 36
  • 1
    Jest: https://facebook.github.io/jest/docs/en/api.html#describeonlyname-fn Mocha: https://mochajs.org/#exclusive-tests – Rotemmiz Apr 27 '18 at 07:35
2

Consider the following code for running particular suite

describe.only('App screen', () => {
    beforeEach(async () => {
        await device.reloadReactNative();
    });

In the above code "Only" will run the whole test suite.

Consider the following code for running particular test case

it.only('should write into TextInputs', async () => {
            await element(by.id("input")).typeText("Detox Automation POC-Test for enter text and delete text");
            await element(by.id("input")).clearText();
            await element(by.id("input")).replaceText("Bye");

In the above code "Only" will run the particular test case.

Saif Siddiqui
  • 604
  • 1
  • 8
  • 27
  • 1
    This will only run signle describe in one file, but will still run other tests in different file. You need to use it in combination with `detox test /path/to/yourTest.spec.js`. – Townsheriff Aug 21 '20 at 13:54
  • @Townsheriff The question is asked about running an individual detox test and does not mention whether to run in a single file or not so the answer is right don't interpret it to confuse others :) – Saif Siddiqui Aug 22 '20 at 04:51
1

workaround can be helpfull. Did it also for ssh-channel to remote host with detox through ssh:

  1. create another folder for tests (not e2e, f.E. "temp-tests") on the same folder-structure level.
  2. copy all your tests to temp-tests
  3. write small script and start it from "e2e" folder. It will :

    delete all tests in e2e, then will take only one test from "temp-tests" and put it to "e2e" folder and start it.

4 (optional) also one can write saving of mocha-report into another file for each test for reporting issues.

script: //////////////////////////////////////////// rm *.spec.js cd ../run-tests cp unique-testname ../e2e/ detox test --configuration yourapp.initial