对于异步测试和钩子,请确保调用"done()";如果返回承诺,请确保它解决了木偶师和摩卡



我正在尝试使用Mocha和Google Puppeteer测试我的组件。在我的单元测试文件中,I'AM在函数后并关闭浏览器的puppeteer浏览器。运行测试文件时,我会遇到关注错误"在所有"挂钩之前错误:超过2000毫秒的超时。对于异步测试和钩子,请确保"完成()"为" DONE DONE";如果返回诺言,请确保其解决方案。

const puppeteer = require('puppeteer');
const { expect } = require('chai');
const _ = require('lodash');
/* create the global variable by using lodash function */
const globalVariables = _.pick(global, ['browser', 'expect']);
/* configurable options or object for puppeteer */
const opts = {
    headless: false,
    slowMo: 100,
    timeout: 0,
    args: ['--start-maximized', '--window-size=1920,1040'] 
}
/* call the before for puppeteer for execute this code before start testing */
before (async () => {
  global.expect = expect;
  global.browser = await puppeteer.launch(opts);
});
/* call the function after puppeteer done testing */
after ( () => {
  browser.close();
  global.browser = globalVariables.browser;
  global.expect = globalVariables.expect;
});

在您的单元测试用例的根目录内,在保留测试文件的位置,添加mocha.opts文件,然后添加--timeout 50000,该文件将在50000毫秒后将摩卡设置为超时。

现在应用了默认超时,并且由于测试操作未完成,因此您会遇到此错误。

最新更新