node package manager

test-routes

test-routes

Helps you implement your testing API easier

Introductory Example

const express = require('express')
const app = express()
const someCustomModule = require('./someCustomModule');

const testroutes = require('test-routes');

testroutes.config({
    server : app,
    authorization : someAuthorizationMiddleware,

});

testroutes.addScope('unit-tests', 'path/to/unit/tests')
testroutes.addScope('integration-tests', 'path/to/integration/tests')
testroutes.setDestroyMethod(someCustomModule.getDestroyPromise);

app.listen(3000, function () { /*...*/ })

Routes

GET /tests/

returns an object with info on the testing capabilities of the service:

  • destroy {Boolean} : whether the service implements and destroy logic
  • scopes {Array} : the list of available scopes.

For each scope:

  • scope.name : the unique identifier for the scope. Can be used to invoke the scope tests through the run route (see later)
  • scope.dir : the directory of the scope
  • route : the route to send the request to in order to invoke the scope tests
  • files : the list of files (filenames) available in the scope

GET /tests/:scope?files[]

Runs the selected files in the scope. The test results are sent in JSON format in the response body. Note 1 : it's always better to invoke only one file per request. Note 2 : watch out for request timeouts

GET /tests/destroy

Runs any destroy logic.

API

config(options)

  • options.server : mandatory, reference to express server
  • options.authorization : optional, an authorization middleware (e.g. worf.authenticate()). Default: no authorization
  • options.route : optional, the base route for the test API. Default: /tests/
  • options.legalEnvs : options, values of process.env.NODE_ENV under which the API is active. If the current env is not in this list, the API does not expose any route. Default: 'development'.

addScope(name, dir)

Adds a scope. This is a directory that contains a series of .js test files (mocha).

setDestroyMethod(method)

Set the reference to the method that returns a destroy promise. This promise is invoked every time a request to the destroy route is sent. Example of destroy method;

var destroy = module.exports = function() {
    const db = require('./path/to/db');

    return new Promise((resolve, reject) => {
        db.SomeModel.destroy({ truncate: true, cascade : true, force : true })
        .then(() => db.SomeOtherModel.destroy({ truncate: true, cascade : true, force : true }))
        .then(resolve)
        .catch(reject)
    })
}

Dev-only logic

To enforce the test API to be activated only in development mode, the following approach is suggested:

try {
    const someCustomModule = require('./someCustomModule');
    const testroutes = require('test-routes');

    testroutes.config({
        server : app,
        authorization : someAuthorizationMiddleware,
    });

    testroutes.addScope('unit-tests', 'path/to/unit/tests')
    testroutes.addScope('integration-tests', 'path/to/integration/tests')
    testroutes.setDestroyMethod(someCustomModule.getDestroyPromise);
    /* ... ayn other testroutes configuratio */
}
catch(e) { console.error(e) }

Now, if test-routes is installed as npm install --save-dev test-routes, all the testing API logic is enabled only in development, since in production the module is not even available.