基于异步数据源动态创建 theIntern.io 个测试

Dynamically creating theIntern.io tests based on asynchronous data source

我想读取远程数据源(returns 结果的承诺),然后使用该结果来定义一组测试。

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);
    });
  });
})();

问题是异步 IIFE 完成,Intern 加载器继续启动一组空的测试。最终,承诺得到解决,套件定义继续进行,但只有在节点执行者返回后才可以:

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

在 intern.configure 中是否有事件 ("preRun") 或挂钩,或者在加载程序认为我完成定义测试之前使用插件等待 getCaseIds() 调用的方法?

您可以在 beforeRun 事件处理程序中执行操作,例如

const { suite, test, before } = intern.getInterface('tdd');

const testDef = ...;

intern.on('beforeRun', () => {
  return getCaseIds()
    .then(caseIds => {
      suite(...);
    });
});