Skip to content

Commit 8ceb80f

Browse files
committed
refactor: declare test cases typings as expected
1 parent 751889b commit 8ceb80f

15 files changed

+1967
-1563
lines changed

tests/lib/rules/await-async-query.test.ts

+99-42
Original file line numberDiff line numberDiff line change
@@ -257,73 +257,126 @@ ruleTester.run(RULE_NAME, rule, {
257257
],
258258

259259
invalid: [
260-
// async queries without await operator or then method are not valid
261-
...createTestCase((query) => ({
262-
code: `
260+
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
261+
(query) =>
262+
({
263+
code: `// async queries without await operator or then method are not valid
264+
import { render } from '@testing-library/react'
265+
266+
test("An example test", async () => {
263267
doSomething()
264268
const foo = ${query}('foo')
269+
});
265270
`,
266-
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
267-
})),
271+
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
272+
} as const)
273+
),
274+
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
275+
(query) =>
276+
({
277+
code: `// async screen queries without await operator or then method are not valid
278+
import { render } from '@testing-library/react'
279+
280+
test("An example test", async () => {
281+
screen.${query}('foo')
282+
});
283+
`,
284+
errors: [
285+
{
286+
messageId: 'awaitAsyncQuery',
287+
line: 5,
288+
column: 16,
289+
data: { name: query },
290+
},
291+
],
292+
} as const)
293+
),
294+
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
295+
(query) =>
296+
({
297+
code: `
298+
import { render } from '@testing-library/react'
268299
269-
// async screen queries without await operator or then method are not valid
270-
...createTestCase((query) => ({
271-
code: `screen.${query}('foo')`,
272-
errors: [{ messageId: 'awaitAsyncQuery', line: 4, column: 14 }],
273-
})),
300+
test("An example test", async () => {
301+
doSomething()
302+
const foo = ${query}('foo')
303+
});
304+
`,
305+
errors: [
306+
{
307+
messageId: 'awaitAsyncQuery',
308+
line: 6,
309+
column: 21,
310+
data: { name: query },
311+
},
312+
],
313+
} as const)
314+
),
315+
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
316+
(query) =>
317+
({
318+
code: `
319+
import { render } from '@testing-library/react'
274320
275-
...createTestCase((query) => ({
276-
code: `
321+
test("An example test", async () => {
277322
const foo = ${query}('foo')
278323
expect(foo).toBeInTheDocument()
279324
expect(foo).toHaveAttribute('src', 'bar');
325+
});
280326
`,
281-
errors: [
282-
{
283-
line: 5,
284-
column: 21,
285-
messageId: 'awaitAsyncQuery',
286-
data: {
287-
name: query,
288-
},
289-
},
290-
],
291-
})),
327+
errors: [
328+
{
329+
messageId: 'awaitAsyncQuery',
330+
line: 5,
331+
column: 21,
332+
data: { name: query },
333+
},
334+
],
335+
} as const)
336+
),
292337

293338
// unresolved async queries are not valid (aggressive reporting)
294-
...ALL_ASYNC_COMBINATIONS_TO_TEST.map((query) => ({
295-
code: `
339+
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
340+
(query) =>
341+
({
342+
code: `
296343
import { render } from "another-library"
297344
298345
test('An example test', async () => {
299346
const example = ${query}("my example")
300347
})
301348
`,
302-
errors: [{ messageId: 'awaitAsyncQuery', line: 5, column: 27 }],
303-
})),
349+
errors: [{ messageId: 'awaitAsyncQuery', line: 5, column: 27 }],
350+
} as const)
351+
),
304352

305353
// unhandled promise from async query function wrapper is invalid
306-
...ALL_ASYNC_COMBINATIONS_TO_TEST.map((query) => ({
307-
code: `
354+
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
355+
(query) =>
356+
({
357+
code: `
308358
function queryWrapper() {
309359
doSomethingElse();
310-
360+
311361
return screen.${query}('foo')
312362
}
313-
363+
314364
test("An invalid example test", () => {
315365
const element = queryWrapper()
316366
})
317-
367+
318368
test("An valid example test", async () => {
319369
const element = await queryWrapper()
320370
})
321371
`,
322-
errors: [{ messageId: 'asyncQueryWrapper', line: 9, column: 27 }],
323-
})),
372+
errors: [{ messageId: 'asyncQueryWrapper', line: 9, column: 27 }],
373+
} as const)
374+
),
324375
// unhandled promise from async query arrow function wrapper is invalid
325-
...ALL_ASYNC_COMBINATIONS_TO_TEST.map((query) => ({
326-
code: `
376+
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
377+
(query) =>
378+
({
379+
code: `
327380
const queryWrapper = () => {
328381
doSomethingElse();
329382
@@ -338,11 +391,14 @@ ruleTester.run(RULE_NAME, rule, {
338391
const element = await queryWrapper()
339392
})
340393
`,
341-
errors: [{ messageId: 'asyncQueryWrapper', line: 9, column: 27 }],
342-
})),
394+
errors: [{ messageId: 'asyncQueryWrapper', line: 9, column: 27 }],
395+
} as const)
396+
),
343397
// unhandled promise implicitly returned from async query arrow function wrapper is invalid
344-
...ALL_ASYNC_COMBINATIONS_TO_TEST.map((query) => ({
345-
code: `
398+
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
399+
(query) =>
400+
({
401+
code: `
346402
const queryWrapper = () => screen.${query}('foo')
347403
348404
test("An invalid example test", () => {
@@ -353,7 +409,8 @@ ruleTester.run(RULE_NAME, rule, {
353409
const element = await queryWrapper()
354410
})
355411
`,
356-
errors: [{ messageId: 'asyncQueryWrapper', line: 5, column: 27 }],
357-
})),
412+
errors: [{ messageId: 'asyncQueryWrapper', line: 5, column: 27 }],
413+
} as const)
414+
),
358415
],
359416
});

