Skip to content

Commit ae23678

Browse files
committed
more async support
1 parent b2330fc commit ae23678

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const valid = [
4949
];
5050
const invalid = [
5151
// Invalid cases that applies to all variants
52-
...["getByText", "getByRole"].map((q) => [
52+
...["getByText", "getAllByRole"].map((q) => [
5353
invalidCase(
5454
`expect(screen.${q}('foo')).toHaveLength(1)`,
5555
`expect(screen.${q}('foo')).toBeInTheDocument()`
@@ -92,7 +92,7 @@ const invalid = [
9292
),
9393
]),
9494
// Invalid cases that applies to queryBy* and queryAllBy*
95-
...["queryByText"].map((q) => [
95+
...["queryByText", "queryAllByText"].map((q) => [
9696
invalidCase(
9797
`expect(${q}('foo')).toHaveLength(0)`,
9898
`expect(${q}('foo')).not.toBeInTheDocument()`
@@ -148,6 +148,14 @@ const invalid = [
148148
expect(await findByRole("button")).toBeInTheDocument();
149149
})`
150150
),
151+
invalidCase(
152+
`it("foo", async () => {
153+
expect(await findByRole("button")).not.toBeNull();
154+
})`,
155+
`it("foo", async () => {
156+
expect(await findByRole("button")).toBeInTheDocument();
157+
})`
158+
),
151159
invalidCase(
152160
`it("foo", async () => {
153161
expect(await screen.findByText(/Compressing video/)).toBeDefined();
@@ -156,6 +164,14 @@ const invalid = [
156164
expect(await screen.findByText(/Compressing video/)).toBeInTheDocument();
157165
})`
158166
),
167+
invalidCase(
168+
`it("foo", async () => {
169+
expect(await screen.findByText(/Compressing video/)).not.toBeDefined();
170+
})`,
171+
`it("foo", async () => {
172+
expect(await screen.findByText(/Compressing video/)).not.toBeInTheDocument();
173+
})`
174+
),
159175
invalidCase(
160176
`it("foo", async () => {
161177
const compressingFeedback = await screen.findByText(/Compressing video/);
@@ -166,6 +182,16 @@ const invalid = [
166182
expect(compressingFeedback).toBeInTheDocument();
167183
});`
168184
),
185+
invalidCase(
186+
`it("foo", async () => {
187+
const compressingFeedback = await screen.findByText(/Compressing video/);
188+
expect(compressingFeedback).not.toBeNull();
189+
});`,
190+
`it("foo", async () => {
191+
const compressingFeedback = await screen.findByText(/Compressing video/);
192+
expect(compressingFeedback).toBeInTheDocument();
193+
});`
194+
),
169195
invalidCase(
170196
`it("foo", async () => {
171197
let compressingFeedback;
@@ -178,6 +204,18 @@ const invalid = [
178204
expect(compressingFeedback).toBeInTheDocument();
179205
});`
180206
),
207+
invalidCase(
208+
`it("foo", async () => {
209+
let compressingFeedback;
210+
compressingFeedback = await screen.findByText(/Compressing video/);
211+
expect(compressingFeedback).not.toBeDefined();
212+
});`,
213+
`it("foo", async () => {
214+
let compressingFeedback;
215+
compressingFeedback = await screen.findByText(/Compressing video/);
216+
expect(compressingFeedback).not.toBeInTheDocument();
217+
});`
218+
),
181219
];
182220

183221
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2017 } });

src/rules/prefer-in-document.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ export const create = (context) => {
109109
}
110110
return {
111111
// expect(<query>).not.<matcher>
112-
[`CallExpression[callee.object.object.callee.name='expect'][callee.object.property.name='not'][callee.property.name=${alternativeMatchers}]`](
112+
[`CallExpression[callee.object.object.callee.name='expect'][callee.object.property.name='not'][callee.property.name=${alternativeMatchers}], CallExpression[callee.object.callee.name='expect'][callee.object.property.name='not'][callee.object.arguments.0.argument.callee.name=${alternativeMatchers}]`](
113113
node
114114
) {
115-
const queryNode = node.callee.object.object.arguments[0].callee;
115+
const arg = node.callee.object.object.arguments[0];
116+
const queryNode =
117+
arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;
116118
const matcherNode = node.callee.property;
117119
const matcherArguments = node.arguments;
118120

0 commit comments

Comments
 (0)