Running after hook changes the test terminal output.

50 views
Skip to first unread message

Smadar

unread,
Aug 10, 2018, 10:49:23 AM8/10/18
to Mocha
I have a few test files but this is true for when I run just one test file.
My 'after' hook only contains process.exit(0). Without it the tests run to completion and show the summary but do not close the test and bring me back to the prompt in the console.

If I add the 'after' hook the tests run as expected but at the end of the test the console does not display the test summary:
183 passing (6s)
  3 pending
  2 failing
which is followed by the 2 failed cases detailing the fail.

Is there an easy way to fix it?
I tried the 'after' hook inside the top describe, and outside of it (as a global hook in the root describe), and even in a dedicated file listed to run last == all with the same outcome.


after(() => {
process.exit(0);
});

Vlad GURDIGA

unread,
Aug 12, 2018, 5:19:55 AM8/12/18
to Mocha
Hey Smadar! 👋

I don’t understand what is it that you’re trying to accomplish. 🤔

As for process.exit(0), it’ll surely drop you back to the console immediately—no matter where you put it—because it exits Node. 🤓

So, what are you up to? 🤓

Smadar

unread,
Aug 13, 2018, 9:22:29 AM8/13/18
to Mocha
Hi Vlad, thank you for responding.

I think I have a few issues going on, but first I will tell you what I am trying to do.
I am trying to have 1 'after' hook for 2 files, and all it has to do is close the process so I get back to the prompt.
The 2 files can be run by either dictating the order on package.json or just putting *.js

Here are the problems that I am having:
1. When I use process.exit it seems to finish the tests a little too soon before I can see all the output of the tests on the console. To be clear, every test runs and writes a success or fail line on the console. But the final summary that tells me how many tests run and how many failed is not showing. If I comment out the exit line the tests shows all the missing data but does not exit to the prompt. Is there a better way to got the prompt?

2. Since I am trying to run 2 files I noticed that the 'after' is blocking the second file from running. I tried to move the after to be a global hook (outside of the first describe) but that didn't work. I tried a 3rd file that will hold only the after command but that didn't work either. It is possible that if there is a better way to close the tests in the first question, this question will resolve itself.

Vlad GURDIGA

unread,
Aug 13, 2018, 11:28:02 AM8/13/18
to Mocha
Hey Smadar! 👋

As far as I can understand, mocha should definitely drop you back to the terminal after it runs the tests (unless you run it with the --watch parameter, of course). 🤔

So, what I’d do in cases like these is I’d reduce the context to the minimum: I’d to move all the test files to a safe place, out of the test directory, and create an empty-ish test file, something like this:

describe('file1', function() {
  it('runs', function() {
    // TODO: put assertions here
  });
});

If that runs to completion and drops me back to the terminal after it runs the “test” — this is my first little success! 🤓

Now I will begin copy-pasting the contents of the real tests (that I just moved away) into this new one, piece by piece, and after each little step I’d make sure that it still runs to completion and drops me back to the terminal. One small (but sure!) step at a time. 🙂

My suspicion is that some of the code involved in the tests—probably the production code—holds the Node process. I’m thinking it may be listening to the network as an HTTP server does, or just waiting for some input, which could be the case for a console app.

In any case, I’d give it a try and see what comes out. 🙂

Good luck! 🤓

Smadar Mavor

unread,
Aug 13, 2018, 12:31:54 PM8/13/18
to moc...@googlegroups.com
Thanks! I will try that, but after you described this - I think this started after I added DB connection and some promises. I will confirm this is the case and see if I can figure out a better solution for that issue. 

--
You received this message because you are subscribed to the Google Groups "Mocha" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mochajs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Smadar

unread,
Aug 21, 2018, 10:15:51 AM8/21/18
to Mocha
Hey Vlad
Since I found a solution I figured I would share here in case someone else struggles with this issue.
Instead of doing an 'after' with process out. I am now adding --exit to the end of the run command in package.json
so it looks like this:
"npm run pretest && mocha *.js --config=qa --exit",
To unsubscribe from this group and stop receiving emails from it, send an email to mochajs+unsubscribe@googlegroups.com.

Vlad GURDIGA

unread,
Aug 22, 2018, 3:35:11 AM8/22/18
to Mocha
Wow! I didn’t expect that. 🤓

I’m wondering if you have a .mocha.opts file? 🤔
Reply all
Reply to author
Forward
0 new messages