Skip to content

Commit

Permalink
feat: add flagged describe
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed May 13, 2020
1 parent 6bc8671 commit 06f5ff9
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 17 deletions.
130 changes: 114 additions & 16 deletions packages/mocha/src/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,46 @@ function areAllFlagsPresent(names, total) {
});
}

function infoFor(stringOrObjectOrCallback) {
let name;
let flags = [];
if (typeof stringOrObjectOrCallback === 'string') {
name = stringOrObjectOrCallback;
} else {
name = stringOrObjectOrCallback.name;
if (stringOrObjectOrCallback.flags) {
flags = stringOrObjectOrCallback.flags;
}
}

return { name, flags };
}

function formatName(name, flags, handler) {
if (flags.length) {
name = formatTitle(name);
name += `${handler.titleSeparator || titleSeparator}flags: ${flags.join(', ')}`;
}

return name;
}


function isSkipped({ flags, total, isDeactivated }) {
return !isDeactivated && flags.length && !areAllFlagsPresent(flags, total);
}

function flaggedTest(total, isDeactivated) {
return it => {
return function flaggedTest(stringOrObject, callback) {
let name;
let flags = [];
if (typeof stringOrObject === 'string') {
name = stringOrObject;
} else {
name = stringOrObject.name;
if (stringOrObject.flags) {
flags = stringOrObject.flags;
}
}
let { name, flags } = infoFor(stringOrObject);

if (!isDeactivated && flags.length) {
name = formatTitle(name);
name += `${it.titleSeparator || titleSeparator}flags: ${flags.join(', ')}`;
if (!isDeactivated) {
name = formatName(name, flags, it);
}

it(name, async function () {
if (!isDeactivated && flags.length && !areAllFlagsPresent(flags, total)) {
if (isSkipped({ flags, total, isDeactivated })) {
this.skip();
return;
}
Expand All @@ -48,14 +67,93 @@ function flaggedTest(total, isDeactivated) {
};
}

function create(it, flags, isDeactivated) {
const mochaBefore = global.before;
const mochaAfter = global.after;
const mochaBeforeEach = global.beforeEach;
const mochaAfterEach = global.afterEach;

function flaggedDescribe(total, isDeactivated) {
return (describe) => {
return function _flaggedDescribe(stringOrObjectOrCallback, callback) {
let { name, flags } = infoFor(stringOrObjectOrCallback);

if (!isDeactivated) {
name = formatName(name, flags, describe);
}

if (!callback) {
callback = stringOrObjectOrCallback;
}

describe(name, function() {
function before(beforeCallback) {
return mochaBefore(function (...args) {
if (isSkipped({ flags, total, isDeactivated })) {
this.skip();
return;
}

return beforeCallback.apply(this, args);
});
}

function after(afterCallback) {
return mochaAfter(function (...args) {
if (isSkipped({ flags, total, isDeactivated })) {
this.skip();
return;
}

return afterCallback.apply(this, args);
});
}

function beforeEach(beforeEachCallback) {
return mochaBeforeEach(function (...args) {
if (isSkipped({ flags, total, isDeactivated })) {
this.skip();
return;
}

return beforeEachCallback.apply(this, args);
});
}

function afterEach(afterEachCallback) {
return mochaAfterEach(function (...args) {
if (isSkipped({ flags, total, isDeactivated })) {
this.skip();
return;
}

return afterEachCallback.apply(this, args);
});
}

callback.apply(this, [{ beforeEach, afterEach, before, after }]);
});
};
};
}

function createIt(it, flags, isDeactivated) {
let _flaggedTest = flaggedTest(flags, isDeactivated);

let _it = wrap(it, _flaggedTest);

return _it;
}

function createDescribe(describe, flags, isDeactivated) {
let _flaggedDescribe = flaggedDescribe(flags, isDeactivated);

let _describe = wrap(describe, _flaggedDescribe);

return _describe;
}

module.exports = {
create,
create: createIt,
createIt: createIt,
createDescribe,
};
8 changes: 7 additions & 1 deletion packages/mocha/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const Mocha = require('mocha');
const { promisify } = require('util');
const glob = promisify(require('glob'));
const { buildGrep } = require('./tag');
const {
createIt: createFlaggedIt,
createDescribe: createFlaggedDescribe,
} = require('./flag');

async function runMocha(mocha) {
let runner;
Expand Down Expand Up @@ -78,5 +82,7 @@ async function runTests({
module.exports = {
runTests,
createRolesHelper: require('./role').create,
createFlaggedTest: require('./flag').create,
createFlaggedTest: createFlaggedIt,
createFlaggedIt: createFlaggedIt,
createFlaggedDescribe,
};

0 comments on commit 06f5ff9

Please sign in to comment.