Skip to content

Commit dddf405

Browse files
authored
feat(prefer-explicit-assert): add 'assertion' config option (#220)
Closes #218
1 parent 609c6db commit dddf405

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

docs/rules/prefer-explicit-assert.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ getByNonTestingLibraryVariant('foo');
5050

5151
## Options
5252

53-
This rule accepts a single options argument:
53+
This rule has a few options:
54+
55+
- `assertion`: this string allows defining the preferred assertion to use
56+
with `getBy*` queries. By default, any assertion is valid (`toBeTruthy`,
57+
`toBeDefined`, etc.). However, they all assert slightly different things.
58+
This option ensures all `getBy*` assertions are consistent and use the same
59+
assertion.
60+
61+
```js
62+
"testing-library/prefer-explicit-assert": ["error", {"assertion": "toBeInTheDocument"}],
63+
```
5464

5565
- `customQueryNames`: this array option allows to extend default Testing
5666
Library queries with custom ones for including them into rule

lib/rules/prefer-explicit-assert.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import { getDocsUrl, ALL_QUERIES_METHODS } from '../utils';
33
import { isMemberExpression } from '../node-utils';
44

55
export const RULE_NAME = 'prefer-explicit-assert';
6-
export type MessageIds = 'preferExplicitAssert';
6+
export type MessageIds =
7+
| 'preferExplicitAssert'
8+
| 'preferExplicitAssertAssertion';
79
type Options = [
810
{
9-
customQueryNames: string[];
11+
assertion?: string;
12+
customQueryNames?: string[];
1013
}
1114
];
1215

@@ -34,12 +37,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
3437
messages: {
3538
preferExplicitAssert:
3639
'Wrap stand-alone `getBy*` query with `expect` function for better explicit assertion',
40+
preferExplicitAssertAssertion:
41+
'`getBy*` queries must be asserted with `{{assertion}}`',
3742
},
3843
fixable: null,
3944
schema: [
4045
{
4146
type: 'object',
47+
additionalProperties: false,
4248
properties: {
49+
assertion: {
50+
type: 'string',
51+
},
4352
customQueryNames: {
4453
type: 'array',
4554
},
@@ -54,7 +63,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5463
],
5564

5665
create: function(context, [options]) {
57-
const { customQueryNames } = options;
66+
const { customQueryNames, assertion } = options;
5867
const getQueryCalls: TSESTree.Identifier[] = [];
5968

6069
return {
@@ -74,6 +83,22 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
7483
node: queryCall,
7584
messageId: 'preferExplicitAssert',
7685
});
86+
} else if (assertion) {
87+
const expectation = node.parent.parent.parent;
88+
89+
if (
90+
expectation.type === 'MemberExpression' &&
91+
expectation.property.type === 'Identifier' &&
92+
expectation.property.name !== assertion
93+
) {
94+
context.report({
95+
node: expectation.property,
96+
messageId: 'preferExplicitAssertAssertion',
97+
data: {
98+
assertion,
99+
},
100+
});
101+
}
77102
}
78103
});
79104
},

tests/lib/rules/prefer-explicit-assert.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ ruleTester.run(RULE_NAME, rule, {
6666
{
6767
code: `queryByText("foo")`,
6868
},
69+
{
70+
code: `expect(getByText('foo')).toBeTruthy()`,
71+
options: [
72+
{
73+
assertion: 'toBeTruthy',
74+
},
75+
],
76+
},
6977
],
7078

7179
invalid: [
@@ -132,5 +140,20 @@ ruleTester.run(RULE_NAME, rule, {
132140
},
133141
],
134142
},
143+
{
144+
code: `expect(getByText('foo')).toBeDefined()`,
145+
options: [
146+
{
147+
assertion: 'toBeInDocument',
148+
},
149+
],
150+
errors: [
151+
{
152+
messageId: 'preferExplicitAssertAssertion',
153+
column: 26,
154+
data: { assertion: 'toBeInDocument' },
155+
},
156+
],
157+
},
135158
],
136159
});

0 commit comments

Comments
 (0)