-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (62 loc) · 1.91 KB
/
index.js
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
/// <reference types="Cypress" />
module.exports = {
plan(n) {
// TODO check if the number was set already
cy.state('expectAssertions', n)
},
}
const shouldOverwrite = (_should, ...args) => {
// console.log(...args)
if (!Cypress._.isFunction(args[1])) {
// debugger
incrementAssertions()
return _should(...args)
}
const n = getActualAssertions()
return _should(...args).tapCatch(() => {
// restore assertion count to prevent retries
// from incrementing the counter A LOT
setActualAssertions(n)
})
}
Cypress.Commands.overwrite('should', shouldOverwrite)
Cypress.Commands.overwrite('and', shouldOverwrite)
const getActualAssertions = () => {
return cy.state('actualAssertions') || 0
}
const setActualAssertions = (n) => {
cy.state('actualAssertions', n)
}
const incrementAssertions = () => {
const actual = getActualAssertions()
setActualAssertions(actual + 1)
}
// overwrite the global "expect" function
const _expect = expect
expect = (val, message) => {
// console.log('retries', cy.state('runnable'))
incrementAssertions()
return _expect(val, message)
}
afterEach(function () {
if (this.currentTest.state === 'passed') {
// look at expected and actual number of assertions
let assertions = getActualAssertions()
const expectAssertions = cy.state('expectAssertions')
// console.log('%d assertions %d expected', assertions, expectAssertions)
if (
Cypress._.isFinite(expectAssertions) &&
assertions !== expectAssertions
) {
if (assertions < expectAssertions) {
// wait until the number of assertions becomes the expected number
cy.wrap(null, { log: false }).should(() => {
assertions = getActualAssertions()
expect(assertions, 'assertions vs planned').to.equal(expectAssertions)
})
} else {
expect(assertions, 'assertions vs planned').to.equal(expectAssertions)
}
}
}
})