Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support flagged describe #284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 123 additions & 18 deletions packages/mocha/src/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,48 @@ function areAllFlagsPresent(names, total) {
});
}

function flaggedTest(total, isDeactivated) {
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 flaggedIt(total, isDeactivated) {
return mocha => {
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;
}
}
return function flaggedIt(stringOrObject, callback) {
let { name, flags } = infoFor(stringOrObject);

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

name = formatName(name, flags, mocha.it);

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

function create(mocha, flags, isDeactivated) {
let _flaggedTest = flaggedTest(flags, isDeactivated);
function flaggedDescribe(total, isDeactivated) {
return mocha => {
return function flaggedDescribe(stringOrObjectOrCallback, callback) {
let { name, flags } = infoFor(stringOrObjectOrCallback);

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

if (!callback) {
callback = stringOrObjectOrCallback;
}

let describeArgs = [];

if (name) {
describeArgs.push(name);
}

function anonymousDescribe() {
function before(beforeCallback) {
return mocha.before(function (...args) {
if (isSkipped({ flags, total, isDeactivated })) {
this.skip();
return;
}

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

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

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

function beforeEach(beforeEachCallback) {
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved
return mocha.beforeEach(function (...args) {
if (isSkipped({ flags, total, isDeactivated })) {
this.skip();
return;
}

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

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

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

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

}

describeArgs.push(anonymousDescribe);

mocha.describe(...describeArgs);
};
};
}

function createIt(mocha, flags, isDeactivated) {
let _flaggedTest = flaggedIt(flags, isDeactivated);

let _it = wrap(mocha, 'it', _flaggedTest);

return _it;
}

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

let _describe = wrap(mocha, 'describe', _flaggedDescribe);

return _describe;
}

module.exports = {
create,
createIt,
createDescribe,
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved
};
7 changes: 6 additions & 1 deletion packages/mocha/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const glob = promisify(require('glob'));
const { buildGrep } = require('./tag');
const failureArtifacts = require('./failure-artifacts');
const debug = require('./debug');
const {
createIt: createFlaggedIt,
createDescribe: createFlaggedDescribe,
} = require('./flag');

const { Runner } = Mocha;
const { constants } = Runner;
Expand Down Expand Up @@ -157,5 +161,6 @@ async function runTests(options) {
module.exports = {
runTests,
createRolesHelper: require('./role').create,
createFlaggedTest: require('./flag').create,
createFlaggedIt,
createFlaggedDescribe,
};