Detox icon indicating copy to clipboard operation
Detox copied to clipboard

Possibility to create tags to run test suite that contain specific tag.

Open ErickMaeda opened this issue 2 years ago • 6 comments

Describe your idea

Today it's possible to run suite tests by just passing the path e2e/my-test/login, however, I wanted to be able to tag my test suite like we have in Maestro

https://maestro.mobile.dev/cli/tags

For example, sometimes I wanted to run tests for Pull Requests, which is just some specific tests, I also would like to have the nightly tag to run by cron all the tests.

I know I can create a naming convention for the folders like e2e/nightly. However some tests I would like to have 2 or more tags, to be more flexible.

Example

  • detox test --c myconfig --tag pull-request
  • detox test --c myconfig --tag pull-request --tag another-tag
  • detox test --c myconfig --tag nightly

ErickMaeda avatar Sep 22 '23 09:09 ErickMaeda

Hi, this is possible via a custom Detox test environment.

@andy-zy, would you like to share your experience, maybe?

noomorph avatar Oct 02 '23 08:10 noomorph

We added testEnvironment.ts script, which defines whether the test should skip or not, based on the environment variable.

const { DetoxCircusEnvironment } = require('detox/runners/jest');

export default class CustomDetoxEnvironment extends DetoxCircusEnvironment {
  constructor(config, context) {
    super(config, context);

    this.registerListeners({ SkipTestListener });
  }
}
  
class SkipTestListener {
  _cachedNames = new Map();

  add_test(event, state) {
    const currentTest = _.last(state.currentDescribeBlock.children);
    const fullName = this._getFullName(currentTest);

    if (!this._shouldRunTest(fullName)) {
      currentTest.mode = 'skip';
    }
  }

  _getFullName(testEntryOrDescribeBlock) {
    if (!this._cachedNames.has(testEntryOrDescribeBlock)) {
      if (!testEntryOrDescribeBlock.parent) {
        return '';
      }

      const parentName = this._getFullName(testEntryOrDescribeBlock.parent);
      const name = testEntryOrDescribeBlock.name;
      const fullName = parentName ? `${parentName} ${name}` : name;
      this._cachedNames.set(testEntryOrDescribeBlock, fullName);
    }

    return this._cachedNames.get(testEntryOrDescribeBlock);
  }

  _shouldRunTest(testName: string): boolean {
    const tagsToRun = process.env['TAGS_TO_RUN'];

    if (!tagsToRun) {
      return true;
    }

    return tagsToRun.toLowerCase().split(/\s*,\s*/).some(tag => hasTag(testName, tag));
  }
}

function hasTag(name, tag) {
  return name.toLowerCase().includes(`[${tag}]`);
}

so it runs only the tests that contain in the description the tags that are listed in TAGS_TO_RUN variable. Here is an example of the test.

it('[SOME_TAG] Test to run', () => { ... });


it('Test to skip', () => { ... });

Example of the env var TAGS_TO_RUN=SOME_TAG ANOTHER_TAG

andy-zy avatar Oct 03 '23 08:10 andy-zy

@andy-zy, I updated your snippet and restored some missing parts. Thanks for your response!

noomorph avatar Oct 10 '23 15:10 noomorph

@ErickMaeda, please examine the snippet. I hope it answers your question.

noomorph avatar Oct 10 '23 16:10 noomorph

We are using jest -t cli option and it's working fine (skips tests that are not tagged by specific tag). it('Does a smoke test @smoke-test', async () => { detox test -c android_12 -t @smoke-test

mantasnakt avatar Oct 25 '23 07:10 mantasnakt

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back.

Thank you for your contributions!

For more information on bots in this repository, read this discussion.

stale[bot] avatar Dec 15 '23 04:12 stale[bot]