Is it possible to tell nodeunit not to complete a specific test before calling test.done ()?

I am doing some asynchronous checks using nodeunit and I was wondering if it is possible to tell nodeunit not to interrupt the test cases until the test.done call is called.

This is basically what my test cases look like:

exports.basic = testCase({ setUp: function (callback) { this.ws = new WrappedServer(); this.ws.run(PORT); callback(); }, tearDown: function (callback) { callback(); }, testFoo: function(test) { var socket = ioClient.connect(URL); socket.emit('PING', 1, 1); socket.on('PONG', function() { // do some assertion of course test.done(); }); } }); 

Now the problem is that the PONG is not sent back fast enough for the test code to be executed. Any ideas?

+4
source share
4 answers

I had a very similar problem, so I was looking through this question. In my case, a server (similar to your WrappedServer) threw an exception, causing the test to exit abruptly without hitting the event handler with test.done (). I think it's pretty rude of nodeunit to catch an exception without peeping.

I had to resort to a debugger to find a problem that, if you havenโ€™t done it yet, I can save your web search: node --debug-brk node_modules / nodeunit / bin / nodeunit your_nodeunit_test.js

+1
source

The problem is that nodeunit is not expect any statements, so it does not wait for them and immediately exits. Count your statements and call test.expect() at the beginning of the test.

 exports.example = function(test) { // If you delete next line, the test will terminate immediately with failure. test.expect(1); setTimeout(function(){ test.ok(true); test.done(); }, 5000); }; 
+1
source

When nodeunit says โ€œUndone testsโ€, this means that the node process exited without completing all the tests. To be clear, this does not mean that "PONG is not sent fast enough", which means that there were no more handlers in the event loop. If there are no more handlers, then the PONG event does not exist, so the test is not possible.

For example, if you ran something like this:

 var s = require('http').createServer(); s.listen(80) 

When listen starts, the server starts listening for incoming data and is added to the event loop to check incoming connections. If you just created createServer, the events will not fire and your program will simply exit.

Do you have anything related to the error event somewhere that could lead to errors?

0
source

You probably want something like:

 /** Invoke a child function safely and prevent nodeunit from swallowing errors */ var safe = function(test, x) { try { x(test); } catch(ex) { console.log(ex); console.log(ex.stack); test.ok(false, 'Error invoking async code'); test.done(); } }; exports.testSomething = function(test){ test.expect(1); // Prevent early exit safe(test, function(test) { // ... some async code here }); }; 
0
source

Source: https://habr.com/ru/post/1397525/


All Articles