Skip to content

Commit b2330fc

Browse files
committed
support awaits
1 parent 0249708 commit b2330fc

File tree

7 files changed

+70
-29
lines changed

7 files changed

+70
-29
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ learn how: http://kcd.im/pull-request
1818
Relevant code or config
1919

2020
```javascript
21+
2122
```
2223

2324
What you did:

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,19 @@ module.exports = {
100100
🔧 indicates that a rule is fixable.
101101

102102
<!-- __BEGIN AUTOGENERATED TABLE__ -->
103-
Name | 👍 | 🔧 | Description
104-
----- | ----- | ----- | -----
105-
[prefer-checked](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-checked.md) | 👍 | 🔧 | prefer toBeChecked over checking attributes
106-
[prefer-empty](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-empty.md) | 👍 | 🔧 | Prefer toBeEmpty over checking innerHTML
107-
[prefer-enabled-disabled](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-enabled-disabled.md) | 👍 | 🔧 | prefer toBeDisabled or toBeEnabled over checking attributes
108-
[prefer-focus](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-focus.md) | 👍 | 🔧 | prefer toHaveFocus over checking document.activeElement
109-
[prefer-in-document](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-in-document.md) | | 🔧 | Prefer .toBeInTheDocument() in favor of checking the length of the result using .toHaveLength(1)
110-
[prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-required.md) | 👍 | 🔧 | prefer toBeRequired over checking properties
111-
[prefer-to-have-attribute](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-attribute.md) | 👍 | 🔧 | prefer toHaveAttribute over checking getAttribute/hasAttribute
112-
[prefer-to-have-style](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-style.md) | 👍 | 🔧 | prefer toHaveStyle over checking element style
113-
[prefer-to-have-text-content](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-text-content.md) | 👍 | 🔧 | Prefer toHaveTextContent over checking element.textContent
103+
104+
| Name | 👍 | 🔧 | Description |
105+
| ---------------------------------------------------------------------------------------------------------------------------------------------- | --- | --- | ------------------------------------------------------------------------------------------------ |
106+
| [prefer-checked](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-checked.md) | 👍 | 🔧 | prefer toBeChecked over checking attributes |
107+
| [prefer-empty](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-empty.md) | 👍 | 🔧 | Prefer toBeEmpty over checking innerHTML |
108+
| [prefer-enabled-disabled](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-enabled-disabled.md) | 👍 | 🔧 | prefer toBeDisabled or toBeEnabled over checking attributes |
109+
| [prefer-focus](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-focus.md) | 👍 | 🔧 | prefer toHaveFocus over checking document.activeElement |
110+
| [prefer-in-document](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-in-document.md) | | 🔧 | Prefer .toBeInTheDocument() in favor of checking the length of the result using .toHaveLength(1) |
111+
| [prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-required.md) | 👍 | 🔧 | prefer toBeRequired over checking properties |
112+
| [prefer-to-have-attribute](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-attribute.md) | 👍 | 🔧 | prefer toHaveAttribute over checking getAttribute/hasAttribute |
113+
| [prefer-to-have-style](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-style.md) | 👍 | 🔧 | prefer toHaveStyle over checking element style |
114+
| [prefer-to-have-text-content](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-text-content.md) | 👍 | 🔧 | Prefer toHaveTextContent over checking element.textContent |
115+
114116
<!-- __END AUTOGENERATED TABLE__ -->
115117

116118
## Issues

other/MAINTAINING.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,4 @@ necessary by the git commit messages. With this in mind, **please brush up on
6666

6767
Thank you so much for helping to maintain this project!
6868

69-
[commit]:
70-
https://github.com/conventional-changelog-archived-repos/conventional-changelog-angular/blob/ed32559941719a130bb0327f886d6a32a8cbc2ba/convention.md
69+
[commit]: https://github.com/conventional-changelog-archived-repos/conventional-changelog-angular/blob/ed32559941719a130bb0327f886d6a32a8cbc2ba/convention.md

src/__tests__/lib/rules/prefer-in-document.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//------------------------------------------------------------------------------
99

1010
import { RuleTester } from "eslint";
11-
import { queries, queriesByVariant } from "../../../queries";
1211
import * as rule from "../../../rules/prefer-in-document";
1312

1413
//------------------------------------------------------------------------------
@@ -28,7 +27,7 @@ function invalidCase(code, output) {
2827
}
2928

3029
const valid = [
31-
...queries.map((q) => [
30+
...["getByText", "getByRole"].map((q) => [
3231
`expect(screen.${q}('foo')).toBeInTheDocument()`,
3332
`expect(${q}('foo')).toBeInTheDocument()`,
3433
`expect(wrapper.${q}('foo')).toBeInTheDocument()`,
@@ -50,7 +49,7 @@ const valid = [
5049
];
5150
const invalid = [
5251
// Invalid cases that applies to all variants
53-
...queries.map((q) => [
52+
...["getByText", "getByRole"].map((q) => [
5453
invalidCase(
5554
`expect(screen.${q}('foo')).toHaveLength(1)`,
5655
`expect(screen.${q}('foo')).toBeInTheDocument()`
@@ -93,7 +92,7 @@ const invalid = [
9392
),
9493
]),
9594
// Invalid cases that applies to queryBy* and queryAllBy*
96-
...queriesByVariant.query.map((q) => [
95+
...["queryByText"].map((q) => [
9796
invalidCase(
9897
`expect(${q}('foo')).toHaveLength(0)`,
9998
`expect(${q}('foo')).not.toBeInTheDocument()`
@@ -141,9 +140,47 @@ const invalid = [
141140
expect(foo).toBeInTheDocument();`
142141
),
143142
]),
143+
invalidCase(
144+
`it("foo", async () => {
145+
expect(await findByRole("button")).toBeDefined();
146+
})`,
147+
`it("foo", async () => {
148+
expect(await findByRole("button")).toBeInTheDocument();
149+
})`
150+
),
151+
invalidCase(
152+
`it("foo", async () => {
153+
expect(await screen.findByText(/Compressing video/)).toBeDefined();
154+
})`,
155+
`it("foo", async () => {
156+
expect(await screen.findByText(/Compressing video/)).toBeInTheDocument();
157+
})`
158+
),
159+
invalidCase(
160+
`it("foo", async () => {
161+
const compressingFeedback = await screen.findByText(/Compressing video/);
162+
expect(compressingFeedback).toBeDefined();
163+
});`,
164+
`it("foo", async () => {
165+
const compressingFeedback = await screen.findByText(/Compressing video/);
166+
expect(compressingFeedback).toBeInTheDocument();
167+
});`
168+
),
169+
invalidCase(
170+
`it("foo", async () => {
171+
let compressingFeedback;
172+
compressingFeedback = await screen.findByText(/Compressing video/);
173+
expect(compressingFeedback).toBeDefined();
174+
});`,
175+
`it("foo", async () => {
176+
let compressingFeedback;
177+
compressingFeedback = await screen.findByText(/Compressing video/);
178+
expect(compressingFeedback).toBeInTheDocument();
179+
});`
180+
),
144181
];
145182

146-
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
183+
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2017 } });
147184
ruleTester.run("prefer-in-document", rule, {
148185
valid: [].concat(...valid),
149186
invalid: [].concat(...invalid),

src/__tests__/lib/rules/prefer-to-have-style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ruleTester.run("prefer-to-have-style", rule, {
2020
invalid: [
2121
{
2222
code: `expect(a.style).toHaveProperty('transform')`,
23-
errors
23+
errors,
2424
},
2525
{
2626
code: `expect(el.style.foo).toBe("bar")`,

src/queries.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
import { queries as allQueries } from "@testing-library/dom";
22

33
export const queries = Object.keys(allQueries);
4-
5-
export const queriesByVariant = {
6-
query: queries.filter((q) => q.startsWith("query")),
7-
get: queries.filter((q) => q.startsWith("get")),
8-
find: queries.filter((q) => q.startsWith("find")),
9-
};

src/rules/prefer-in-document.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ export const create = (context) => {
8585
let queryNode;
8686
if (init) {
8787
// let foo = screen.<query>();
88-
queryNode = init.callee.property || init.callee;
88+
queryNode =
89+
init.type === "AwaitExpression"
90+
? init.argument.callee.property
91+
: init.callee.property || init.callee;
8992
} else {
9093
// let foo;
9194
// foo = screen.<query>();
@@ -96,7 +99,9 @@ export const create = (context) => {
9699
return;
97100
}
98101
queryNode =
99-
assignmentRef.writeExpr.type === "CallExpression"
102+
assignmentRef.writeExpr.type === "AwaitExpression"
103+
? assignmentRef.writeExpr.argument.callee
104+
: assignmentRef.writeExpr.type === "CallExpression"
100105
? assignmentRef.writeExpr.callee
101106
: assignmentRef.writeExpr;
102107
}
@@ -160,11 +165,14 @@ export const create = (context) => {
160165
matcherArguments,
161166
});
162167
},
168+
// expect(await <query>).<matcher>
163169
// expect(<query>).<matcher>
164-
[`CallExpression[callee.object.callee.name='expect'][callee.property.name=${alternativeMatchers}]`](
170+
[`CallExpression[callee.object.callee.name='expect'][callee.property.name=${alternativeMatchers}], CallExpression[callee.object.callee.name='expect'][callee.object.arguments.0.argument.callee.name=${alternativeMatchers}]`](
165171
node
166172
) {
167-
const queryNode = node.callee.object.arguments[0].callee;
173+
const arg = node.callee.object.arguments[0];
174+
const queryNode =
175+
arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;
168176
const matcherNode = node.callee.property;
169177
const matcherArguments = node.arguments;
170178

0 commit comments

Comments
 (0)