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

时间:2019-03-04 19:40:41

标签: intern

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

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加载程序继续启动空的测试套件。最终,promise解析并且套件定义继续进行,但是仅在节点执行程序返回之后已经很久之后:

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

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

1 个答案:

答案 0 :(得分:0)

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

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

const testDef = ...;

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