Skip to content

Commit df3dc0c

Browse files
test: refactor @marko/testing-library tests (#582)
1 parent e20f0e4 commit df3dc0c

20 files changed

+1576
-1782
lines changed

tests/create-testing-library-rule.test.ts

+14-28
Original file line numberDiff line numberDiff line change
@@ -442,40 +442,26 @@ ruleTester.run(RULE_NAME, rule, {
442442
},
443443
],
444444
},
445-
{
446-
code: `
445+
...['@testing-library/react', '@marko/testing-library'].map(
446+
(testingFramework) =>
447+
({
448+
code: `
447449
// case: render imported from Testing Library module
448-
import { render } from '@testing-library/react'
450+
import { render } from '${testingFramework}'
449451
import { somethingElse } from 'another-module'
450452
const foo = require('bar')
451453
452454
const utils = render();
453455
`,
454-
errors: [
455-
{
456-
line: 7,
457-
column: 21,
458-
messageId: 'renderError',
459-
},
460-
],
461-
},
462-
{
463-
code: `
464-
// case: render imported from Testing Library module
465-
import { render } from '@marko/testing-library'
466-
import { somethingElse } from 'another-module'
467-
const foo = require('bar')
468-
469-
const utils = render();
470-
`,
471-
errors: [
472-
{
473-
line: 7,
474-
column: 21,
475-
messageId: 'renderError',
476-
},
477-
],
478-
},
456+
errors: [
457+
{
458+
line: 7,
459+
column: 21,
460+
messageId: 'renderError',
461+
},
462+
],
463+
} as const)
464+
),
479465
{
480466
code: `
481467
// case: render imported from Testing Library module (require version)

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

+14-21
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import { createRuleTester } from '../test-utils';
1111

1212
const ruleTester = createRuleTester();
1313

14+
const SUPPORTED_TESTING_FRAMEWORKS = [
15+
'@testing-library/react',
16+
'@marko/testing-library',
17+
];
18+
1419
interface TestCode {
1520
code: string;
1621
isAsync?: boolean;
@@ -325,33 +330,21 @@ ruleTester.run(RULE_NAME, rule, {
325330
],
326331

327332
invalid: [
328-
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
329-
(query) =>
330-
({
331-
code: `// async queries without await operator or then method are not valid
332-
import { render } from '@testing-library/react'
333-
334-
test("An example test", async () => {
335-
doSomething()
336-
const foo = ${query}('foo')
337-
});
338-
`,
339-
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
340-
} as const)
341-
),
342-
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
343-
(query) =>
344-
({
345-
code: `// async queries for @marko/testing-library without await operator or then method are not valid
346-
import { render } from '@marko/testing-library'
333+
...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) =>
334+
ALL_ASYNC_COMBINATIONS_TO_TEST.map(
335+
(query) =>
336+
({
337+
code: `// async queries without await operator or then method are not valid
338+
import { render } from '${testingFramework}'
347339
348340
test("An example test", async () => {
349341
doSomething()
350342
const foo = ${query}('foo')
351343
});
352344
`,
353-
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
354-
} as const)
345+
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
346+
} as const)
347+
)
355348
),
356349
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
357350
(query) =>

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

+43-44
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,54 @@ import { createRuleTester } from '../test-utils';
44

55
const ruleTester = createRuleTester();
66

7+
const SUPPORTED_TESTING_FRAMEWORKS = [
8+
'@testing-library/dom',
9+
'@marko/testing-library',
10+
];
11+
712
ruleTester.run(RULE_NAME, rule, {
8-
valid: [
13+
valid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
914
...ASYNC_UTILS.map((asyncUtil) => ({
1015
code: `
11-
import { ${asyncUtil} } from '@testing-library/dom';
16+
import { ${asyncUtil} } from '${testingFramework}';
1217
test('${asyncUtil} util directly waited with await operator is valid', async () => {
1318
doSomethingElse();
1419
await ${asyncUtil}(() => getByLabelText('email'));
1520
});
1621
`,
1722
})),
18-
1923
...ASYNC_UTILS.map((asyncUtil) => ({
2024
code: `
21-
import { ${asyncUtil} } from '@testing-library/dom';
25+
import { ${asyncUtil} } from '${testingFramework}';
2226
test('${asyncUtil} util promise saved in var and waited with await operator is valid', async () => {
2327
doSomethingElse();
2428
const aPromise = ${asyncUtil}(() => getByLabelText('email'));
2529
await aPromise;
2630
});
2731
`,
2832
})),
29-
3033
...ASYNC_UTILS.map((asyncUtil) => ({
3134
code: `
32-
import { ${asyncUtil} } from '@testing-library/dom';
35+
import { ${asyncUtil} } from '${testingFramework}';
3336
test('${asyncUtil} util directly chained with then is valid', () => {
3437
doSomethingElse();
3538
${asyncUtil}(() => getByLabelText('email')).then(() => { console.log('done') });
3639
});
3740
`,
3841
})),
39-
4042
...ASYNC_UTILS.map((asyncUtil) => ({
4143
code: `
42-
import { ${asyncUtil} } from '@testing-library/dom';
44+
import { ${asyncUtil} } from '${testingFramework}';
4345
test('${asyncUtil} util promise saved in var and chained with then is valid', () => {
4446
doSomethingElse();
4547
const aPromise = ${asyncUtil}(() => getByLabelText('email'));
4648
aPromise.then(() => { console.log('done') });
4749
});
4850
`,
4951
})),
50-
5152
...ASYNC_UTILS.map((asyncUtil) => ({
5253
code: `
53-
import { ${asyncUtil} } from '@testing-library/dom';
54+
import { ${asyncUtil} } from '${testingFramework}';
5455
test('${asyncUtil} util directly returned in arrow function is valid', async () => {
5556
const makeCustomWait = () =>
5657
${asyncUtil}(() =>
@@ -59,10 +60,9 @@ ruleTester.run(RULE_NAME, rule, {
5960
});
6061
`,
6162
})),
62-
6363
...ASYNC_UTILS.map((asyncUtil) => ({
6464
code: `
65-
import { ${asyncUtil} } from '@testing-library/dom';
65+
import { ${asyncUtil} } from '${testingFramework}';
6666
test('${asyncUtil} util explicitly returned in arrow function is valid', async () => {
6767
const makeCustomWait = () => {
6868
return ${asyncUtil}(() =>
@@ -72,10 +72,9 @@ ruleTester.run(RULE_NAME, rule, {
7272
});
7373
`,
7474
})),
75-
7675
...ASYNC_UTILS.map((asyncUtil) => ({
7776
code: `
78-
import { ${asyncUtil} } from '@testing-library/dom';
77+
import { ${asyncUtil} } from '${testingFramework}';
7978
test('${asyncUtil} util returned in regular function is valid', async () => {
8079
function makeCustomWait() {
8180
return ${asyncUtil}(() =>
@@ -85,10 +84,9 @@ ruleTester.run(RULE_NAME, rule, {
8584
});
8685
`,
8786
})),
88-
8987
...ASYNC_UTILS.map((asyncUtil) => ({
9088
code: `
91-
import { ${asyncUtil} } from '@testing-library/dom';
89+
import { ${asyncUtil} } from '${testingFramework}';
9290
test('${asyncUtil} util promise saved in var and returned in function is valid', async () => {
9391
const makeCustomWait = () => {
9492
const aPromise = ${asyncUtil}(() =>
@@ -132,7 +130,7 @@ ruleTester.run(RULE_NAME, rule, {
132130
})),
133131
...ASYNC_UTILS.map((asyncUtil) => ({
134132
code: `
135-
import { ${asyncUtil} } from '@testing-library/dom';
133+
import { ${asyncUtil} } from '${testingFramework}';
136134
test('${asyncUtil} util used in with Promise.all() is valid', async () => {
137135
await Promise.all([
138136
${asyncUtil}(callback1),
@@ -143,7 +141,7 @@ ruleTester.run(RULE_NAME, rule, {
143141
})),
144142
...ASYNC_UTILS.map((asyncUtil) => ({
145143
code: `
146-
import { ${asyncUtil} } from '@testing-library/dom';
144+
import { ${asyncUtil} } from '${testingFramework}';
147145
test('${asyncUtil} util used in with Promise.all() with an await is valid', async () => {
148146
await Promise.all([
149147
await ${asyncUtil}(callback1),
@@ -154,7 +152,7 @@ ruleTester.run(RULE_NAME, rule, {
154152
})),
155153
...ASYNC_UTILS.map((asyncUtil) => ({
156154
code: `
157-
import { ${asyncUtil} } from '@testing-library/dom';
155+
import { ${asyncUtil} } from '${testingFramework}';
158156
test('${asyncUtil} util used in with Promise.all() with ".then" is valid', async () => {
159157
Promise.all([
160158
${asyncUtil}(callback1),
@@ -165,7 +163,7 @@ ruleTester.run(RULE_NAME, rule, {
165163
})),
166164
{
167165
code: `
168-
import { waitFor, waitForElementToBeRemoved } from '@testing-library/dom';
166+
import { waitFor, waitForElementToBeRemoved } from '${testingFramework}';
169167
test('combining different async methods with Promise.all does not throw an error', async () => {
170168
await Promise.all([
171169
waitFor(() => getByLabelText('email')),
@@ -176,7 +174,7 @@ ruleTester.run(RULE_NAME, rule, {
176174
},
177175
{
178176
code: `
179-
import { waitForElementToBeRemoved } from '@testing-library/dom';
177+
import { waitForElementToBeRemoved } from '${testingFramework}';
180178
test('waitForElementToBeRemoved receiving element rather than callback is valid', async () => {
181179
doSomethingElse();
182180
const emailInput = getByLabelText('email');
@@ -186,7 +184,7 @@ ruleTester.run(RULE_NAME, rule, {
186184
},
187185
...ASYNC_UTILS.map((asyncUtil) => ({
188186
code: `
189-
import { ${asyncUtil} } from '@testing-library/dom';
187+
import { ${asyncUtil} } from '${testingFramework}';
190188
test('${asyncUtil} util used in Promise.allSettled + await expression is valid', async () => {
191189
await Promise.allSettled([
192190
${asyncUtil}(callback1),
@@ -197,7 +195,7 @@ ruleTester.run(RULE_NAME, rule, {
197195
})),
198196
...ASYNC_UTILS.map((asyncUtil) => ({
199197
code: `
200-
import { ${asyncUtil} } from '@testing-library/dom';
198+
import { ${asyncUtil} } from '${testingFramework}';
201199
test('${asyncUtil} util used in Promise.allSettled + then method is valid', async () => {
202200
Promise.allSettled([
203201
${asyncUtil}(callback1),
@@ -208,7 +206,7 @@ ruleTester.run(RULE_NAME, rule, {
208206
})),
209207
...ASYNC_UTILS.map((asyncUtil) => ({
210208
code: `
211-
import { ${asyncUtil} } from '@testing-library/dom';
209+
import { ${asyncUtil} } from '${testingFramework}';
212210
213211
function waitForSomethingAsync() {
214212
return ${asyncUtil}(() => somethingAsync())
@@ -230,23 +228,24 @@ ruleTester.run(RULE_NAME, rule, {
230228
})
231229
`,
232230
},
233-
234-
// edge case for coverage
235-
// valid async query usage without any function defined
236-
// so there is no innermost function scope found
237-
`
238-
import { waitFor } from '@testing-library/dom';
239-
test('edge case for no innermost function scope', () => {
240-
const foo = waitFor
241-
})
242-
`,
243-
],
244-
invalid: [
231+
{
232+
// edge case for coverage
233+
// valid async query usage without any function defined
234+
// so there is no innermost function scope found
235+
code: `
236+
import { waitFor } from '${testingFramework}';
237+
test('edge case for no innermost function scope', () => {
238+
const foo = waitFor
239+
})
240+
`,
241+
},
242+
]),
243+
invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
245244
...ASYNC_UTILS.map(
246245
(asyncUtil) =>
247246
({
248247
code: `
249-
import { ${asyncUtil} } from '@testing-library/dom';
248+
import { ${asyncUtil} } from '${testingFramework}';
250249
test('${asyncUtil} util not waited is invalid', () => {
251250
doSomethingElse();
252251
${asyncUtil}(() => getByLabelText('email'));
@@ -266,7 +265,7 @@ ruleTester.run(RULE_NAME, rule, {
266265
(asyncUtil) =>
267266
({
268267
code: `
269-
import { ${asyncUtil} } from '@testing-library/dom';
268+
import { ${asyncUtil} } from '${testingFramework}';
270269
test('${asyncUtil} util not waited is invalid', () => {
271270
doSomethingElse();
272271
const el = ${asyncUtil}(() => getByLabelText('email'));
@@ -286,7 +285,7 @@ ruleTester.run(RULE_NAME, rule, {
286285
(asyncUtil) =>
287286
({
288287
code: `
289-
import * as asyncUtil from '@testing-library/dom';
288+
import * as asyncUtil from '${testingFramework}';
290289
test('asyncUtil.${asyncUtil} util not handled is invalid', () => {
291290
doSomethingElse();
292291
asyncUtil.${asyncUtil}(() => getByLabelText('email'));
@@ -306,7 +305,7 @@ ruleTester.run(RULE_NAME, rule, {
306305
(asyncUtil) =>
307306
({
308307
code: `
309-
import { ${asyncUtil} } from '@testing-library/dom';
308+
import { ${asyncUtil} } from '${testingFramework}';
310309
test('${asyncUtil} util promise saved not handled is invalid', () => {
311310
doSomethingElse();
312311
const aPromise = ${asyncUtil}(() => getByLabelText('email'));
@@ -326,7 +325,7 @@ ruleTester.run(RULE_NAME, rule, {
326325
(asyncUtil) =>
327326
({
328327
code: `
329-
import { ${asyncUtil} } from '@testing-library/dom';
328+
import { ${asyncUtil} } from '${testingFramework}';
330329
test('several ${asyncUtil} utils not handled are invalid', () => {
331330
const aPromise = ${asyncUtil}(() => getByLabelText('username'));
332331
doSomethingElse(aPromise);
@@ -353,7 +352,7 @@ ruleTester.run(RULE_NAME, rule, {
353352
(asyncUtil) =>
354353
({
355354
code: `
356-
import { ${asyncUtil}, render } from '@testing-library/dom';
355+
import { ${asyncUtil}, render } from '${testingFramework}';
357356
358357
function waitForSomethingAsync() {
359358
return ${asyncUtil}(() => somethingAsync())
@@ -400,7 +399,7 @@ ruleTester.run(RULE_NAME, rule, {
400399
(asyncUtil) =>
401400
({
402401
code: `
403-
import { ${asyncUtil}, render } from '@testing-library/dom';
402+
import { ${asyncUtil}, render } from '${testingFramework}';
404403
405404
function waitForSomethingAsync() {
406405
return ${asyncUtil}(() => somethingAsync())
@@ -443,5 +442,5 @@ ruleTester.run(RULE_NAME, rule, {
443442
],
444443
} as const)
445444
),
446-
],
445+
]),
447446
});

0 commit comments

Comments
 (0)