Skip to content

Instantly share code, notes, and snippets.

@shamasis
Last active September 5, 2023 07:12
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save shamasis/7b12f451374314500da3 to your computer and use it in GitHub Desktop.
Save shamasis/7b12f451374314500da3 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);
@NirLevanon-zz
Copy link

I've tried forking and doing it myself, but I found it difficult to add in some logic that would add to the name of the test that failed assertion something like (". Actual value: " + v), just so we can get some indication to what was the value that caused the failure of assertion.

@Stwissel
Copy link

Stwissel commented Sep 8, 2016

How hard would it be to add a few more? I would love to see toBeAnArray() and haveElementCount(from, to)

@apanaghia
Copy link

It looks like toEql and toNotEql have inverted logic? same for toBe and toNotBe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment