# Testing Framework

## 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:

* [chai](http://www.chaijs.com/)
* [chai-datetime](https://github.com/mguterl/chai-datetime)
* [mocha](https://mochajs.org/)
* [superagent](https://github.com/visionmedia/superagent)
* [supertest](https://github.com/visionmedia/supertest)

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

## Installation

The testing module is automatically installed when you generate a new Blueprint project. But, in case you need to manually install it, or update to the latest version, use the following command.

```bash
npm install --save-dev @onehilltech/blueprint-testing 
```

{% hint style="info" %}
Do not forget to the `--save-dev` option when installing the `blueprint-testing` module.
{% endhint %}

## Running Test Cases

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

```bash
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](https://www.jetbrains.com/webstorm/). If you choose to run your test cases via an IDE, you must remember that `tests/unit-tests/bootstrap.js` must load first  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. A common solution to ensure `tests/unit-tests/bootstrap.js` loads first is to set `tests/unit-tests` as the starting directory for your unit tests.

## 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](/developer-guide/routers-and-controllers/routers.md#reactions-to-paths) defined the route `POST /messages`. We can test this route by sending a request a `POST` request to `/messages`.

{% code title="tests/unit-tests/app/routers/message-test.js" %}

```javascript
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});
  });
});
```

{% endcode %}

As shown in the example above, we start by importing the `request` method from [blueprint-testing](https://github.com/onehilltech/blueprint-testing). The request method is a helper method that will return a [supertest request](https://github.com/visionmedia/supertest) 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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blueprint.onehilltech.com/developer-guide/testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
