4

I have a service class, which 50 other "services" extend. While each service has its own test, I would like to write a test suite for all of the services shared functionality (I have a method thing which every service has to implement).

To test thing, every service also has a function thingConfig which returns an array of configurations thing can run in. I would like to do the following:

describe('service', () => {
  let configs;
  before(async () => configs = await service.thingConfig())

  configs.forEach(config => {
    it(config.name + ' should run thing', () => {
      thing = await service.thing(config);
      expect(thing).to....
    });
  });
})

Is it possible to make this dynamic tests (forEach) based on asynchronous data?

2

2 Answers 2

2

In order to make it work, you have to make some dummy it case.

Look at the following example:

describe('Dummy spec', () => {
    before(async () => {
        const configs = await service.thingConfig();
        describe('Generated spec', () => {
            configs.forEach((config) => {
                it(`Test for config: ${config}`, async () => {
                    const thing = await service.thing(config);
                    expect(thing).to....
                });
            });
        });
    });

    it('Dummy test case, so before is executed', () => assert.strictEqual(1, 1));
});

Assuming that everything went well, you should see results in following form:

  Dummy spec
    √ Dummy test case, so before is executed

  Generated spec
    √ Test for config: test1
    √ Test for config: test2
    √ Test for config: test3
    √ Test for config: test4


  5 passing (3s)
4
  • Is this really the best way? That seems pretty ugly and hacky to me...
    – Ini
    Commented Apr 30, 2020 at 10:47
  • Why not use --delay for a delayed root suite?
    – Ini
    Commented Apr 30, 2020 at 11:12
  • Creating a test suite in beforeAll hook of another suite. I didn't know this would work
    – tiriana
    Commented Oct 8, 2021 at 18:48
  • Unfortunately, if tests are generated such way, they won't be visible when using --grep. --grep Dummy will run only dummy spec. --grep Generated will run nothing. Commented Nov 1, 2022 at 22:06
0

Since this seems to be still an issue in mocha (The delay flag breaks parallel mode and does not play well with TypeScript) I have been using a still hacky, but slightly more structured approach:

function asyncSuite(name: string, setupSuite: () => Promise<Suite>) {
  suite(`Async dummy Suite for ${name}`, function () {
    let suite: Suite;

    suiteSetup(async () => (suite = await setupSuite()));

    test(`Async tests for ${name} loaded`, async function () {
      assert.ok(suite.suites.length || suite.tests.length);
    });
  });
}

Asynchronously added suites are never attached to their parents, as the suite tree is built synchronously by mocha. This approach makes that a bit clearer and forces you to create a suite for your asynchronously added data.

Additionally it tests for added tests and suites, preventing a silent fail when no tests are dynamically added. Usage is fairly simple, although you do have one extra indentation level:

asyncSuite('async tests', async function () {
  const values = await new Promise<string[]>((r) => r(['abcd', 'efg', 'hijk']));

  return suite('Asynchronous Test Suite', function () {
    for (const value of values) {
      test(`${value} is length 4`, () => {
        assert.strictEqual(value.length, 4);
      });
    }
  });
});

This will result in one dummy suite that is used to load the asynchronous dynamic tests in it's setup function and the actual test suite:

example of mocha test output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.