]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/require-await.js
4ed598dc86c35db7a800fbfc1f1522526c1473fe
[pve-eslint.git] / eslint / tests / lib / rules / require-await.js
1 /**
2 * @fileoverview Tests for require-await rule.
3 * @author Toru Nagashima
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/require-await"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({
20 parserOptions: {
21 ecmaVersion: 2018
22 }
23 });
24
25 ruleTester.run("require-await", rule, {
26 valid: [
27 "async function foo() { await doSomething() }",
28 "(async function() { await doSomething() })",
29 "async () => { await doSomething() }",
30 "async () => await doSomething()",
31 "({ async foo() { await doSomething() } })",
32 "class A { async foo() { await doSomething() } }",
33 "(class { async foo() { await doSomething() } })",
34 "async function foo() { await async () => { await doSomething() } }",
35
36 // empty functions are ok.
37 "async function foo() {}",
38 "async () => {}",
39
40 // normal functions are ok.
41 "function foo() { doSomething() }",
42
43 // for-await-of
44 "async function foo() { for await (x of xs); }",
45
46 // global await
47 {
48 code: "await foo()",
49 parser: require.resolve("../../fixtures/parsers/typescript-parsers/global-await")
50 },
51 {
52 code: `
53 for await (let num of asyncIterable) {
54 console.log(num);
55 }
56 `,
57 parser: require.resolve("../../fixtures/parsers/typescript-parsers/global-for-await-of")
58 },
59 {
60 code: "async function* run() { yield * anotherAsyncGenerator() }",
61 parserOptions: { ecmaVersion: 9 }
62 },
63 {
64 code: `async function* run() {
65 await new Promise(resolve => setTimeout(resolve, 100));
66 yield 'Hello';
67 console.log('World');
68 }
69 `,
70 parserOptions: { ecmaVersion: 9 }
71 },
72 {
73 code: "async function* run() { }",
74 parserOptions: { ecmaVersion: 9 }
75 },
76 {
77 code: "const foo = async function *(){}",
78 parserOptions: { ecmaVersion: 9 }
79 },
80 {
81 code: 'const foo = async function *(){ console.log("bar") }',
82 parserOptions: { ecmaVersion: 9 }
83 },
84 {
85 code: 'async function* run() { console.log("bar") }',
86 parserOptions: { ecmaVersion: 9 }
87 }
88
89 ],
90 invalid: [
91 {
92 code: "async function foo() { doSomething() }",
93 errors: [{
94 messageId: "missingAwait",
95 data: { name: "Async function 'foo'" }
96 }]
97 },
98 {
99 code: "(async function() { doSomething() })",
100 errors: [{
101 messageId: "missingAwait",
102 data: { name: "Async function" }
103 }]
104 },
105 {
106 code: "async () => { doSomething() }",
107 errors: [{
108 messageId: "missingAwait",
109 data: { name: "Async arrow function" }
110 }]
111 },
112 {
113 code: "async () => doSomething()",
114 errors: [{
115 messageId: "missingAwait",
116 data: { name: "Async arrow function" }
117 }]
118 },
119 {
120 code: "({ async foo() { doSomething() } })",
121 errors: [{
122 messageId: "missingAwait",
123 data: { name: "Async method 'foo'" }
124 }]
125 },
126 {
127 code: "class A { async foo() { doSomething() } }",
128 errors: [{
129 messageId: "missingAwait",
130 data: { name: "Async method 'foo'" }
131 }]
132 },
133 {
134 code: "(class { async foo() { doSomething() } })",
135 errors: [{
136 messageId: "missingAwait",
137 data: { name: "Async method 'foo'" }
138 }]
139 },
140 {
141 code: "(class { async ''() { doSomething() } })",
142 errors: [{
143 messageId: "missingAwait",
144 data: { name: "Async method ''" }
145 }]
146 },
147 {
148 code: "async function foo() { async () => { await doSomething() } }",
149 errors: [{
150 messageId: "missingAwait",
151 data: { name: "Async function 'foo'" }
152 }]
153 },
154 {
155 code: "async function foo() { await async () => { doSomething() } }",
156 errors: [{
157 messageId: "missingAwait",
158 data: { name: "Async arrow function" }
159 }]
160 }
161 ]
162 });