Skip to content

Commit

Permalink
feat: support flagged describe
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Apr 24, 2020
1 parent 64924a0 commit d5a2ee1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 15 deletions.
101 changes: 87 additions & 14 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(stringOrObject) {
let name;
let flags = [];
if (typeof stringOrObject === 'string') {
name = stringOrObject;
} else {
name = stringOrObject.name;
if (stringOrObject.flags) {
flags = stringOrObject.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,6 +67,51 @@ function flaggedTest(total, isDeactivated) {
};
}

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 beforeEach(beforeEachCallback) {
return mochaBeforeEach(function () {
if (isSkipped({ flags, total, isDeactivated })) {
this.skip();
return;
}

return beforeEachCallback();
});
}

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

return afterEachCallback();
});
}

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

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

Expand All @@ -56,6 +120,15 @@ function create(it, flags, isDeactivated) {
return _it;
}

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

let _describe = wrap(describe, _flaggedDescribe);

return _describe;
}

module.exports = {
create,
createDescribe,
};
7 changes: 6 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 {
create: createFlaggedTest,
createDescribe: createFlaggedDescribe,
} = require('./flag');

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

0 comments on commit d5a2ee1

Please sign in to comment.