UNPKG

1.33 kBJavaScriptView Raw
1'use strict';
2
3const {visitIf} = require('enhance-visitors');
4const createAvaRule = require('../create-ava-rule');
5const util = require('../util');
6
7const create = context => {
8 const ava = createAvaRule();
9 let testUsed = false;
10 let asyncTest;
11
12 const registerUseOfAwait = () => {
13 if (asyncTest) {
14 testUsed = true;
15 }
16 };
17
18 const isAsync = node => Boolean(node?.async);
19
20 return ava.merge({
21 CallExpression: visitIf([
22 ava.isInTestFile,
23 ava.isTestNode,
24 ])(node => {
25 asyncTest = (isAsync(node.arguments[0]) && node.arguments[0])
26 || (isAsync(node.arguments[1]) && node.arguments[1]);
27 }),
28 AwaitExpression: registerUseOfAwait,
29 YieldExpression: registerUseOfAwait,
30 'ForOfStatement[await=true]': registerUseOfAwait,
31 'CallExpression:exit': visitIf([
32 ava.isInTestFile,
33 ava.isTestNode,
34 ])(() => {
35 if (asyncTest && !testUsed) {
36 context.report({
37 node: asyncTest,
38 loc: {
39 start: asyncTest.loc.start,
40 end: asyncTest.loc.start + 5,
41 },
42 message: 'Function was declared as `async` but doesn\'t use `await`.',
43 });
44 }
45
46 asyncTest = undefined;
47 testUsed = false;
48 }),
49 });
50};
51
52module.exports = {
53 create,
54 meta: {
55 type: 'suggestion',
56 docs: {
57 description: 'Ensure that async tests use `await`.',
58 url: util.getDocsUrl(__filename),
59 },
60 schema: [],
61 },
62};