UNPKG

semtest

Version:

NodeJs Unit test framework combining Tape, Proxyquire and Sinon

133 lines (127 loc) 4.2 kB
/** * ### 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 */ import BPromise from "bluebird"; import tape from "blue-tape"; /** * Execute all given tests, each with a concatenated description. * Descriptions will be concatenated to form an assertion statement that will * have this structure: * "Module - Function(): [When XXXXXXXXX,] it should YYYYYYYYY" * ("When" can be omitted for assertions that are always true, thus the * brackets notation) * @param {Function} testEngine - Test engine * @param {String} moduleName - Tested module description * @param {Array<Object>} functionBlocks - Function blocks * @return {Void} Nothing */ export function executeTestsInternal(testEngine) { return (moduleName, functionBlocks) => functionBlocks.forEach( (fnObj) => fnObj.assertions.forEach( function(assert) { const description = `${moduleName}-${fnObj.name}: ${(assert.when) ? "when " + assert.when + ", " : ""}it should ${assert.should}`; if (assert.skip && testEngine.skip) { testEngine.skip(`${description}`, assert.test); } else if (assert.only && testEngine.only) { testEngine.only(`${description}`, assert.test); } else { testEngine(`${description}`, assert.test); } } ) ); } export function executeTests(...args) { return executeTestsInternal(tape)(...args); } const utilityFunctions = {}; /** * Identity function * @param {Mixed} input - Value to be returned * @return {Mixed} Same as input */ utilityFunctions.idFn = (input) => input; /** * Return the identity function * @return {Function} Identity function */ utilityFunctions.idFnHO = () => utilityFunctions.idFn; /** * Returns null * @return {Null} Null */ utilityFunctions.nullFn = () => null; /** * Returns a function that returns null * @return {Function} Function that returns null */ utilityFunctions.nullFnHO = () => () => null; /** * Throws an Error * @param {String} message - Error message * @return {Error} Error thrown */ utilityFunctions.throwFn = function(message) { throw new Error(message); }; /** * Returns a function that throws an Error * @param {String} message - Error message * @return {Function} Function that throws an error */ utilityFunctions.throwFnHO = (message) => function() { throw new Error(message); }; /** * Returns a resolved Promise * @param {Mixed} value - Resolved value * @return {Promise<Mixed>} Promise resolved with given value */ utilityFunctions.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 */ utilityFunctions.resolveFnHO = (value) => () => BPromise.resolve(value); /** * Returns a rejected Promise * @param {String} reason - Rejection reason * @return {Promise<String>} Promise rejected with given reason */ utilityFunctions.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 */ utilityFunctions.rejectFnHO = (reason) => (otherReason) => (otherReason) ? BPromise.reject(new Error(otherReason)) : BPromise.reject(new Error(reason)); /** * Create an Express-like response mock * @return {Object} Response mock */ utilityFunctions.createResponseMock = function() { const response = {}; response.status = function(status) { response.status = status; return response; }; response.json = function(body) { response.body = body; return response; }; return response; }; export { utilityFunctions };