Skip to content

Instantly share code, notes, and snippets.

@Crewe
Forked from shamasis/describe-it.js
Created May 3, 2016 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Crewe/4821aa16b88efa8d17c91032cbc99b55 to your computer and use it in GitHub Desktop.
Save Crewe/4821aa16b88efa8d17c91032cbc99b55 to your computer and use it in GitHub Desktop.
mocha/jasmine compatible test framework for postman test scripts (in less than 1KB minified)
/**
* @module describe-it
*
* This module defines global variables to provide unit test case runner functions compatible with mocha and jasmine.
* The codebase is written for brevity and facilitate being as lightweight as possible.
*
* The code is intended to be included in Postman Test Sandbox.
*/
/**
* Create global test object to be retrieved for final results
* @global
*
* @type {Object}
*/
(typeof tests !== 'object') && (tests = {});
var
/**
* Execute your test within this functions.
*
* @param {string} k - name of the test
* @param {function} v - the test itself
*/
it = ((it = function (k, v) {
it.d.push(k); // start tracking test depth
it.t[it.d] = 1; // test marked as passed unless set otherwise
it.b.forEach(it.c); // execute all before each
try { v(); } // execute the test and safely catch it and set the result to failure on error
catch (e) { it.t[it.d] = 0; setTimeout && setTimeout(function () { throw e; }); }
it.a.forEach(it.c); // execute after each
it.d.pop(); // untrack
}),
/**
* afterEach function stack
* @private
* @type {Array}
*/
it.a = [],
/**
* beforeEach function stack
* @private
* @type {Array}
*/
it.b = [],
/**
* Function to call a function sent as parameter
*/
it.c = function (x) { x(); },
/**
* Test depth tracking stack
* @private
* @type {Array}
*/
it.d = [],
it.d.toString = function () { return this.join(' '); }, // pretty format test tracking
/**
* Store reference for global test variable for shorthand access.
* @private
* @type {object}
*/
it.t = tests,
/**
* TDD assertion library
* @constructor
* @private
*
* @param {*=} [v] value to assert
*/
it.x = function (v) { this.v = v; }, // the expect prototype
it.xp = it.x.prototype,
it.xp.toBe = function (x) { (this.v !== x) && it._(); },
it.xp.toNotBe = function (x) { (this.v === x) && it._(); },
it.xp.toEql = function (x) { (this.v != x) && it._(); },
it.xp.toNotEql = function (x) { (this.v == x) && it._(); },
it.xp.toBeOk = function () { !this.v && it._(); },
it.xp.toNotBeOk = function () { this.v && it._(); },
it), // return original variable
/**
* Group a unit test by component
*
* @param {string} k - name of the component
* @param {function} v - tests within the component
*/
describe = function (k, v) { it.d.push(k); v(); it.d.pop(); },
/**
* Assert a value
*
* @param {*=} [v]
* @returns {object}
*/
expect = function (v) { return new it.x(v); },
/**
* Execute fixture and other setup before each test
* @function
* @param {function} f
*/
beforeEach = it.b.push.bind(it.b),
/**
* Execute teardown of fixtures after each test
* @function
* @param {function} f
*/
afterEach = it.a.push.bind(it.a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment