Testing Framework

Last updated 6 months ago

Learn to write test cases

Overview

Testing is an integral part of any application. We therefore have a node module and guidelines to testing a Blueprint application. We use the following middleware to facilitate testing:

The remainder of this section will detail how to write test for Blueprint.

Installation

Let's install our testing module as a development dependency.

npm install --save-dev blueprint-testing

Do not forget to the --save-dev option when installing the blueprint-testing module.

Running Test Cases

The simplest way to run your test cases is via npm on the command-line.

npm test

This will instruct npm to run all test cases in your tests/unit-tests.

Using an IDE

npm is just one way to run your test cases, but you are not restricted to only using npm. You can use your favorite IDE to run your tests cases, such as WebStorm. If you choose to run your test cases via an IDE, you must remember that tests/unit-tests/bootstrap.js must always load this file before running any test cases. This file will load your Blueprint application into memory, and make the application resources available for testing. If you fail to load this file first during your testing exercises, you will get error messages related to the application not being loaded.

Recommend Mocha Usage

Testing Routes

You test different routes in Blueprint by sending requests to paths on the Blueprint application under test. For example, the message router from our previous examples defined the route POST /messages. We can test this route by sending a request a POST request to /messages.

tests/unit-tests/app/routers/message-test.js
const { request } = require ('@onehilltech/blueprint-testing');
describe ('app | routers | message', function () {
it ('should create a message', function () {
const message = {
id: 0,
to: 'john.doe@me.com',
from: 'jane.doe@you.com',
date: Date.now (),
subject: 'Dummy Message',
content; 'This is a dummy message.'
};
return request ()
.post ('/messages')
.send ({message})
.expect (200, {message});
});
});

As shown in the example above, we start by importing the request method from blueprint-testing. The request method is a helper method that will return a supertest request object that initiates a http request against the Blueprint application. The request has the option of returning a Promise that can be returned from the unit testing method to signify asynchronous execution.

Testing Blueprint Modules