Error: Timeout of 2000ms exceeded. For async tests and hooks. Unit test with mocha and chai

人盡茶涼 提交于 2019-12-25 09:40:48

问题


While establishing the connection using socket

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done( )" is called; if returning a Promise, ensure it resolves.

Given below is the code reference of the same

beforeEach(function(done) {    
    var socketOptions = {};
    var socket = io.connect("http://localhost:5000", socketOptions);
    socket.on('connect', function () {
         console.log('Connection Established');
         setTimeout(done, 500);
    });
    socket.on('error', function (err) {
        console.log('Connection Error', err);
        setTimeout(done, 500);
    });
});

While checking the error of socket connection, I'm getting the error mentioned below:

Connection Error { Error: xhr poll error
at XHR.Transport.onError (F:\Projects\SDK\dynamicLocationServer\node_modules\engine.io-client\lib\transport.js:64:13)
at Request.<anonymous> (F:\Projects\SDK\dynamicLocationServer\node_modules\engine.io-client\lib\transports\polling-xhr.js:129:10)
at Request.Emitter.emit (F:\Projects\SDK\dynamicLocationServer\node_modules\engine.io-client\node_modules\component-emitter\index.js:133:20)
at Request.onError (F:\Projects\SDK\dynamicLocationServer\node_modules\engine.io-client\lib\transports\polling-xhr.js:307:8)
at Timeout._onTimeout (F:\Projects\SDK\dynamicLocationServer\node_modules\engine.io-client\lib\transports\polling-xhr.js:254:18)
at tryOnTimeout (timers.js:232:11)
at Timer.listOnTimeout (timers.js:202:5) type: 'TransportError', description:400}

回答1:


Issue is resolved by changing the socketOptions variable data like

var socketOptions = {'transports': ['websocket']};

Since the error is on transport error polling, we need to specify the way of transports as options as pass with the socket connection options



来源:https://stackoverflow.com/questions/43451140/error-timeout-of-2000ms-exceeded-for-async-tests-and-hooks-unit-test-with-moc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!