kocha
Version:
Modern, simpler Mocha clone, no globals, lint friendly
149 lines (120 loc) • 4.14 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var TestSuite = require('./test-suite');
var TestCase = require('./test-case');
var TestHook = require('./test-hook');
var TestRunner = require('./test-runner');
var stringify = require('stringifier').stringify;
var runner = void 0;
/**
* Gets the runner.
* @return {TestRunner}
*/
var getRunner = exports.getRunner = function () {
return runner;
};
/**
* Resets the runner.
*/
exports.resetRunner = function () {
runner = new TestRunner();
};
exports.resetRunner();
var addSuite = function addSuite(title, cb, skipped) {
var parent = currentSuite();
var child = new TestSuite(title, skipped, parent);
parent.addSuite(child);
getRunner().setCurrentSuite(child);
cb();
getRunner().setCurrentSuite(parent);
};
var addTest = function addTest(title, cb, skipped) {
currentSuite().addTest(new TestCase(title, cb, skipped, currentSuite()));
};
var currentSuite = function currentSuite() {
return getRunner().getCurrentSuite();
};
var currentNode = function currentNode() {
return getRunner().getCurrentNode
/**
* Adds the test suite by the name and factory method.
* @param {string} title The title of the suite.
* @param {Function} cb The factory of subnodes
*/
();
};exports.describe = exports.context = function (title, cb) {
addSuite(title, cb, false);
};
/**
* Adds the skipped test suite by the name and factory method.
* @param {string} title The title of the suite.
* @param {Function} cb The factory of subnodes
*/
exports.describe.skip = exports.xdescribe = exports.xcontext = function (title, cb) {
addSuite(title, cb, true);
};
/**
* Adds the test case by the name and test case function.
* @param {string} title The title of the test case
* @param {Function} cb The test case function
*/
exports.it = exports.specify = function (title, cb) {
addTest(title, cb, false);
};
/**
* Adds the skipped test case by the name and test case function.
* @param {string} title The title of the test case
* @param {Function} cb The test case function
*/
exports.it.skip = exports.xit = exports.xspecify = function (title, cb) {
addTest(title, cb, true);
};
exports.before = function (cb) {
currentSuite().setBeforeHook(new TestHook('"before all" hook', cb, currentSuite()));
};
exports.beforeEach = function (cb) {
currentSuite().setBeforeEachHook(new TestHook('"before each" hook', cb, currentSuite()));
};
exports.after = function (cb) {
currentSuite().setAfterHook(new TestHook('"after all" hook', cb, currentSuite()));
};
exports.afterEach = function (cb) {
currentSuite().setAfterEachHook(new TestHook('"after each" hook', cb, currentSuite()));
};
exports.timeout = function (timeout) {
currentNode().setTimeout(timeout);
};
exports.retries = function (n) {
currentNode().setRetryCount(n);
};
/**
* Runs the test cases. Retruns the promise which resolves with true if all tests passed and returns false when any of the test cases and hooks failed.
* @return {Promise<boolean>}
*/
exports.run = function () {
var failed = false;
return new Promise(function (resolve, reject) {
return getRunner().on('fail', function () {
failed = true;
}).on('end', function () {
return setTimeout(function () {
return resolve(!failed);
});
}).run().catch(reject);
});
};
exports.TestSuite = TestSuite;
exports.TestCase = TestCase;
exports.TestHook = TestHook;
exports.TestRunner = TestRunner;
exports.stringify = stringify;
// Pretends to be ESM for transform-es2015-modules-commonjs
exports.__esModule = true;
// Exports all as default
// This enables `import kocha from 'kocha'` in babel.
exports.default = exports;
// Expose window.__kocha__ if the environment is browser
// This is for karma environment
if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object') {
window.__kocha__ = exports;
}
;