From 6c3d9c9358a56c33cb5027e4bfe00aabbd47ca8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bryx=C3=AD?= Date: Sat, 9 May 2020 23:36:40 +0100 Subject: [PATCH] feat(#281): flexible roles helper Supported format: - Concatenated string - Multiple arguments --- CONTRIBUTING.md | 22 +++++++++++++++++++- packages/mocha/src/role.js | 22 +++++++++++++------- packages/mocha/test/acceptance/index-test.js | 4 ++-- packages/mocha/test/fixtures/role-test.js | 8 +++---- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0ac1d136c..be9f7a51d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -91,7 +91,7 @@ Tagging tests uses the `#` sigil followed by a kebab case name. Tagging happens ### Roles -Roles are a special type of [tag](#tagging). You invoke them the same way, using the `--tag` option, but they must map to login details contained in `config/`. You tag tests using the `roles` helper. The `roles` helper binds the tests inside it for each role in the string. +Roles are a special type of [tag](#tagging). You invoke them the same way, using the `--tag` option, but they must map to login details contained in `config/`. You tag tests using the `roles` helper. The `roles` helper binds the tests inside it for each role in the string, array of string or first `n` arguments. If you have a scenario like @@ -103,6 +103,26 @@ roles('#my-role-1 #my-role-2', function() { }); ``` +or + +```js +roles(['#my-role-1', '#my-role-2'], function() { + it('my test', function() { + // ... + }); +}); +``` + +or + +```js +roles('#my-role-1', '#my-role-2', function() { + it('my test', function() { + // ... + }); +}); +``` + You would get two tests ``` diff --git a/packages/mocha/src/role.js b/packages/mocha/src/role.js index ac026df3c..9ed860fbe 100644 --- a/packages/mocha/src/role.js +++ b/packages/mocha/src/role.js @@ -7,16 +7,22 @@ const { wrap } = require('./mocha'); function roles(getRole, getFilePathTitle) { return describe => { - return function roles(tags, callback) { - // must be instanced and not inline - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Finding_successive_matches - let regex = /(?:^| )#([^ ]+)/g; + return function roles(...args) { + let callback = args.pop(); + + let roles; + if (args.length > 1) { // roles as multiple arguments + roles = args; + } else if (typeof args[0] === 'string' || args[0] instanceof String) { // roles as concatenated string + // must be instanced and not inline + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Finding_successive_matches + let regex = /(?:^| )#([^ ]+)/g; + let matches = args[0].matchAll(regex); + roles = [...matches].map(i => i[1]); + } function loopRoles() { - // eslint-disable-next-line no-cond-assign - for (let matches; matches = regex.exec(tags);) { - let role = matches[1]; - + for (let role of roles) { describe(`#${role}`, function() { before(function() { this.role = getRole(role); diff --git a/packages/mocha/test/acceptance/index-test.js b/packages/mocha/test/acceptance/index-test.js index dd584246b..7da5572f8 100644 --- a/packages/mocha/test/acceptance/index-test.js +++ b/packages/mocha/test/acceptance/index-test.js @@ -110,7 +110,7 @@ describe(function() { globs, }); - expect(stats.passes).to.equal(3); + expect(stats.passes).to.equal(6); }); it('works with a role', async function() { @@ -128,7 +128,7 @@ describe(function() { tag: ['!role1'], }); - expect(stats.passes).to.equal(2); + expect(stats.passes).to.equal(5); }); }); diff --git a/packages/mocha/test/fixtures/role-test.js b/packages/mocha/test/fixtures/role-test.js index 6c8e3ace9..bd475aeaa 100644 --- a/packages/mocha/test/fixtures/role-test.js +++ b/packages/mocha/test/fixtures/role-test.js @@ -12,15 +12,15 @@ describe('role', function() { }); }); - roles('#role2', function() { + roles('#role2 #role3', function() { it('works', function() { - assert.strictEqual(this.role, 'role2'); + assert.ok(this.role.match(/role[2|3]/)); }); }); - roles('#role3', function() { + roles('#role4', '#role5', '#role6', function() { it('works', function() { - assert.strictEqual(this.role, 'role3'); + assert.ok(this.role.match(/role[4|5|6]/)); }); }); });