UNPKG

semtest

Version:

NodeJs Unit test framework combining Tape, Proxyquire and Sinon

184 lines (160 loc) 5.4 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: main.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: main.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/** * ### 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&lt;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 &amp;&amp; testEngine.skip) { testEngine.skip(`${description}`, assert.test); } else if (assert.only &amp;&amp; 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&lt;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&lt;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 }; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-Library_Test-Helpers.html">Library/Test-Helpers</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Jan 03 2017 10:34:18 GMT+0100 (CET) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>