Skip to content

Commit

Permalink
Merge pull request #35 from levibuzolic/wildcard-block-matchers
Browse files Browse the repository at this point in the history
Match exact blocks by default, allow prefix matching with the use of a `*` in the block options
  • Loading branch information
levibuzolic authored Jul 21, 2022
2 parents ebeef69 + 11bd494 commit 929d64a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# v3.0.0

## Added

* Block scope matchers can accept a trailing `*` to optionally match blocks by prefix #35

## Breaking

* Block matchers no longer match prefixes of blocks by default, can now be configured via options #35

# v2.6.0

* Disable auto fixing by default, allow it to be optionally enabled. #26
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ This rule supports autofixing only when the `fix` option is set to `true` to avo

Option | Type | Description
---|---|---
`block` | `Array<string>` | Specify the block names that your testing framework uses.<br>Defaults to `["describe", "it", "context", "test", "tape", "fixture", "serial"]`
`block` | `Array<string>` | Specify the block names that your testing framework uses. Add a `*` to the end of any string to enable prefix matching (ie. `test*` will match `testExample.only`)<br>Defaults to `["describe", "it", "context", "test", "tape", "fixture", "serial"]`
`focus` | `Array<string>` | Specify the focus scope that your testing framework uses.<br>Defaults to `["only"]`
`fix` | `boolean` | Enable this rule to auto-fix violations, useful for a pre-commit hook, not recommended for users with auto-fixing enabled in their editor.<br>Defaults to `false`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-no-only-tests",
"version": "2.6.0",
"version": "3.0.0",
"description": "ESLint rule for .only blocks in mocha tests",
"keywords": [
"eslint",
Expand Down
24 changes: 18 additions & 6 deletions rules/no-only-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
// Rule Definition
//------------------------------------------------------------------------------

const BLOCK_DEFAULTS = ['describe', 'it', 'context', 'test', 'tape', 'fixture', 'serial'];
const FOCUS_DEFAULTS = ['only'];
const defaultOptions = {
block: ['describe', 'it', 'context', 'test', 'tape', 'fixture', 'serial'],
focus: ['only'],
fix: false,
};

module.exports = {
meta: {
Expand All @@ -31,26 +34,29 @@ module.exports = {
type: 'string',
},
uniqueItems: true,
default: defaultOptions.block,
},
focus: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
default: defaultOptions.focus,
},
fix: {
type: 'boolean',
default: defaultOptions.fix,
},
},
additionalProperties: false,
},
],
},
create(context) {
const options = context.options[0] || {};
const block = options.block || BLOCK_DEFAULTS;
const focus = options.focus || FOCUS_DEFAULTS;
const options = Object.assign({}, defaultOptions, context.options[0]);
const blocks = options.block || [];
const focus = options.focus || [];
const fix = !!options.fix;

return {
Expand All @@ -62,7 +68,13 @@ module.exports = {
const callPath = getCallPath(node.parent).join('.');

// comparison guarantees that matching is done with the beginning of call path
if (block.find(b => callPath.split(b)[0] === '')) {
if (
blocks.find(block => {
// Allow wildcard tail matching of blocks when ending in a `*`
if (block.endsWith('*')) return callPath.startsWith(block.replace(/\*$/, ''));
return callPath.startsWith(`${block}.`);
})
) {
context.report({
node,
message: callPath + ' not permitted',
Expand Down
7 changes: 7 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ruleTester.run('no-only-tests', rules['no-only-tests'], {
'xtape.only("A tape block", function() {});',
'xtest.only("A test block", function() {});',
'other.only("An other block", function() {});',
'testResource.only("A test resource block", function() {});',
'var args = {only: "test"};',
'it("should pass meta only through", function() {});',
'obscureTestBlock.only("An obscure testing library test works unless options are supplied", function() {});',
Expand Down Expand Up @@ -151,6 +152,12 @@ ruleTester.run('no-only-tests', rules['no-only-tests'], {
output: 'test("An alternative focus function", function() {});',
errors: [{message: 'test.focus not permitted'}],
},
{
options: [{block: ['test*']}],
code: 'testResource.only("A test resource block", function() {});',
output: 'testResource.only("A test resource block", function() {});',
errors: [{message: 'testResource.only not permitted'}],
},
],
});

Expand Down

0 comments on commit 929d64a

Please sign in to comment.