File size: 1,552 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict'

module.exports = {
  meta: {
    type: 'problem',
    docs: {
      description: 'disallow using `async`/`await` in Cypress `before` methods',
      category: 'Possible Errors',
      recommended: true,
      url: 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/no-async-before.md',
    },
    schema: [],
    messages: {
      unexpected: 'Avoid using async functions with Cypress before / beforeEach functions',
    },
  },

  create (context) {
    function isBeforeBlock (callExpressionNode) {
      const { type, name } = callExpressionNode.callee

      return type === 'Identifier'
                && name === 'before' || name === 'beforeEach'
    }

    function isBeforeAsync (node) {
      return node.arguments
                && node.arguments.length >= 2
                && node.arguments[1].async === true
    }
    const sourceCode = context.sourceCode ?? context.getSourceCode()

    return {
      Identifier (node) {
        if (node.name === 'cy' || node.name === 'Cypress') {
          const ancestors = sourceCode.getAncestors
            ? sourceCode.getAncestors(node)
            : context.getAncestors()
          const asyncTestBlocks = ancestors
          .filter((n) => n.type === 'CallExpression')
          .filter(isBeforeBlock)
          .filter(isBeforeAsync)

          if (asyncTestBlocks.length >= 1) {
            asyncTestBlocks.forEach((node) => {
              context.report({ node, messageId: 'unexpected' })
            })
          }
        }
      },
    }
  },
}