From dddf40526afaa8f062b84340572ac721d130d571 Mon Sep 17 00:00:00 2001 From: Spencer Miskoviak <5247455+skovy@users.noreply.github.com> Date: Sat, 22 Aug 2020 10:58:30 -0700 Subject: [PATCH] feat(prefer-explicit-assert): add 'assertion' config option (#220) Closes #218 --- docs/rules/prefer-explicit-assert.md | 12 ++++++- lib/rules/prefer-explicit-assert.ts | 31 +++++++++++++++++-- .../lib/rules/prefer-explicit-assert.test.ts | 23 ++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/docs/rules/prefer-explicit-assert.md b/docs/rules/prefer-explicit-assert.md index 34c9bb31..b0a0079f 100644 --- a/docs/rules/prefer-explicit-assert.md +++ b/docs/rules/prefer-explicit-assert.md @@ -50,7 +50,17 @@ getByNonTestingLibraryVariant('foo'); ## Options -This rule accepts a single options argument: +This rule has a few options: + +- `assertion`: this string allows defining the preferred assertion to use + with `getBy*` queries. By default, any assertion is valid (`toBeTruthy`, + `toBeDefined`, etc.). However, they all assert slightly different things. + This option ensures all `getBy*` assertions are consistent and use the same + assertion. + + ```js + "testing-library/prefer-explicit-assert": ["error", {"assertion": "toBeInTheDocument"}], + ``` - `customQueryNames`: this array option allows to extend default Testing Library queries with custom ones for including them into rule diff --git a/lib/rules/prefer-explicit-assert.ts b/lib/rules/prefer-explicit-assert.ts index 86a03586..29b0e954 100644 --- a/lib/rules/prefer-explicit-assert.ts +++ b/lib/rules/prefer-explicit-assert.ts @@ -3,10 +3,13 @@ import { getDocsUrl, ALL_QUERIES_METHODS } from '../utils'; import { isMemberExpression } from '../node-utils'; export const RULE_NAME = 'prefer-explicit-assert'; -export type MessageIds = 'preferExplicitAssert'; +export type MessageIds = + | 'preferExplicitAssert' + | 'preferExplicitAssertAssertion'; type Options = [ { - customQueryNames: string[]; + assertion?: string; + customQueryNames?: string[]; } ]; @@ -34,12 +37,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({ messages: { preferExplicitAssert: 'Wrap stand-alone `getBy*` query with `expect` function for better explicit assertion', + preferExplicitAssertAssertion: + '`getBy*` queries must be asserted with `{{assertion}}`', }, fixable: null, schema: [ { type: 'object', + additionalProperties: false, properties: { + assertion: { + type: 'string', + }, customQueryNames: { type: 'array', }, @@ -54,7 +63,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({ ], create: function(context, [options]) { - const { customQueryNames } = options; + const { customQueryNames, assertion } = options; const getQueryCalls: TSESTree.Identifier[] = []; return { @@ -74,6 +83,22 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({ node: queryCall, messageId: 'preferExplicitAssert', }); + } else if (assertion) { + const expectation = node.parent.parent.parent; + + if ( + expectation.type === 'MemberExpression' && + expectation.property.type === 'Identifier' && + expectation.property.name !== assertion + ) { + context.report({ + node: expectation.property, + messageId: 'preferExplicitAssertAssertion', + data: { + assertion, + }, + }); + } } }); }, diff --git a/tests/lib/rules/prefer-explicit-assert.test.ts b/tests/lib/rules/prefer-explicit-assert.test.ts index 695bdcf0..5715d134 100644 --- a/tests/lib/rules/prefer-explicit-assert.test.ts +++ b/tests/lib/rules/prefer-explicit-assert.test.ts @@ -66,6 +66,14 @@ ruleTester.run(RULE_NAME, rule, { { code: `queryByText("foo")`, }, + { + code: `expect(getByText('foo')).toBeTruthy()`, + options: [ + { + assertion: 'toBeTruthy', + }, + ], + }, ], invalid: [ @@ -132,5 +140,20 @@ ruleTester.run(RULE_NAME, rule, { }, ], }, + { + code: `expect(getByText('foo')).toBeDefined()`, + options: [ + { + assertion: 'toBeInDocument', + }, + ], + errors: [ + { + messageId: 'preferExplicitAssertAssertion', + column: 26, + data: { assertion: 'toBeInDocument' }, + }, + ], + }, ], });