-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
77 lines (66 loc) · 1.85 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
75
76
77
const spok = require('@bahmutov/spok').default
const stripAnsi = require('strip-ansi')
if (!Cypress) {
throw new Error('Missing Cypress global')
}
if (!Cypress._) {
throw new Error('Missing Cypress._ property')
}
if (!Cypress._.isEqual) {
throw new Error('Missing Cypress._.isEqual method')
}
spok.color = false
spok.printDescription = false
class Assert {
constructor() {
this.failed = []
this.passed = []
}
equal(actual, expected, msg) {
if (actual !== expected) {
this.failed.push(msg)
} else {
this.passed.push(msg)
}
}
deepEqual(actual, expected, msg) {
const pass = Cypress._.isEqual(actual, expected)
if (!pass) {
this.failed.push(msg)
} else {
this.passed.push(msg)
}
}
}
const spokHelper = (expectation) => {
return function (value) {
const assert = new Assert()
spok(assert, value, expectation)
// by default, Chai assertions will print actual and expected values
// but Spok already gives us the complete error message
// we overwrite util.getMessage to simply return Spok's message
// Make sure to restore the original getMessage in all circumstances!
const chaiUtilGetMessage = chai.util.getMessage
try {
chai.util.getMessage = function (assert, args) {
return assert.__flags.message
}
assert.passed.forEach((message) => {
const msg = stripAnsi(message)
// create passing assertion in the Command Log
expect(true, msg).to.be.true
})
assert.failed.forEach((message) => {
const msg = stripAnsi(message)
// create failing assertion in the Command Log
expect(true, msg).to.be.false
})
} finally {
chai.util.getMessage = chaiUtilGetMessage
}
}
}
// adds all spok conditions
Object.assign(spokHelper, spok)
spokHelper.spok = spok
module.exports = spokHelper