From 13a1c4c95988a2a77493e16fe56b83de2048ab23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Beltra=CC=81n=20Alarco=CC=81n?= Date: Tue, 11 Feb 2020 11:02:20 +0100 Subject: [PATCH] fix: false positives for negated matchers --- .../no-get-by-for-checking-element-not-present.md | 2 ++ .../no-get-by-for-checking-element-not-present.js | 11 ++++++++--- package.json | 1 + .../no-get-by-for-checking-element-not-present.js | 6 +++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/rules/no-get-by-for-checking-element-not-present.md b/docs/rules/no-get-by-for-checking-element-not-present.md index 77576d8a..0dd2a354 100644 --- a/docs/rules/no-get-by-for-checking-element-not-present.md +++ b/docs/rules/no-get-by-for-checking-element-not-present.md @@ -23,6 +23,7 @@ Examples of **incorrect** code for this rule: test('some test', () => { const { getByText } = render(); expect(getByText('Foo')).not.toBeInTheDocument(); + expect(getByText('Foo')).not.toBeTruthy(); expect(getByText('Foo')).toBeFalsy(); expect(getByText('Foo')).toBeNull(); }); @@ -41,6 +42,7 @@ Examples of **correct** code for this rule: test('some test', () => { const { getByText } = render(); expect(getByText('Foo')).toBeInTheDocument(); + expect(getByText('Foo')).not.toBeDisabled(); expect(queryByText('Foo')).not.toBeInTheDocument(); expect(queryByText('Foo')).toBeFalsy(); }); diff --git a/lib/rules/no-get-by-for-checking-element-not-present.js b/lib/rules/no-get-by-for-checking-element-not-present.js index 96597d54..3eeaba5a 100644 --- a/lib/rules/no-get-by-for-checking-element-not-present.js +++ b/lib/rules/no-get-by-for-checking-element-not-present.js @@ -2,7 +2,12 @@ const { getDocsUrl } = require('../utils'); -const falsyMatchers = ['toBeNull', 'toBeFalsy']; +const FALSY_MATCHERS = ['toBeNull', 'toBeFalsy']; +const NOT_ALLOWED_NEGATED_MATCHERS = [ + 'toBeInTheDocument', + 'toBeTruthy', + 'toBeDefined', +]; module.exports = { meta: { @@ -34,7 +39,7 @@ module.exports = { if (matcher === 'not') { const negatedMatcher = expectStatement.parent.property.name; - if (!falsyMatchers.includes(negatedMatcher)) { + if (NOT_ALLOWED_NEGATED_MATCHERS.includes(negatedMatcher)) { return context.report({ node, messageId: 'expectQueryBy', @@ -42,7 +47,7 @@ module.exports = { } } - if (falsyMatchers.includes(matcher)) { + if (FALSY_MATCHERS.includes(matcher)) { return context.report({ node, messageId: 'expectQueryBy', diff --git a/package.json b/package.json index 96213c91..359d8cab 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "format": "prettier --write README.md {lib,docs,tests}/**/*.{js,md}", "test:local": "jest", "test:ci": "jest --coverage", + "test:watch": "npm run test:local -- --watch", "test": "is-ci test:ci test:local", "semantic-release": "semantic-release" }, diff --git a/tests/lib/rules/no-get-by-for-checking-element-not-present.js b/tests/lib/rules/no-get-by-for-checking-element-not-present.js index 36d6f66f..fc1ae8dc 100644 --- a/tests/lib/rules/no-get-by-for-checking-element-not-present.js +++ b/tests/lib/rules/no-get-by-for-checking-element-not-present.js @@ -27,7 +27,7 @@ const getInvalidAssertion = (query, matcher) => errors: [{ messageId: 'expectQueryBy' }], })); -ruleTester.run('prefer-expect-query-by', rule, { +ruleTester.run('no-get-by-for-checking-element-not-present', rule, { valid: [ ...getByQueries.reduce( (validRules, queryName) => [ @@ -38,6 +38,8 @@ ruleTester.run('prefer-expect-query-by', rule, { ...getValidAssertion(queryName, '.toEqual("World")'), ...getValidAssertion(queryName, '.not.toBeFalsy()'), ...getValidAssertion(queryName, '.not.toBeNull()'), + ...getValidAssertion(queryName, '.not.toBeDisabled()'), + ...getValidAssertion(queryName, '.not.toHaveClass("btn")'), ], [] ), @@ -47,6 +49,7 @@ ruleTester.run('prefer-expect-query-by', rule, { ...getValidAssertion(queryName, '.not.toBeInTheDocument()'), ...getValidAssertion(queryName, '.toBeNull()'), ...getValidAssertion(queryName, '.not.toBeTruthy()'), + ...getValidAssertion(queryName, '.not.toBeDefined()'), ...getValidAssertion(queryName, '.toBeFalsy()'), { code: `(async () => { @@ -77,6 +80,7 @@ ruleTester.run('prefer-expect-query-by', rule, { ...getInvalidAssertion(queryName, '.not.toBeInTheDocument()'), ...getInvalidAssertion(queryName, '.toBeNull()'), ...getInvalidAssertion(queryName, '.not.toBeTruthy()'), + ...getInvalidAssertion(queryName, '.not.toBeDefined()'), ...getInvalidAssertion(queryName, '.toBeFalsy()'), { code: `(async () => {