Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: testing-library/eslint-plugin-testing-library
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.6.0
Choose a base ref
...
head repository: testing-library/eslint-plugin-testing-library
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.7.0
Choose a head ref
  • 2 commits
  • 5 files changed
  • 2 contributors

Commits on Aug 10, 2020

  1. docs: add skovy as a contributor (#213)

    * docs: update README.md [skip ci]
    
    * docs: update .all-contributorsrc [skip ci]
    
    Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
    allcontributors[bot] authored Aug 10, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    609c6db View commit details

Commits on Aug 22, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    dddf405 View commit details
Showing with 75 additions and 5 deletions.
  1. +11 −0 .all-contributorsrc
  2. +2 −1 README.md
  3. +11 −1 docs/rules/prefer-explicit-assert.md
  4. +28 −3 lib/rules/prefer-explicit-assert.ts
  5. +23 −0 tests/lib/rules/prefer-explicit-assert.test.ts
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -326,6 +326,17 @@
"test",
"doc"
]
},
{
"login": "skovy",
"name": "Spencer Miskoviak",
"avatar_url": "https://avatars1.githubusercontent.com/u/5247455?v=4",
"profile": "https://skovy.dev",
"contributions": [
"code",
"test",
"doc"
]
}
],
"contributorsPerLine": 7,
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
[![Tweet][tweet-badge]][tweet-url]

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-30-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-31-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

## Installation
@@ -216,6 +216,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tr>
<td align="center"><a href="https://github.com/codecog"><img src="https://avatars0.githubusercontent.com/u/5106076?v=4" width="100px;" alt=""/><br /><sub><b>Josh Kelly</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-testing-library/commits?author=codecog" title="Code">💻</a></td>
<td align="center"><a href="http://aless.co"><img src="https://avatars0.githubusercontent.com/u/5139846?v=4" width="100px;" alt=""/><br /><sub><b>Alessia Bellisario</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-testing-library/commits?author=alessbell" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-testing-library/commits?author=alessbell" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-testing-library/commits?author=alessbell" title="Documentation">📖</a></td>
<td align="center"><a href="https://skovy.dev"><img src="https://avatars1.githubusercontent.com/u/5247455?v=4" width="100px;" alt=""/><br /><sub><b>Spencer Miskoviak</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-testing-library/commits?author=skovy" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-testing-library/commits?author=skovy" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-testing-library/commits?author=skovy" title="Documentation">📖</a></td>
</tr>
</table>

12 changes: 11 additions & 1 deletion docs/rules/prefer-explicit-assert.md
Original file line number Diff line number Diff line change
@@ -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
31 changes: 28 additions & 3 deletions lib/rules/prefer-explicit-assert.ts
Original file line number Diff line number Diff line change
@@ -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)<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',
},
@@ -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 {
@@ -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,
},
});
}
}
});
},
23 changes: 23 additions & 0 deletions tests/lib/rules/prefer-explicit-assert.test.ts
Original file line number Diff line number Diff line change
@@ -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' },
},
],
},
],
});