Search code examples
intern

Dynamically creating theIntern.io tests based on asynchronous data source


I want to read a remote data source (that returns the promise of a result), then use that result to define a collection of tests.

const { suite, test, before } = intern.getInterface('tdd');
const testDef = (theTest) => {
  const searchString = theTest.name;
  return theTest.remote
    .get('http://www.google.com?q=' + searchString )
    .findDisplayedByXpath(`//input[@name='q' and @value='${searchString }']`)
    .end();
};
(async () => {
  const caseIds = await getCaseIds(); // a promise that resolves to an array of ids
  suite('a suite of tests', function (theSuite) {
    caseIds.forEach((id) => {
      const testName = id;
      test(testName , testDef);
    });
  });
})();

The problem is that the async IIFE completes and theIntern loader proceeds to start an empty suite of tests. Eventually, the promise resolves and the suite definition proceeds, but only after the node executor haslong since returned:

No unit test coverage for chrome 69.0.3497.81 on Windows NT
chrome 69.0.3497.81 on Windows NT: 0 passed, 0 failed
TOTAL: tested 1 platforms, 0 passed, 0 failed

Is there an event ("preRun") or a hook in intern.configure or a way to use a plugin to await the getCaseIds() call before the loader thinks I'm done defining tests?


Solution

  • You can take action in a beforeRun event handler, like

    const { suite, test, before } = intern.getInterface('tdd');
    
    const testDef = ...;
    
    intern.on('beforeRun', () => {
      return getCaseIds()
        .then(caseIds => {
          suite(...);
        });
    });