Skip to content

Commit 1c5ab1f

Browse files
committed
test: add tests for addAsyncToFunctionFix rule
1 parent 77ed1eb commit 1c5ab1f

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { createTestingLibraryRule } from '../../../lib/create-testing-library-rule';
2+
import { addAsyncToFunctionFix } from '../../../lib/utils/add-async-fo-function-fix';
3+
import { createRuleTester } from '../test-utils';
4+
5+
import type { TSESTree } from '@typescript-eslint/utils';
6+
7+
type MessageIds = 'alwaysAsync';
8+
9+
const rule = createTestingLibraryRule<[], MessageIds>({
10+
name: __filename,
11+
meta: {
12+
docs: {
13+
recommendedConfig: {
14+
dom: 'error',
15+
angular: 'error',
16+
react: 'error',
17+
vue: 'error',
18+
svelte: 'error',
19+
marko: 'error',
20+
},
21+
description: 'Fake rule for testing addAsyncToFunctionFix',
22+
},
23+
messages: { alwaysAsync: 'Function should be async' },
24+
schema: [],
25+
fixable: 'code',
26+
type: 'problem',
27+
},
28+
defaultOptions: [],
29+
create(context) {
30+
const reportIfNotAsync = (
31+
node:
32+
| TSESTree.FunctionExpression
33+
| TSESTree.FunctionDeclaration
34+
| TSESTree.ArrowFunctionExpression
35+
) => {
36+
if (!node.async) {
37+
context.report({
38+
node,
39+
messageId: 'alwaysAsync',
40+
fix(fixer) {
41+
return addAsyncToFunctionFix(
42+
fixer,
43+
fixer.insertTextBefore(node, ''),
44+
node
45+
);
46+
},
47+
});
48+
}
49+
};
50+
return {
51+
FunctionExpression: reportIfNotAsync,
52+
ArrowFunctionExpression: reportIfNotAsync,
53+
FunctionDeclaration: reportIfNotAsync,
54+
};
55+
},
56+
});
57+
58+
const ruleTester = createRuleTester();
59+
60+
ruleTester.run(addAsyncToFunctionFix.name, rule, {
61+
valid: [
62+
{ code: 'async function foo() {}' },
63+
{ code: 'const bar = async function() {}' },
64+
{
65+
code: `
66+
async function foo(a, b) {
67+
return a + b;
68+
}`,
69+
},
70+
],
71+
invalid: [
72+
{
73+
code: 'const bar = function() {}',
74+
output: 'const bar = async function() {}',
75+
errors: [{ messageId: 'alwaysAsync' }],
76+
},
77+
{
78+
code: 'const bar = () => {}',
79+
output: 'const bar = async () => {}',
80+
errors: [{ messageId: 'alwaysAsync' }],
81+
},
82+
{
83+
code: `
84+
function foo(a, b) {
85+
return a + b;
86+
}`,
87+
output: `
88+
async function foo(a, b) {
89+
return a + b;
90+
}`,
91+
errors: [{ messageId: 'alwaysAsync', line: 1 }],
92+
},
93+
{
94+
code: `
95+
const bar = async function() {}
96+
const foo = function() {}
97+
`,
98+
output: `
99+
const bar = async function() {}
100+
const foo = async function() {}
101+
`,
102+
errors: [{ messageId: 'alwaysAsync', line: 3 }],
103+
},
104+
{
105+
code: `
106+
const bar = function() {}
107+
const foo = function() {}
108+
`,
109+
output: `
110+
const bar = async function() {}
111+
const foo = async function() {}
112+
`,
113+
errors: [
114+
{ messageId: 'alwaysAsync', line: 2 },
115+
{ messageId: 'alwaysAsync', line: 3 },
116+
],
117+
},
118+
],
119+
});

0 commit comments

Comments
 (0)