File size: 2,906 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'

const assertionCommands = [
  // assertions
  'should',
  'and',
  'contains',

  // retries until it gets something
  'get',

  // not an assertion, but unlikely to require waiting for render
  'scrollIntoView',
  'scrollTo',
]

module.exports = {
  meta: {
    type: 'problem',
    docs: {
      description: 'require screenshots to be preceded by an assertion',
      category: 'Possible Errors',
      recommended: false,
      url: 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/assertion-before-screenshot.md',
    },
    schema: [],
    messages: {
      unexpected: 'Make an assertion on the page state before taking a screenshot',
    },
  },
  create (context) {
    return {
      CallExpression (node) {
        if (isCallingCyScreenshot(node) && !isPreviousAnAssertion(node)) {
          context.report({ node, messageId: 'unexpected' })
        }
      },
    }
  },
}

function isRootCypress (node) {
  while (node.type === 'CallExpression') {
    if (node.callee.type !== 'MemberExpression') return false

    if (node.callee.object.type === 'Identifier' &&
        node.callee.object.name === 'cy') {
      return true
    }

    node = node.callee.object
  }

  return false
}

function getPreviousInChain (node) {
  return node.type === 'CallExpression' &&
         node.callee.type === 'MemberExpression' &&
         node.callee.object.type === 'CallExpression' &&
         node.callee.object.callee.type === 'MemberExpression' &&
         node.callee.object.callee.property.type === 'Identifier' &&
         node.callee.object.callee.property.name
}

function getCallExpressionCypressCommand (node) {
  return isRootCypress(node) &&
         node.callee.property.type === 'Identifier' &&
         node.callee.property.name
}

function isCallingCyScreenshot (node) {
  return getCallExpressionCypressCommand(node) === 'screenshot'
}

function getPreviousCypressCommand (node) {
  const previousInChain = getPreviousInChain(node)

  if (previousInChain) {
    return previousInChain
  }

  while (node.parent && !node.parent.body) {
    node = node.parent
  }

  if (!node.parent || !node.parent.body) return null

  const body = node.parent.body.type === 'BlockStatement' ? node.parent.body.body : node.parent.body

  const index = body.indexOf(node)

  // in the case of a function declaration it won't be found
  if (index < 0) return null

  if (index === 0) return getPreviousCypressCommand(node.parent)

  const previousStatement = body[index - 1]

  if (previousStatement.type !== 'ExpressionStatement' ||
      previousStatement.expression.type !== 'CallExpression') {
    return null
  }

  return getCallExpressionCypressCommand(previousStatement.expression)
}

function isPreviousAnAssertion (node) {
  const previousCypressCommand = getPreviousCypressCommand(node)

  return assertionCommands.indexOf(previousCypressCommand) >= 0
}