Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'assertion' config option to prefer-explicit-assert #220

Merged
merged 1 commit into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally didn't have to add the assertion type because the schema wasn't validating excess properties. Technically this could be considered a breaking change if any folks have an invalid configuration, but this is now consistent with other rules (eg: consistent-data-testid.). Thought it was worthwhile to make this change to also catch typos (eg: customQueryName)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, TIL! Definitely worth it. We can consider this a fix, even if it breaks invalid configurations. Potentially, any bugfix can break something.

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' },
},
],
},
],
});