From 9a2e1a77899f9fbfed6147ed68f48392fe475c26 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Wed, 30 Jun 2021 10:26:13 -0400 Subject: [PATCH] test: improve test coverage of `no-identical-tests` rule --- lib/rules/no-identical-tests.js | 10 +++-- package.json | 4 +- tests/lib/rules/no-identical-tests.js | 54 ++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/lib/rules/no-identical-tests.js b/lib/rules/no-identical-tests.js index 8fb756ed..e32f7188 100644 --- a/lib/rules/no-identical-tests.js +++ b/lib/rules/no-identical-tests.js @@ -21,13 +21,15 @@ module.exports = { }, fixable: 'code', schema: [], + messages: { + identical: 'This test case is identical to another case.', + }, }, create (context) { // ---------------------------------------------------------------------- // Public // ---------------------------------------------------------------------- - const message = 'This test case is identical to another case.'; const sourceCode = context.getSourceCode(); // ---------------------------------------------------------------------- @@ -46,8 +48,8 @@ module.exports = { return sourceCode.getText(testA) === sourceCode.getText(testB); } - const propertiesA = testA.properties || []; - const propertiesB = testB.properties || []; + const propertiesA = testA.properties; + const propertiesB = testB.properties; // if properties length not eq; return false; if (propertiesA.length !== propertiesB.length) { @@ -78,7 +80,7 @@ module.exports = { if (cache.some(item => eq(item, test))) { context.report({ node: test, - message, + messageId: 'identical', fix (fixer) { const start = sourceCode.getTokenBefore(test); const end = sourceCode.getTokenAfter(test); diff --git a/package.json b/package.json index e26a3924..e37c4330 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,9 @@ "eslint-utils": "^2.1.0" }, "nyc": { - "branches": 96, + "branches": 97, "functions": 98, - "lines": 98, + "lines": 99, "statements": 98 }, "devDependencies": { diff --git a/tests/lib/rules/no-identical-tests.js b/tests/lib/rules/no-identical-tests.js index 343c5cfd..d8c7bd74 100644 --- a/tests/lib/rules/no-identical-tests.js +++ b/tests/lib/rules/no-identical-tests.js @@ -12,7 +12,8 @@ const rule = require('../../../lib/rules/no-identical-tests'); const RuleTester = require('eslint').RuleTester; -const ERROR = { message: 'This test case is identical to another case.' }; +const ERROR_OBJECT_TEST = { messageId: 'identical', type: 'ObjectExpression' }; +const ERROR_STRING_TEST = { messageId: 'identical', type: 'Literal' }; // ------------------------------------------------------------------------------ // Tests @@ -46,6 +47,26 @@ ruleTester.run('no-identical-tests', rule, { invalid: [] }); `, + // Object and string test. + ` + new RuleTester().run('foo', bar, { + valid: [ + { code: 'foo' }, + 'foo', + ], + invalid: [] + }); + `, + // One test object with more properties than the other. + ` + new RuleTester().run('foo', bar, { + valid: [ + { code: 'foo' }, + { code: 'foo', options: [{}] }, + ], + invalid: [] + }); + `, ], invalid: [ @@ -67,7 +88,7 @@ ruleTester.run('no-identical-tests', rule, { invalid: [] }); `, - errors: [ERROR], + errors: [ERROR_OBJECT_TEST], }, { code: ` @@ -87,7 +108,7 @@ ruleTester.run('no-identical-tests', rule, { invalid: [] }); `, - errors: [ERROR], + errors: [ERROR_OBJECT_TEST], }, { code: ` @@ -112,7 +133,7 @@ ruleTester.run('no-identical-tests', rule, { ] }); `, - errors: [ERROR, ERROR], + errors: [ERROR_OBJECT_TEST, ERROR_OBJECT_TEST], }, { code: ` @@ -132,7 +153,28 @@ ruleTester.run('no-identical-tests', rule, { invalid: [] }); `, - errors: [ERROR], + errors: [ERROR_OBJECT_TEST], + }, + { + // Empty objects. + code: ` + new RuleTester().run('foo', bar, { + valid: [ + {}, + {}, + ], + invalid: [] + }); + `, + output: ` + new RuleTester().run('foo', bar, { + valid: [ + {}, + ], + invalid: [] + }); + `, + errors: [ERROR_OBJECT_TEST], }, { code: ` @@ -152,7 +194,7 @@ ruleTester.run('no-identical-tests', rule, { invalid: [] }); `, - errors: [ERROR], + errors: [ERROR_STRING_TEST], }, ], });