forked from bahmutov/cypress-failed-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (198 loc) · 6 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/// <reference types="cypress" />
'use strict'
const kebabCase = require('lodash.kebabcase')
const deburr = require('lodash.deburr')
const reject = require('lodash.reject')
const path = require('path')
const cleanupFilename = s => kebabCase(deburr(s))
const getFilepath = filename => path.join('cypress', 'logs', filename)
const useSingleQuotes = s => Cypress._.replace(
Cypress._.replace(s, /'/g, "\\'"),
/"/g, "'"
)
function writeFailedTestInfo ({
specName,
title, suiteName, testName,
testError, testCommands, screenshot}) {
const info = {
specName,
title,
suiteName,
testName,
testError,
testCommands,
screenshot
}
const str = JSON.stringify(info, null, 2) + '\n'
const cleaned = cleanupFilename(testName)
const filename = `failed-${cleaned}.json`
const filepath = getFilepath(filename)
cy.writeFile(filepath, str)
.log('saved failed test information')
// work around shell ENOENT failure in CI container
// const runCmd = `npm run failed-test -- ${filename}`
// pass filename as environment variable
// try discovering the shell script filename
const candidates = [
'./node_modules/cypress-failed-log/on-failed.sh',
'./on-failed.sh'
]
const options = {
failOnNonZeroExit: false,
env: {
FAILED_FILENAME: filepath
}
}
function onFailedExec (result) {
console.log('running cy.exec has failed')
console.log(result)
cy.log(JSON.stringify(result))
const failedExecFilepath = getFilepath('failed-exec.json')
cy.writeFile(failedExecFilepath, JSON.stringify(result, null, 2))
}
cy.exec(candidates[0], options)
.then(result => {
if (result.code) {
onFailedExec(result)
return cy.exec(candidates[1], options)
} else {
console.log('ran npm command successfully', candidates[0])
return result
}
})
.then(result => {
if (result.code) {
onFailedExec(result)
}
})
// .log('ran "npm run failed-test" with the failed test filename', filepath)
.then(result => {
console.log('exec output')
console.log(result)
cy.log(result.stdout)
})
}
var loggedCommands = []
const stringify = x => useSingleQuotes(JSON.stringify(x))
const isSimple = x =>
Cypress._.isString(x) ||
Cypress._.isNumber(x) ||
Cypress._.isPlainObject(x)
function startLogging () {
console.log('Will log Cypress commands')
// should we use command:start or command:end
// or combination of both to keep track?
// hmm, not every command seems to show up in command:end
Cypress.on('command:end', ({attributes}) => {
const str = attributes.name + ' ' + attributes.args.map(stringify).join(' ')
if (isSimple(attributes.subject)) {
try {
const s = stringify(attributes.subject)
loggedCommands.push(s + ' ' + str)
} catch (e) {
// if subject is complex (like Window or circular element)
// use just name and arguments
console.error('could not convert subject', attributes.subject)
console.error('for command', attributes)
loggedCommands.push(str)
}
} else {
loggedCommands.push(str)
}
})
}
function initLog () {
loggedCommands = []
}
function duplicate (s, k, collection) {
if (k === 0) {
return
}
return s === collection[k - 1]
}
// const describeCommand = c => `${c.name} ${c.message}`.trim()
const notEmpty = c => c
function onFailed () {
if (this.currentTest.state === 'passed') {
return
}
const testName = this.currentTest.fullTitle()
// prevents processing failed test twice - from our "afterEach" callback
// and from wrapping user "afterEach"
if (hasSeen(testName)) {
return
}
doneWithTest(testName)
const title = this.currentTest.title
const screenshotName = `${cleanupFilename(title)}-failed`
cy.wait(1000)
.log('waited for UI before capturing screenshot')
cy.screenshot(screenshotName)
cy.wait(1000)
const suiteName = this.currentTest.parent &&
this.currentTest.parent.title
const testError = this.currentTest.err.message
const commands = loggedCommands
// sometimes the message is the same, since the log command events
// repeat when state changes (command starts, runs, etc)
// so filter and cleanup
const testCommands = reject(commands.filter(notEmpty), duplicate)
const specName = path.basename(window.location.pathname)
const screenshot = `${screenshotName}.png`
console.log('=== test failed ===')
console.log(specName)
console.log('=== title ===')
console.log(title)
if (suiteName) {
console.log('suite', suiteName)
}
console.log(testName)
console.log('=== error ===')
console.log(testError)
console.log('=== commands ===')
console.log(testCommands.join('\n'))
console.log('=== screenshot ===')
console.log(screenshot)
writeFailedTestInfo({
specName,
title,
suiteName,
testName,
testError,
testCommands,
screenshot
})
}
// We have to do a hack to make sure OUR "afterEach" callback function
// runs BEFORE any user supplied "afterEach" callback. This is necessary
// to take screenshot of the failure AS SOON AS POSSIBLE.
// Otherwise commands executed by the user callback might destroys the
// screen and add too many commands to the log, making post-mortem
// triage very difficult. In this case we just wrap client supplied
// "afterEach" function with our callback "onFailed". This ensures we run
// first.
// remember which tests we have processed already
const seenTests = {}
function hasSeen (testName) {
return seenTests[testName]
}
function doneWithTest (testName) {
seenTests[testName] = true
}
const _afterEach = afterEach
afterEach = (name, fn) => { // eslint-disable-line
if (typeof name === 'function') {
fn = name
name = fn.name
}
// before running the client function "fn"
// run our "onFailed" to capture the screenshot sooner
_afterEach(name, function () {
onFailed.call(this)
fn()
})
}
startLogging()
beforeEach(initLog)
// register our callback to process failed tests without wrapping
_afterEach(onFailed)