mocha-annotated
Version:
Mocha but with tasks and feedback built into it!
233 lines (203 loc) • 6.58 kB
JavaScript
var _templateObject = _taggedTemplateLiteral(['^[ \t]{1,', '}'], ['^[ \\t]{1,', '}']);
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var Mocha = require('mocha');
var Common = require('mocha/lib/interfaces/common');
var _require = require('mocha/lib/utils'),
inherits = _require.inherits;
/* eslint-disable no-param-reassign, no-multi-assign */
function AnnotatedTest(title, task, feedback, fn) {
Mocha.Test.call(this, title, fn);
this.pending = !fn;
this.type = 'test';
this.task = task;
this.feedback = feedback;
this.annotated = true;
}
/**
* Inherit from `Test.prototype`.
*/
inherits(AnnotatedTest, Mocha.Test);
AnnotatedTest.prototype.clone = function clone() {
var test = new AnnotatedTest(this.title, this.task, this.feedback, this.fn);
test.timeout(this.timeout());
test.slow(this.slow());
test.enableTimeouts(this.enableTimeouts());
test.retries(this.retries());
test.currentRetry(this.currentRetry());
test.globals(this.globals());
test.parent = this.parent;
test.file = this.file;
test.ctx = this.ctx;
return test;
};
Mocha.AnnotatedTest = AnnotatedTest;
/**
* Annotated BDD-style interface:
* @example How to use
* describe('Array', function() {
* describe('#indexOf()', function() {
* // With a task number
* it.annotated(
* 'should return -1 when not present',
* 1,
* strip_heredoc`
* Whoops, it looks like we forgot to return -1 from our when the
* item is not found in the array.
* `,
* () => {
* expect(arr.indexOf(11)).to.equal(-1);
* },
* );
*
* // Without a task number
* it.annotated(
* 'should return item index when present',
* strip_heredoc`
* Uh oh, when the item is in the array, we should return the item's index.
* `,
* () => {
* expect(arr.indexOf(1)).to.equal(0);
* },
* );
* });
* });
*
* @param {Suite} suite Root suite.
*/
Mocha.interfaces['mocha-annotated'] = function annotatedBDD(suite) {
var suites = [suite];
suite.on('pre-require', function (context, file, mocha) {
var common = Common(suites, context, mocha);
context.before = common.before;
context.after = common.after;
context.beforeEach = common.beforeEach;
context.afterEach = common.afterEach;
context.run = mocha.options.delay && common.runWithSuite(suite);
/**
* Describe a "suite" with the given `title`
* and callback `fn` containing nested suites
* and/or tests.
*/
context.describe = context.context = function (title, fn) {
return common.suite.create({
title: title,
file: file,
fn: fn
});
};
/**
* Pending describe.
*/
context.xdescribe = context.xcontext = context.describe.skip = function (title, fn) {
return common.suite.skip({
title: title,
file: file,
fn: fn
});
};
/**
* Exclusive suite.
*/
context.describe.only = function (title, fn) {
return common.suite.only({
title: title,
file: file,
fn: fn
});
};
/**
* Describe a specification or test-case
* with the given `title` and callback `fn`
* acting as a thunk.
*/
context.it = context.specify = function (title, fn) {
var current = suites[0];
if (current.isPending()) {
fn = null;
}
var test = new Mocha.Test(title, fn);
test.file = file;
current.addTest(test);
return test;
};
/**
* it.annotated(title, task, feedback, fn)
* it.annotated(title, feedback, fn)
* it.annotated(title, task, fn)
* it.annotated(title, fn)
*/
context.it.annotated = function (title) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var task = 1;
var feedback = void 0;
var fn = args.pop();
while (args.length > 0) {
var arg = args.pop();
if (typeof arg !== 'number') {
feedback = arg;
} else {
task = arg;
}
}
var current = suites[0];
if (current.isPending()) {
fn = null;
}
var test = new Mocha.AnnotatedTest(title, task, feedback, fn);
test.file = file;
current.addTest(test);
return test;
};
/**
* Exclusive test-case.
*/
context.it.only = function (title, fn) {
return common.test.only(mocha, context.it(title, fn));
};
context.it.annotated.only = function () {
var _context$it;
var test = (_context$it = context.it).annotated.apply(_context$it, arguments);
return common.test.only(mocha, test);
};
/**
* Pending test case.
*/
context.xit = context.xit.annotated = context.xspecify = context.it.skip = context.it.annotated.skip = function (title) {
context.it(title);
};
/**
* Number of attempts to retry.
*/
context.it.retries = context.it.annotated.retries = function (n) {
context.retries(n);
};
/**
* Strip the leading whitespace, preserve newlines, and preserve indentation level
*/
context.strip_heredoc = function (str) {
for (var _len2 = arguments.length, substitutions = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
substitutions[_key2 - 1] = arguments[_key2];
}
var result = str.reduce(function (prev, cur, i) {
return prev + cur + (substitutions[i] !== undefined ? substitutions[i] : '');
}, '');
var indents = result.split('\n').filter(function (line) {
return line.trim().length > 0;
}).map(function (line) {
var x = line.match(/^([ \t]+)/);
return x !== null ? x[1].length : 0;
});
var indent = Math.min.apply(Math, _toConsumableArray(indents));
if (indent !== 0) {
result = result.replace(new RegExp(String.raw(_templateObject, indent), 'mg'), '');
}
return result.trim();
};
});
};
module.exports = Mocha.interfaces['mocha-annotated'];
/* eslint-enable no-param-reassign, no-multi-assign */
;