From 3354c54b7706945136d876bfdbdec7678d04b61b Mon Sep 17 00:00:00 2001 From: Levi Buzolic Date: Wed, 4 Dec 2019 12:19:02 +1100 Subject: [PATCH] Add support for defining 2 levels deep in blocks (ie. `ava.default`) --- CHANGELOG.md | 6 +++- package.json | 2 +- rules/no-only-tests.js | 27 +++++++++++---- tests.js | 77 +++++++++++++++++++++++++----------------- 4 files changed, 72 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c48135..025bd1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,11 @@ +# v2.4.0 + + * Add support for defining 2 levels deep in blocks (ie. `ava.default`) + # v2.3.1 * Bump js-yaml from 3.13.0 to 3.13.1 due to security vulnerability - #11 - + # v2.3.0 * Allow test block names to be specified in options - #10 diff --git a/package.json b/package.json index 2bd0ee5..ce3b681 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-no-only-tests", - "version": "2.3.1", + "version": "2.4.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 c115086..85fcb56 100644 --- a/rules/no-only-tests.js +++ b/rules/no-only-tests.js @@ -49,15 +49,28 @@ module.exports = { return { Identifier(node) { - if ( - focus.indexOf(node.name) != -1 && - node.parent && - node.parent.object && - block.indexOf(node.parent.object.name) != -1 - ) { - context.report(node, node.parent.object.name + '.' + node.name + ' not permitted'); + var parentObject = node.parent && node.parent.object; + if (parentObject == null) return; + if (focus.indexOf(node.name) === -1) return; + + var parentName = parentObject.name; + + if (parentName != null && block.indexOf(parentName) != -1) { + context.report(node, parentName + '.' + node.name + ' not permitted'); + } + + var parentParentName = dotName(parentObject); + + if (parentParentName != null && block.indexOf(parentParentName) != -1) { + context.report(node, parentParentName + '.' + node.name + ' not permitted'); } }, }; }, }; + +function dotName(object) { + if (object.property && object.property.name && object.object && object.object.name) + return object.object.name + '.' + object.property.name; + return null; +} diff --git a/tests.js b/tests.js index 9ed1ba9..d760cd9 100644 --- a/tests.js +++ b/tests.js @@ -22,39 +22,54 @@ ruleTester.run('no-only-tests', rules['no-only-tests'], { { options: [{focus: ['focus']}], code: 'test.only("Options will exclude this from being caught", function() {});', - } + }, ], - invalid: [{ - code: 'describe.only("Some describe block", function() {});', - errors: [{message: 'describe.only not permitted'}] - }, { - code: 'it.only("Some assertion", function() {});', - errors: [{message: 'it.only not permitted'}] - }, { - code: 'context.only("Some context", function() {});', - errors: [{message: 'context.only not permitted'}] - }, { - code: 'test.only("Some test", function() {});', - errors: [{message: 'test.only not permitted'}] - }, { - code: 'tape.only("A tape", function() {});', - errors: [{message: 'tape.only not permitted'}] - }, { - code: 'fixture.only("A fixture", function() {});', - errors: [{message: 'fixture.only not permitted'}] - }, { - code: 'serial.only("A serial test", function() {});', - errors: [{message: 'serial.only not permitted'}] - }, { - options: [{block: ['obscureTestBlock']}], - code: 'obscureTestBlock.only("An obscure testing library test", function() {});', - errors: [{message: 'obscureTestBlock.only not permitted'}] - }, { - options: [{focus: ['focus']}], - code: 'test.focus("An alternative focus function", function() {});', - errors: [{message: 'test.focus not permitted'}] - }] + invalid: [ + { + code: 'describe.only("Some describe block", function() {});', + errors: [{message: 'describe.only not permitted'}], + }, + { + code: 'it.only("Some assertion", function() {});', + errors: [{message: 'it.only not permitted'}], + }, + { + code: 'context.only("Some context", function() {});', + errors: [{message: 'context.only not permitted'}], + }, + { + code: 'test.only("Some test", function() {});', + errors: [{message: 'test.only not permitted'}], + }, + { + code: 'tape.only("A tape", function() {});', + errors: [{message: 'tape.only not permitted'}], + }, + { + code: 'fixture.only("A fixture", function() {});', + errors: [{message: 'fixture.only not permitted'}], + }, + { + code: 'serial.only("A serial test", function() {});', + errors: [{message: 'serial.only not permitted'}], + }, + { + options: [{block: ['obscureTestBlock']}], + code: 'obscureTestBlock.only("An obscure testing library test", function() {});', + errors: [{message: 'obscureTestBlock.only not permitted'}], + }, + { + options: [{block: ['ava.default']}], + code: 'ava.default.only("Block with dot", function() {});', + errors: [{message: 'ava.default.only not permitted'}], + }, + { + options: [{focus: ['focus']}], + code: 'test.focus("An alternative focus function", function() {});', + errors: [{message: 'test.focus not permitted'}], + }, + ], }); console.log('Tests completed successfully');