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: add flagged describe #433

Open
wants to merge 3 commits 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) {
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,
};
8 changes: 7 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,7 @@ async function runTests(options) {
module.exports = {
runTests,
createRolesHelper: require('./role').create,
createFlaggedTest: require('./flag').create,
createFlaggedTest: createFlaggedIt,
createFlaggedIt,
createFlaggedDescribe,
};
2 changes: 1 addition & 1 deletion packages/mocha/test/acceptance/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe(function() {
it('works', async function() {
let stats = await this.runTests();

expect(stats.passes).to.equal(1);
expect(stats.passes).to.equal(4);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it was working, I would expect this to be 2, not 4?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So before the tests I added, there was one describe with three specs in there, I suspected it was counting the successful describe group of specs.

describe('flag - it', function() {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be counting passed tests. Before your change, it was counting the test with '!flag1' because no flags were supplied in the test.

});
});

Expand Down
34 changes: 32 additions & 2 deletions packages/mocha/test/fixtures/flag-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const assert = require('assert');
const { createFlaggedTest } = require('../../src');
const { createFlaggedTest, createFlaggedDescribe } = require('../../src');

const it = createFlaggedTest(global, ['flag1']);
const describe = createFlaggedDescribe(global, ['flag1']);

describe('flag', function() {
describe('flag - it', function() {
it({
name: 'test with a flag',
flags: ['flag1'],
Expand All @@ -27,3 +28,32 @@ describe('flag', function() {
assert.ok(true);
});
});

describe('flag - describe', function() {
describe({
name: 'test with a flag',
flags: ['flag1'],
}, function () {
it('works', function () {
assert.ok(true);
});
});

describe({
name: 'test with an inverted flag',
flags: ['!flag1'],
}, function () {
it('works', function () {
assert.ok(true);
});
});

describe({
name: 'test with a missing flag',
flags: ['flag2'],
}, function () {
it('works', function () {
assert.ok(true);
});
});
});