tests/lib/rules/await-async-utils.test.ts

+52-31
Original file line numberDiff line numberDiff line change
@@ -242,52 +242,66 @@ ruleTester.run(RULE_NAME, rule, {
242242
`,
243243
],
244244
invalid: [
245-
...ASYNC_UTILS.map((asyncUtil) => ({
246-
code: `
245+
...ASYNC_UTILS.map(
246+
(asyncUtil) =>
247+
({
248+
code: `
247249
import { ${asyncUtil} } from '@testing-library/dom';
248250
test('${asyncUtil} util not waited is invalid', () => {
249251
doSomethingElse();
250252
${asyncUtil}(() => getByLabelText('email'));
251253
});
252254
`,
253-
errors: [{ line: 5, column: 11, messageId: 'awaitAsyncUtil' }],
254-
})),
255-
...ASYNC_UTILS.map((asyncUtil) => ({
256-
code: `
255+
errors: [{ line: 5, column: 11, messageId: 'awaitAsyncUtil' }],
256+
} as const)
257+
),
258+
...ASYNC_UTILS.map(
259+
(asyncUtil) =>
260+
({
261+
code: `
257262
import * as asyncUtil from '@testing-library/dom';
258263
test('asyncUtil.${asyncUtil} util not handled is invalid', () => {
259264
doSomethingElse();
260265
asyncUtil.${asyncUtil}(() => getByLabelText('email'));
261266
});
262267
`,
263-
errors: [{ line: 5, column: 21, messageId: 'awaitAsyncUtil' }],
264-
})),
265-
...ASYNC_UTILS.map((asyncUtil) => ({
266-
code: `
268+
errors: [{ line: 5, column: 21, messageId: 'awaitAsyncUtil' }],
269+
} as const)
270+
),
271+
...ASYNC_UTILS.map(
272+
(asyncUtil) =>
273+
({
274+
code: `
267275
import { ${asyncUtil} } from '@testing-library/dom';
268276
test('${asyncUtil} util promise saved not handled is invalid', () => {
269277
doSomethingElse();
270278
const aPromise = ${asyncUtil}(() => getByLabelText('email'));
271279
});
272280
`,
273-
errors: [{ line: 5, column: 28, messageId: 'awaitAsyncUtil' }],
274-
})),
275-
...ASYNC_UTILS.map((asyncUtil) => ({
276-
code: `
281+
errors: [{ line: 5, column: 28, messageId: 'awaitAsyncUtil' }],
282+
} as const)
283+
),
284+
...ASYNC_UTILS.map(
285+
(asyncUtil) =>
286+
({
287+
code: `
277288
import { ${asyncUtil} } from '@testing-library/dom';
278289
test('several ${asyncUtil} utils not handled are invalid', () => {
279290
const aPromise = ${asyncUtil}(() => getByLabelText('username'));
280291
doSomethingElse(aPromise);
281292
${asyncUtil}(() => getByLabelText('email'));
282293
});
283294
`,
284-
errors: [
285-
{ line: 4, column: 28, messageId: 'awaitAsyncUtil' },
286-
{ line: 6, column: 11, messageId: 'awaitAsyncUtil' },
287-
],
288-
})),
289-
...ASYNC_UTILS.map((asyncUtil) => ({
290-
code: `
295+
errors: [
296+
{ line: 4, column: 28, messageId: 'awaitAsyncUtil' },
297+
{ line: 6, column: 11, messageId: 'awaitAsyncUtil' },
298+
],
299+
} as const)
300+
),
301+
...ASYNC_UTILS.map(
302+
(asyncUtil) =>
303+
({
304+
code: `
291305
import { ${asyncUtil}, render } from '@testing-library/dom';
292306
293307
function waitForSomethingAsync() {
@@ -299,10 +313,13 @@ ruleTester.run(RULE_NAME, rule, {
299313
waitForSomethingAsync()
300314
});
301315
`,
302-
errors: [{ messageId: 'asyncUtilWrapper', line: 10, column: 11 }],
303-
})),
304-
...ASYNC_UTILS.map((asyncUtil) => ({
305-
code: `
316+
errors: [{ messageId: 'asyncUtilWrapper', line: 10, column: 11 }],
317+
} as const)
318+
),
319+
...ASYNC_UTILS.map(
320+
(asyncUtil) =>
321+
({
322+
code: `
306323
import { ${asyncUtil} } from 'some-other-library';
307324
test(
308325
'aggressive reporting - util "${asyncUtil}" which is not related to testing library is invalid',
@@ -311,10 +328,13 @@ ruleTester.run(RULE_NAME, rule, {
311328
${asyncUtil}();
312329
});
313330
`,
314-
errors: [{ line: 7, column: 11, messageId: 'awaitAsyncUtil' }],
315-
})),
316-
...ASYNC_UTILS.map((asyncUtil) => ({
317-
code: `
331+
errors: [{ line: 7, column: 11, messageId: 'awaitAsyncUtil' }],
332+
} as const)
333+
),
334+
...ASYNC_UTILS.map(
335+
(asyncUtil) =>
336+
({
337+
code: `
318338
import * as asyncUtils from 'some-other-library';
319339
test(
320340
'aggressive reporting - util "asyncUtils.${asyncUtil}" which is not related to testing library is invalid',
@@ -323,7 +343,8 @@ ruleTester.run(RULE_NAME, rule, {
323343
asyncUtils.${asyncUtil}();
324344
});
325345
`,
326-
errors: [{ line: 7, column: 22, messageId: 'awaitAsyncUtil' }],
327-
})),
346+
errors: [{ line: 7, column: 22, messageId: 'awaitAsyncUtil' }],
347+
} as const)
348+
),
328349
],
329350
});

0 commit comments

Comments
 (0)