Skip to content

Commit 398632d

Browse files
committed
fix: make generated tests more realistic AST
1 parent 0f50183 commit 398632d

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

tests/lib/rules/prefer-query-matchers.test.ts

+29-20
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,39 @@ type AssertionFnParams = {
2626
options: Options;
2727
};
2828

29+
const wrapExpectInTest = (expectStatement: string) => `
30+
import { render, screen } from '@testing-library/react'
31+
32+
test('a fake test', () => {
33+
render(<Component />)
34+
35+
${expectStatement}
36+
})`;
37+
2938
const getValidAssertions = ({
3039
query,
3140
matcher,
3241
options,
3342
}: AssertionFnParams): RuleValidTestCase[] => {
34-
const code = `expect(${query}('Hello'))${matcher}`;
35-
const screenCode = `expect(screen.${query}('Hello'))${matcher}`;
43+
const expectStatement = `expect(${query}('Hello'))${matcher}`;
44+
const expectScreenStatement = `expect(screen.${query}('Hello'))${matcher}`;
3645
return [
3746
{
38-
name: `${code} with default options of empty validEntries`,
39-
code,
47+
name: `${expectStatement} with default options of empty validEntries`,
48+
code: wrapExpectInTest(expectStatement),
4049
},
4150
{
42-
name: `${code} with provided options`,
43-
code,
51+
name: `${expectStatement} with provided options`,
52+
code: wrapExpectInTest(expectStatement),
4453
options,
4554
},
4655
{
47-
name: `${screenCode} with default options of empty validEntries`,
48-
code: screenCode,
56+
name: `${expectScreenStatement} with default options of empty validEntries`,
57+
code: wrapExpectInTest(expectScreenStatement),
4958
},
5059
{
51-
name: `${screenCode} with provided options`,
52-
code: screenCode,
60+
name: `${expectScreenStatement} with provided options`,
61+
code: wrapExpectInTest(expectScreenStatement),
5362
options,
5463
},
5564
];
@@ -60,8 +69,8 @@ const getInvalidAssertions = ({
6069
matcher,
6170
options,
6271
}: AssertionFnParams): RuleInvalidTestCase[] => {
63-
const code = `expect(${query}('Hello'))${matcher}`;
64-
const screenCode = `expect(screen.${query}('Hello'))${matcher}`;
72+
const expectStatement = `expect(${query}('Hello'))${matcher}`;
73+
const expectScreenStatement = `expect(screen.${query}('Hello'))${matcher}`;
6574
const messageId: MessageIds = 'wrongQueryForMatcher';
6675
const [
6776
{
@@ -70,27 +79,27 @@ const getInvalidAssertions = ({
7079
] = options;
7180
return [
7281
{
73-
name: `${code} with provided options`,
74-
code,
82+
name: `${expectStatement} with provided options`,
83+
code: wrapExpectInTest(expectStatement),
7584
options,
7685
errors: [
7786
{
7887
messageId,
79-
line: 1,
80-
column: 8,
88+
line: 7,
89+
column: 10,
8190
data: { query: validEntry.query, matcher: validEntry.matcher },
8291
},
8392
],
8493
},
8594
{
86-
name: `${screenCode} with provided options`,
87-
code: screenCode,
95+
name: `${expectScreenStatement} with provided options`,
96+
code: wrapExpectInTest(expectScreenStatement),
8897
options,
8998
errors: [
9099
{
91100
messageId,
92-
line: 1,
93-
column: 15,
101+
line: 7,
102+
column: 17,
94103
data: { query: validEntry.query, matcher: validEntry.matcher },
95104
},
96105
],

0 commit comments

Comments
 (0)