Brian M. Hunt
Brian M. Hunt

Reputation: 83858

Mocha prematurely exits from testing

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:

  1. Runs one test; or
  2. 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.

Upvotes: 0

Views: 668

Answers (1)

Brian M. Hunt
Brian M. Hunt

Reputation: 83858

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.

Upvotes: 1

Related Questions