semtest
Version:
NodeJs Unit test framework combining Tape, Proxyquire and Sinon
158 lines (144 loc) • 5.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.utilityFunctions = exports.executeTests = exports.executeTestsInternal = undefined;
exports.rewire$executeTestsInternal = rewire$executeTestsInternal;
exports.rewire$executeTests = rewire$executeTests;
exports.restore = restore;
var _bluebird = require("bluebird");
var _bluebird2 = _interopRequireDefault(_bluebird);
var _blueTape = require("blue-tape");
var _blueTape2 = _interopRequireDefault(_blueTape);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var executeTestsInternal = _executeTestsInternal,
executeTests = _executeTests; /**
* ### 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
*/
/**
* 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
*/
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);
}
}));
}
exports.executeTestsInternal = executeTestsInternal;
function _executeTests(...args) {
return executeTestsInternal(_blueTape2.default)(...args);
}
exports.executeTests = executeTests;
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 => _bluebird2.default.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 => () => _bluebird2.default.resolve(value);
/**
* Returns a rejected Promise
* @param {String} reason - Rejection reason
* @return {Promise<String>} Promise rejected with given reason
*/
utilityFunctions.rejectFn = reason => _bluebird2.default.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 ? _bluebird2.default.reject(new Error(otherReason)) : _bluebird2.default.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;
};
exports.utilityFunctions = utilityFunctions;
function rewire$executeTestsInternal($stub) {
exports.executeTestsInternal = executeTestsInternal = $stub;
}
function rewire$executeTests($stub) {
exports.executeTests = executeTests = $stub;
}
function restore() {
exports.executeTestsInternal = executeTestsInternal = _executeTestsInternal;
exports.executeTests = executeTests = _executeTests;
}
//# sourceMappingURL=main.js.map