Testing node.js Applications

Testing is important – without it, bugs sneak into the wild where they’re more difficult and costly to fix. Automating testing can significantly increase test coverage as well as reduce long-term costs.

While there are several testing tools and frameworks available and important ones are listed

Mocha

Mocha is an excellent testing framework that allows for use of promises and asynchronous/await with TypeScript or Babel. Mocha handles executing the tests you create, catches any assertion errors and pretty-prints these to the console. Mocha is a test runner. This just means that it is a tool that runs/executes our tests. The tests themselves aren’t written in Mocha. Other test runners include; Jasmine, Jest.

Let’s install mocha globally to be able to use it on our command line: > npm install -g mocha

Create our new directory for our app:

> mkdir testing-async-code && cd testing-async-code

> npm init

> npm install –save mocha

> mkdir tests

Then add a basic test just to see how Mocha works; for now we’ll use Assert as our assertion library which comes in NodeJS so no npm install needed.

const assert = require(“assert”);

describe(“smoke test”, function() {

it(“checks equality”, function() {

assert.equal(true, true);

});

});

To run this we just run the command mocha and pass in the location of our tests: > mocha tests/

Yay, our test passes. What essentially happens is that the assert tests something, if the test passes it doesn’t do anything; if it fails it throws an exception and this stops your tests. Just to see how a failure would look like we can change the line to something that would fail.

const assert = require(“assert”);

describe(“smoke test”, function() {

it(“checks equality”, function() {

assert.equal(true, false);

});

});

Notice our tests stop and we get an error message telling us we have an AssertionError and what is expected. Assert, throws an error and that is how Mocha knows whether our tests are failing or passing.

Chai

Chai is an assertion library that allows you to use natural language constructs when developing your tests. This is extremely helpful as many assertion libraries can be rather cryptic.

The following is an example that illustrates how naturally assertions can be written with Chai:

expect (myResult).to.equal(23)

Chai is an assertion library. So far, we have just used Assert to make assertions and we have an idea of what asserts do. Chai, does the exact same thing but allows for better readability. Let’s install it:

> npm i –save-dev chai

Now we can change our smoke test to read like this:

const chai = require(“chai”);

const expect = chai.expect;

describe(“smoke test”, function() {

it(“checks equality”, function() {

expect(true).to.be.true;

});

});

Mockery

Mockery is a small npm module that allows you to substitute test mocks without modifying your production code in any way. By simply creating a mock function or module and registering it with mockery, Node.js will inject your mocks wherever a require statement is used in your code.

Jenkins

Jenkins is a continuous integration system that can hook into your version control (e.g. git) and automatically execute mocha any time a commit occurs. This means your product is being tested every time a change occurs.

Setup a testing framework in Node.js

Step #1: Add mocha, chai, and mockery as dependencies to your project.

Step #2: Setup your package.json to include a test script.

Step #3: Create some tests.

Step #4: Execute your tests by typing ‘npm run test’ in a command line.

Git and node.js
Debugging

Get industry recognized certification – Contact us

keyboard_arrow_up