Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`npm test` never exits

I use mocha to run my unit tests, and often use npm test in order to run it. My package.json contains these script definitions:

"pretest": "NODE_ENV=test node migrate all",
"test": "DEBUG= NODE_ENV=test mocha --recursive",

If I run either one of these commands directly in my shell (i.e. not going through npm) they execute fine (790 tests takes about 2m to run, the migration script is done in under 1s). The processes also exit cleanly.

If, however, I run these via npm test, everything runs in exactly the same way but the process doesn't exit (I have to manually cancel it with ^c).

I can't work out how to debug exactly what is going on here in order to work out why the process isn't exiting.

It's worth noting that if I test a subdirectory (npm test ./test/queue) which does not interact at all with the database, then the process exits fine. These tests do, however, interact with an AMQP broker so there is some activity over sockets. This suggests to me that the database connection is causing the problem. I am using knex to connect to a postgres9.6 server. This also suggests that the pretest script is not the problem. If I try to run a suite of tests which do interact with the database, the process never exits (so presumably the open sockets are preventing it from doing so, but why this should only happen in the case of npm test rather than direct execution is beyond me).

Extra info:

  • node version: 8.7.0
  • mocha version: 4.0.1
  • npm version: 5.6.0
  • knex version: 0.14.0
  • I am using async/await in my codebase
like image 729
GTF Avatar asked Dec 28 '17 23:12

GTF


1 Answers

I was mistaken in thinking this to be an npm problem. Rather, when I was running mocha in my shell it was using the globally installed mocha, which is of a different version to the locally installed one. Running with this version causes the same hanging issue.

This change in behaviour appears to be coming from this issue, which is described thus in the changelog:

By default, Mocha will no longer force the process to exit once all tests complete. This means any test code (or code under test) which would normally prevent node from exiting will do so when run in Mocha. Supply the --exit flag to revert to pre-v4.0.0 behavior (@ScottFreeCode, @boneskull)

like image 158
GTF Avatar answered Sep 23 '22 00:09

GTF