简体   繁体   English

摩卡超时后承诺解决

[英]Promise resolves after Mocha times out

I have a worker I'm testing with Mocha, but even though I'm getting the proper response back from promise resolution, Mocha keeps failing the test due to timeout. 我有一个正在与Mocha进行测试的工作人员,但是即使我从诺言解决方案中获得了正确的响应,但由于超时,Mocha仍使测试失败。 Crucial fact is that it resolves after Mocha times out. 至关重要的事实是,它在Mocha超时后解决。

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. 错误:超时超过2000毫秒。 For async tests and hooks, ensure "done()" is called; 对于异步测试和挂钩,请确保调用了“ done()”; if returning a Promise, ensure it resolves. 如果返回承诺,请确保其解决。

I've tried every some permutations of using done() , using before() hooks, using chai-as-promised, using promises AND Mocha's done, using this.timeout(5000) to increase the timeout, and on and on, but nothing seems to work. 我试着使用一些每次置换done()使用before()挂钩,用柴作为许诺的,使用的承诺和摩卡的完成,使用this.timeout(5000)以增加超时,和和,但似乎没有任何工作。

I have narrowed down the problem to the npm package webworker-threads. 我已将问题缩小到npm软件包webworker-threads。 And what do you know. 而且你知道什么。 It's actually an issue that hasn't been resolved yet. 它实际上是一个问题,即尚未解决。

It's only a problem when promises are involved. 当涉及承诺时,这只是一个问题。 I thought I tried using done() inside of dummyWorker.onmessage , but I was sadly mistaken. 我以为我尝试在dummyWorker.onmessage内部使用done() ,但我被误认为是错误的。 Using done() with webworker-threads works just fine. 与webworker-threads一起使用done()可以正常工作。 The problem with using done() however is that any assertion error only ever returns the timeout message. 但是,使用done()的问题在于,任何断言错误只会返回超时消息。 With promises, the error message is much more specific. 有了promise,错误消息就会更加具体。

Thus, I have switched over to using a different web worker package -- tiny-worker, and it works just fine now. 因此,我改用了另一个Web工作程序包-tiny-worker,它现在可以正常工作了。

In the event you want to see the issue with webworker-threads, refer to the simple example below, which reproduces the error without needing an external file: 如果您想查看Webworker线程的问题,请参考下面的简单示例,该示例无需外部文件即可重现错误:

import { expect } from 'chai';
const Worker = require('webworker-threads').Worker;

describe('web worker', () => {
  it('should resolve', () => {
    const p = new Promise(resolve => {
      const dummyWorker = new Worker(function () {
        this.onmessage = e => {
          self.postMessage('foo');
        };
      });

      dummyWorker.onmessage = e => {
        resolve(e.data);
      };
      dummyWorker.postMessage('foo');
    });

    return p.then(data => {
      console.log(data));
      expect(data).to.equal('bar');
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM