Automate and Organize Tests
Install Mocha II

After installing Mocha as a dependency we can run it in two ways.

The first (and more tedious) method is to call it directly from node_modules:

$ ./node_modules/mocha/bin/mocha

The second (and recommended) method is to add a script to package.json. In the scripts object in package.json, set the value of "test" to mocha. It should look like this:

"scripts": { "test": "mocha" }

Now you can call Mocha with the following command:

$ npm test

Instead of manually running each test in the test directory, you can use this command to run the full test suite automatically.



Using the guide above, set the test script to "mocha" in package.json. This connects the command line command npm test with the Mocha package.


Run the test suite with Mocha. In the terminal window type:

$ npm test

The command will be executed but you will see a warning in the terminal: there aren’t any tests to run yet!

Folder Icon

Sign up to start coding

Already have an account?