我有一名工作人员正在使用Mocha进行测试,但即使我从诺言解决方案中获得了适当的响应,但由于超时,Mocha一直未能通过测试。关键的事实是,它在摩卡超时后解决。摩卡超时后Promise解决
import chai, { expect } from 'chai';
// Needed otherwise Worker is undefined in the test environment
const Worker = require('webworker-threads').Worker;
describe('WebWorker',() => {
it('should return correctly',() => {
return new Promise(res => {
const dummyWorker = new Worker('./public/js/worker.bundle.js');
dummyWorker.onmessage = e => {
console.log('test message'); // 'test message' prints to testing log
res(e.data);
};
dummyWorker.postMessage(['foo', 'bar']);
})
.then(workerData => {
console.log(workerData[0]); // 'foo' prints to testing log
expect(workerData[0]).to.equal('foo');
})
.catch(err => console.error(err));
});
});
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我已经试过每一些使用done()
,使用before()
挂钩的排列,采用柴作为许诺的,使用的承诺和摩卡的完成,使用this.timeout(5000)
增加超时,就和上,但似乎没有任何工作。
您应该延长超时摩卡。将'it'箭头函数重写为常规函数并添加为第一行this.timeout(5000)'。这会让摩卡在抱怨之前至少等待5秒钟。 – MarcoL
@MarcoL嗨,感谢您的建议。我也尝试过,但忘记将它列入我试过的事情列表中。我现在更新列表。 – mythereal
你正在返回一个承诺,所以摩卡应妥善等待解决。这强烈暗示'dummyWorker.onmessage(...)'永远不会被调用。你可以发布'。/ public/js/worker.bundle.js'在做什么吗? –