Skip to content

Commit

Permalink
feat(prefer-explicit-assert): add 'assertion' config option (#220)
Browse files Browse the repository at this point in the history
Closes #218
  • Loading branch information
skovy authored Aug 22, 2020
1 parent 609c6db commit dddf405
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
12 changes: 11 additions & 1 deletion docs/rules/prefer-explicit-assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 28 additions & 3 deletions lib/rules/prefer-explicit-assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}
];

Expand Down Expand Up @@ -34,12 +37,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
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',
},
Expand All @@ -54,7 +63,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
],

create: function(context, [options]) {
const { customQueryNames } = options;
const { customQueryNames, assertion } = options;
const getQueryCalls: TSESTree.Identifier[] = [];

return {
Expand All @@ -74,6 +83,22 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
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,
},
});
}
}
});
},
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/prefer-explicit-assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ ruleTester.run(RULE_NAME, rule, {
{
code: `queryByText("foo")`,
},
{
code: `expect(getByText('foo')).toBeTruthy()`,
options: [
{
assertion: 'toBeTruthy',
},
],
},
],

invalid: [
Expand Down Expand Up @@ -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' },
},
],
},
],
});

0 comments on commit dddf405

Please sign in to comment.