From 11bd49458b6e379897fd5e2babaadf3f5eafeeef Mon Sep 17 00:00:00 2001 From: Levi Buzolic Date: Thu, 21 Jul 2022 16:41:42 +1000 Subject: [PATCH] Only match block prefixes when using a wildcard (`*`) character --- CHANGELOG.md | 10 ++++++++++ README.md | 2 +- package.json | 2 +- rules/no-only-tests.js | 24 ++++++++++++++++++------ tests.js | 7 +++++++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf2b1a4..b60b359 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index c279639..39c3cbb 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,6 @@ This rule supports autofixing only when the `fix` option is set to `true` to avo Option | Type | Description ---|---|--- -`block` | `Array` | Specify the block names that your testing framework uses.
Defaults to `["describe", "it", "context", "test", "tape", "fixture", "serial"]` +`block` | `Array` | 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`)
Defaults to `["describe", "it", "context", "test", "tape", "fixture", "serial"]` `focus` | `Array` | Specify the focus scope that your testing framework uses.
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.
Defaults to `false` diff --git a/package.json b/package.json index 14896ee..48b7d13 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/rules/no-only-tests.js b/rules/no-only-tests.js index 324a752..fd80585 100644 --- a/rules/no-only-tests.js +++ b/rules/no-only-tests.js @@ -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: { @@ -31,6 +34,7 @@ module.exports = { type: 'string', }, uniqueItems: true, + default: defaultOptions.block, }, focus: { type: 'array', @@ -38,9 +42,11 @@ module.exports = { type: 'string', }, uniqueItems: true, + default: defaultOptions.focus, }, fix: { type: 'boolean', + default: defaultOptions.fix, }, }, additionalProperties: false, @@ -48,9 +54,9 @@ module.exports = { ], }, 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 { @@ -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', diff --git a/tests.js b/tests.js index 6821377..7463e47 100644 --- a/tests.js +++ b/tests.js @@ -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() {});', @@ -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'}], + }, ], });