Source: lib/test-helpers.js

/**
 * ### Test helpers
 *
 * This module hosts everything needed for tests.
 * Abstraction over the expection engine, abstraction over the basic functions,
 * this module is the Test toolbelt.
 * Little to no test framework logic should be in unit tests to keep them
 * focused on code documentation.
 *
 * @module Library/Test-Helpers
 */
"use strict";

const path = require("path");

const lodash = require("lodash/fp");
const {
    map,
    keys,
    flow,
    defaultsDeep
} = lodash;

const BPromise = require("bluebird");

const tape = require("blue-tape");
const sinon = require("sinon");
const proxyquire = require("proxyquire").noPreserveCache().noCallThru();

/**
 * Return a function that will execute a test with a given description
 * @param {Function} testFn - Test engine
 * @param {String} testDescription - Unit test description
 * @param {Function} cb - Callback called with test engine object
 * @return {Function} Unit test handler
 */
exports.unitTest = (testFn, testDescription) => (cb) =>
    testFn(`${testDescription}`, cb);

/**
 * Returns a function that will perform several tests, each with a concatenated
 * description.
 * Descriptions will be concatenated to form a test statement that will have this
 * structure: "Module - Function(): When XXXXXXXXX, it should YYYYYYYYY
 * @curried
 * @param {String} mode - Execution mode(null = normal, skip, or only)
 * @param {String} moduleName - Tested module description
 * @param {Array<Object>} assertions - Assertion array to perform
 * @return {Array} Assertions execution results
 */
exports.executeTestsInternal = (mode) => (moduleName, functionBlocks) =>
    map((fnObj) =>
        map(function(assert) {
            const description = `${moduleName} - ${fnObj.name}: when ${assert.when}, it should ${assert.should}`;
            const newTest = exports.unitTest(tape, description);
            newTest.skip = exports.unitTest(tape.skip, description);
            newTest.only = exports.unitTest(tape.only, description);
            // istanbul ignore if
            if (mode === "skip" || fnObj.skip) {
                return assert.test(newTest.skip);
            }
            return assert.test(newTest);
        }, fnObj.assertions), functionBlocks);
exports.executeTests = exports.executeTestsInternal();
exports.executeTests.skip = exports.executeTestsInternal("skip");

/**
 * Identity function
 * @param {Mixed} input - Value to be returned
 * @return {Mixed} Same as input
 */
exports.idFn = (input) => input;

/**
 * Return the identity function
 * @return {Function} Identity function
 */
exports.idFnHO = () => exports.idFn;

/**
 * Returns null
 * @return {Null} Null
 */
exports.nullFn = () => null;

/**
 * Returns a function that returns null
 * @return {Function} Function that returns null
 */
exports.nullFnHO = () => exports.nullFn;

/**
 * Throws an Error
 * @param {String} message - Error message
 * @return {Error} Error thrown
 */
exports.throwFn = function throwFn(message) {
    throw new Error(message);
};

/**
 * Returns a function that throws an Error
 * @param {String} message - Error message
 * @return {Function} Function that throws an error
 */
exports.throwFnHO = (message) => () => exports.throwFn(message);

/**
 * Returns a resolved Promise
 * @param {Mixed} value - Resolved value
 * @return {Promise<Mixed>} Promise resolved with given value
 */
exports.resolveFn = (value) => BPromise.resolve(value);

/**
 * Returns a function that return a resolved Promise with given value
 * @param  {Mixed} value - Resolved value
 * @return {Function} Function that returns a resolved Promise with given value
 */
exports.resolveFnHO = (value) => () => exports.resolveFn(value);

/**
 * Returns a rejected Promise
 * @param  {String} reason - Rejection reason
 * @return {Promise<String>} Promise rejected with given reason
 */
exports.rejectFn = (reason) => BPromise.reject(new Error(reason));

/*
 * Returns a function that return a rejected Promise with given reason
 * @param  {String} reason - Rejection reason
 * @return {Function} Function that return a rejected Promise with given reason
 */
