Skip to content

Commit

Permalink
feat(CrowdStrike#281): flexible roles helper
Browse files Browse the repository at this point in the history
Supported format:
 - Concatenated string
 - Multiple arguments
  • Loading branch information
MichalBryxi committed Jun 26, 2020
1 parent d45f8a0 commit bb5d19b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
12 changes: 11 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -103,6 +103,16 @@ roles('#my-role-1 #my-role-2', function() {
});
```

or

```js
roles('#my-role-1', '#my-role-2', function() {
it('my test', function() {
// ...
});
});
```

You would get two tests

```
Expand Down
22 changes: 14 additions & 8 deletions packages/mocha/src/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/mocha/test/acceptance/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -128,7 +128,7 @@ describe(function() {
tag: ['!role1'],
});

expect(stats.passes).to.equal(2);
expect(stats.passes).to.equal(5);
});
});

Expand Down
8 changes: 4 additions & 4 deletions packages/mocha/test/fixtures/role-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]/));
});
});
});

0 comments on commit bb5d19b

Please sign in to comment.