Skip to content

Commit c2129b2

Browse files
neriyardenMichaelDeBoey
authored andcommitted
feat: create a fixer
1 parent 64341f6 commit c2129b2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lib/rules/await-async-queries.ts

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getFunctionName,
88
getInnermostReturningFunction,
99
getVariableReferences,
10+
isMemberExpression,
1011
isPromiseHandled,
1112
} from '../node-utils';
1213

@@ -34,6 +35,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
3435
asyncQueryWrapper:
3536
'promise returned from `{{ name }}` wrapper over async query must be handled',
3637
},
38+
fixable: 'code',
3739
schema: [],
3840
},
3941
defaultOptions: [],
@@ -82,6 +84,19 @@ export default createTestingLibraryRule<Options, MessageIds>({
8284
node: identifierNode,
8385
messageId: 'awaitAsyncQuery',
8486
data: { name: identifierNode.name },
87+
fix: (fixer) => {
88+
if (
89+
isMemberExpression(identifierNode.parent) &&
90+
ASTUtils.isIdentifier(identifierNode.parent.object) &&
91+
identifierNode.parent.object.name === 'screen'
92+
) {
93+
return fixer.insertTextBefore(
94+
identifierNode.parent,
95+
'await '
96+
);
97+
}
98+
return fixer.insertTextBefore(identifierNode, 'await ');
99+
},
85100
});
86101
return;
87102
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ ruleTester.run(RULE_NAME, rule, {
361361
});
362362
`,
363363
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
364+
output: `// async queries without await operator or then method are not valid
365+
import { render } from '${testingFramework}'
366+
367+
test("An example test", async () => {
368+
doSomething()
369+
const foo = await ${query}('foo')
370+
});
371+
`,
364372
} as const)
365373
)
366374
),
@@ -382,6 +390,13 @@ ruleTester.run(RULE_NAME, rule, {
382390
data: { name: query },
383391
},
384392
],
393+
output: `// async screen queries without await operator or then method are not valid
394+
import { render } from '@testing-library/react'
395+
396+
test("An example test", async () => {
397+
await screen.${query}('foo')
398+
});
399+
`,
385400
} as const)
386401
),
387402
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
@@ -403,6 +418,14 @@ ruleTester.run(RULE_NAME, rule, {
403418
data: { name: query },
404419
},
405420
],
421+
output: `
422+
import { render } from '@testing-library/react'
423+
424+
test("An example test", async () => {
425+
doSomething()
426+
const foo = await ${query}('foo')
427+
});
428+
`,
406429
} as const)
407430
),
408431
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
@@ -440,6 +463,13 @@ ruleTester.run(RULE_NAME, rule, {
440463
})
441464
`,
442465
errors: [{ messageId: 'awaitAsyncQuery', line: 5, column: 27 }],
466+
output: `
467+
import { render } from "another-library"
468+
469+
test('An example test', async () => {
470+
const example = await ${query}("my example")
471+
})
472+
`,
443473
} as const)
444474
),
445475

@@ -517,6 +547,11 @@ ruleTester.run(RULE_NAME, rule, {
517547
})
518548
`,
519549
errors: [{ messageId: 'awaitAsyncQuery', line: 3, column: 25 }],
550+
output: `
551+
test('An invalid example test', () => {
552+
const element = await findByIcon('search')
553+
})
554+
`,
520555
},
521556

522557
{

0 commit comments

Comments
 (0)