exports.rejectFnHO = (reason) => (otherReason) =>
    (otherReason) ? exports.rejectFn(otherReason) : exports.rejectFn(reason);

// Abstracts Sinon.js in tests
exports.stub = sinon.stub;
exports.spy = sinon.spy;

/**
 * Create an Express-like response mock
 * @return {Object} Response mock
 */
exports.createResponseMock = function() {
    const response = {};
    response.status = function(status) {
        response.status = status;
        return response;
    };
    response.json = function(body) {
        response.body = body;
        return response;
    };
    return response;
};

// Stubbable modules, based on project root if project related, to ensure consistency across tests
// Node modules can also go here if they can (and should) be stubbed
exports.bluebird = "bluebird";
exports.swaggerTools = "swagger-tools";
exports.lodash = "lodash/fp";

const projectRoot = path.resolve(__dirname, "../");
exports.config = path.join(projectRoot, "config");
exports.utils = path.join(projectRoot, "lib/utils");
exports.middlewares = path.join(projectRoot, "lib/middlewares");
exports.models = path.join(projectRoot, "models");
exports.entity = path.join(projectRoot, "models/entity");
exports.controllers = path.join(projectRoot, "api/controllers");
exports.state = path.join(projectRoot, "lib/state");
exports.swaggerLib = path.join(projectRoot, "lib/swagger");

// Proxyquire configuration object for stubs (to prevent all calls to the
// original module)
const noCallThru = {
    "@noCallThru": true
};

// Dependencies which have a default stub (that can be overriden)
const defaultStubs = {
    bluebird: BPromise,
    path: path,
    proxyquire: proxyquire
};

defaultStubs[exports.utils] = {
    log: exports.nullFn,
    logAndResolve: exports.resolveFnHO,
    logAndReject: exports.rejectFnHO,
    // All other utils functions are allowed because they are related to module
    // loading and path abstraction
    "@noCallThru": false
};
defaultStubs[exports.lodash] = lodash;

// Dependencies that have to be stubbed (no default)
map(function(dep) {
    defaultStubs[dep] = noCallThru;
}, [
    "fs",
    "tape",
    "express",
    exports.swaggerTools,
    exports.config,
    exports.middlewares,
    exports.models,
    exports.entity,
    exports.controllers
]);

/**
 * Convert an object into another object with computed properties by using the given
 * object properties names as variables declared in this module.
 * If they are not defined, this function will assume that they refer to node
 * modules instead of local ones during the computed property resolution.
 * @example Given an object { a: "foo", b: "bar" }
 * this function will return an object { [a]: "foo", [b]: "bar" } with a and b
 * eventually defined as variables in this module.
 * @param  {Object} object - Input object
 * @return {Object} Converted object
 */
exports.convertIntoComputedProperties = function(object) {
    const newObj = {};
    const objKeys = keys(object);
    map(function(property) {
            if (exports[property]) {
                newObj[exports[property]] = object[property];
            } else {
                newObj[property] = object[property];
            }
        },
        objKeys
    );
    return newObj;
};

/**
 * Require a module with stubbed dependencies
 * @param {String} moduleName - Module name to proxiquire
 * @param {Object} customStubs - Custom stubs for the proxified module
 * @return {Mixed} Proxified module
 */
exports.requireWithStubs = function(moduleName, customStubs) {
    const stubs = flow(
        defaultsDeep(exports.convertIntoComputedProperties(customStubs)),
        defaultsDeep(defaultStubs)
    )({});
    return proxyquire(
        moduleName,
        stubs
    );
};

/**
 * @name prepareForTests
 * @function
 * @description Prepare a module for tests by requiring it with stubs
 * Module name is based on the given test file name that
 * will consume the prepared module.
 * @curried
 * @param {String} testFileName - Test file name
 * @param {Object} customStubs - Custom stubs for the proxified module
 * @return {Mixed} Proxified module
 */
exports.prepareForTests = (testFileName) => (customStubs) =>
    exports.requireWithStubs(
        testFileName.replace(".spec", ""),
        customStubs
    );