Running Your Tests?

So we’ve written many tests, but how do we run them? Good question. We actually have many ways we can run our tests, so let’s explore them.

We’ve briefly looked at how we can run our Tests in Mocha by running the following command:

mocha

The report we get from that is known as the ‘Spec view’ and can help with feedback during the development of your tests but what if we wanted to run those tests on a different box and consume results in some sort of dashboard? The spec view is not written to a file so we can provide some additional arguments to Mocha to report the test run to a file:

mocha --reporter=xunit --reporter-options output=results.xml

This will provide us with an XML file, that can be consumed by other applications to make a dashboard or inform the team if the tests have all passed or failed:

Finally, the command we gave Mocha is quite a long one, meaning it’s easy to forget all arguments or make mistakes using it. However, what we can do is update our package.json to link command to npm test.

{"name":"mochalessons","version":"1.0.0","description":"A demo package to show how we can create quick shortcuts to run Mocha",// Here we can set assign commands to keywords like 'test'"scripts":{"test":"mocha --reporter=xunit --reporter-options output=results.xml"},"repository":{"type":"git","url":"git+https://github.com/mwinteringham/free-node-basics-course.git"},"author":"Mark Winteringham"}

So based on the scripts object, we can now run npm test and this will run the command that test is assigned to, which is our special reporter.