Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Completely End a Test in Node Mocha Without Continuing

How do I force a Mochajs test to end completely without continuing on to the next tests. A scenario could be prevent any further tests if the environment was accidentally set to production and I need to prevent the tests from continuing.

I've tried throwing Errors but those don't stop the entire test because it's running asynchronously.

like image 760
adam8810 Avatar asked Aug 27 '14 13:08

adam8810


People also ask

How do you end a Mocha test?

Steps to Reproduce Run with mocha --timeout 1000000 test. js. Do not wait until the test finishes and press Ctrl+C to terminate it.

How do you skip a Mocha test case?

This inclusive ability is available in Mocha by appending . skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.

How do I skip a test in NodeJS?

You can skip tests by placing an x in front of the describe or it block, or placing a . skip after it. describe('feature 1', function() {}); describe.

How do you run a single test case in Mocha?

Learn how to run only one test with Mocha To run a single test (as defined in it() ), you can simply call the only() method on it in the following way: it. only('...', function () { // only this test will run... });


2 Answers

I generally agree with Glen, but since you have a decent use case, you should be able to trigger node to exit with the process.exit() command. See http://nodejs.org/api/process.html#process_process_exit_code. You can use it like so:

process.exit(1);
like image 125
Jessie A. Morris Avatar answered Oct 30 '22 16:10

Jessie A. Morris


As of the mocha documentation, you can add --exit flag when you are executing the tests. It will stop the execution whenever all the tests have been executed successfully or not.

ex:

mocha **/*.spec.js --exit
like image 40
Jayanga Jayathilake Avatar answered Oct 30 '22 17:10

Jayanga Jayathilake