I'm running my test files with script
"test": "node_modules/.bin/mocha *.test.js
but in the first test I'm sending to my Node.js server while(1); and causing it to timeout, simultaneously failing that test. That's the intended behaviour but how do I make mocha run the rest of the files after that?
There is an issue in Mocha's repository for your case.
Infinite loops seem to escape timeouts #1609
TL:DR
This is not possible in mocha(because of how node works).
You can read the interesting discussion about this problem.
The reason it blocks all the code is because of how node works.
The code is executed line by line totally in syncronous way.
The async magic happens with the node event loop .
But when you run infinite loop the event loop will be blocked because only one event can run at a time, and this event will never end:)
Related
I’ve recently started to figure out what event loop really is and that confused me a lot, seems like I don’t know how nodejs works..
I mean when program starts, gets loaded into memory - what’s next?
I can’t see a place inside event loop where all sync. Code executes (like for/ while cycles that’s computes something).. doesn’t that means that V8 executes JavaScript and starts event loop when needed?
If anybody can help and explain how nodejs runtime is functioning on the high level would be really great
I highly recommend reading this Asynchrony: Now & Later
and I'll quote some things that I've once read.
........
JS Engine know nothing about code being asynchronous,It only execute code at a time and finishes..no more no less
the JS host environment is the one who has an implementation of the event-loop concept where code that doesn't need to run now(in the future),is waiting(imagine a network call/ io call) to finish processing and get called (be added to the event-queue of the event-loop and then executed at a next tick)
At program start,I'm 100% sure but I think all code is added to the event-queue(the way how the event-loop is implemented) and it's processed as First in First out (FIFO) which means the earlier the code the first is executed,and while running if some code need to be stalled like a setTimeout or IO process or an Ajax call(which both need time) it's up to them to use for example a callback to call(here the callback is added to the event-queue) and it's the event-loop responsibility to execute these callback in order that they've reached in at a next future tick.
Our mocha tests suddenly stop with this message on the console:
Cannot find module `pg-native`
No stack trace is shown, mocha doesn't render the normal output for the test. The test immediately stops.
If I disable the test in question, all tests run as normal.
Installing pg-native removes the error, but then mocha just hangs at that point instead.
As per this issue the problem is a result of running something that walks sequelize records in depth.
eg
expect(myObject).to.deep.equal(mySequelizeInstance);
changing to
expect(myObject).to.deep.equal(mySequelizeInstance.toJSON());
will solve it
Why?
There's two reasons for the above behaviour
Sequelize records override native getters and so traversing certain properties causes code to be executed. In this case, one of those properties ends up down a rabbit hole that causes require('pg-native') to be executed (and so the error)
The object contains circular references, and so the code hangs traversing the infinite references. Left long enough it will eventually fail when it exhausts the stack.
I have a pretty big and terribly written piece of code that eventually crashes main Node JS process. Probably there are a lot of memory leaks. I tried fixing it, but it's very bad. (Single letter variables and such.)
Sometimes it crashes in 10 seconds, sometimes after 5 hours, but it crashes.
It is not something mission critical. It is trying to read emails by using IMAP.
I don't want to integrate a queue processor right now. Can I simply create a child instance with Node JS, run this code block in the scope of this? Any correct way of doing it?
You can use .spawn() or .exec() from the child_process module. If you're running a node.js script, then the program you are running is node and you specify the script you want to run as the first argument and it will run in another process and any other arguments to the script as subsequent arguments.
You just separate out the troublesome code into it's own node.js script and then you can run it this way.
If you want to understand more about the difference between spawn and exec, this is a good article on that.
Is there a way to execute a piece of code in Node.js Express just before the node.js process exits, regardless whether it was due to an error being thrown, or pressing Ctrl+C, or any other reason?
You're looking for the exit event. From the documentation:
Emitted when the process is about to exit. This is a good hook to
perform constant time checks of the module's state (like for unit
tests). The main event loop will no longer be run after the 'exit'
callback finishes, so timers may not be scheduled.
And it would be implemented as
process.on('exit', function() {
console.log('About to close');
});
It's worth mentioning, that it's generally not a good idea to try to manipulate data if you don't know the reason for the exit or exception. If you don't know what's wrong, it's usually a better idea to start fresh, than try to accomplish something with something that may very well be FUBAR.
I am using Mocha to run some tests on my SnailMailAddressParser project from the command-line. Unfortunately only some tests are run before Mocha exits.
The test file is very straightforward. You can see the test file here: test/test.coffee
It seems there is a race condition somewhere. When I run npm test, it does one of two things:
Runs one test; or
Runs 34 tests, starting at address_tester.
Clearly I am doing something asynchronous that needs to be caught, but I am not quite sure what yet. In any case, I do not know how to tell Mocha to wait for any asynchronous items to be reaped (i.e. some sort of Mocha.wait_all, if that is even possible - perhaps I have to add' done() calls, but I didn't think that was necessary for synchronous testing - which I thought this might be).
I will experiment of course and post any answers I glean from my testing, but I would be grateful for any insight.
The answer was that
fs.readFile filename, "utf8", -> ...
was operating asynchronously. When I had tried using
fs.readFileSync filename, "utf8", -> ...
it was not working because I was still passing a callback instead of reading the return value.
I solved the problem by changing the callback to:
data = fs.readFileSync filename, "utf8"
as no asynchronous operation was now being called.