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

问题描述 投票:0回答:1

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

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完成并且theIntern loader继续启动一组空的测试。最终,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()调用的方法?

intern
1个回答
0
投票

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

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

const testDef = ...;

intern.on('beforeRun', () => {
  return getCaseIds()
    .then(caseIds => {
      suite(...);
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.