-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
mochaEventsUtils.ts
187 lines (153 loc) · 4.88 KB
/
mochaEventsUtils.ts
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
import type { CypressInCypressMochaEvent } from '../../../../src/runner/event-manager'
import _ from 'lodash'
import type { MochaLifecycleData, SanitizedMochaLifecycleData } from './mochaTypes'
import EventEmitter from 'events'
import disparity from 'disparity'
const hooks = {
before: ['before all', 'before each'],
after: ['after each', 'after all'],
} as const
const stringifyShort = (obj: Record<string, any>) => {
const constructorName = _.get(obj, 'constructor.name')
if (constructorName && !_.includes(['Object', 'Array'], constructorName)) {
return `{${constructorName}}`
}
if (_.isArray(obj)) {
return `[Array ${obj.length}]`
}
if (_.isObject(obj)) {
return `{Object ${Object.keys(obj).length}}`
}
return obj
}
const eventCleanseMap = {
snapshots: stringifyShort,
parent: stringifyShort,
tests: stringifyShort,
commands: stringifyShort,
invocationDetails: stringifyShort,
body: () => '[body]',
wallClockStartedAt: () => 'match.date',
lifecycle: () => 'match.number',
fnDuration: () => 'match.number',
duration: () => 'match.number',
afterFnDuration: () => 'match.number',
wallClockDuration: () => 'match.number',
stack: () => 'match.string',
file: (arg: string) => arg ? 'relative/path/to/spec.js' : undefined,
message: () => '[error message]',
parsedStack: () => 'match.array',
name: (n: string) => n === 'Error' ? 'AssertionError' : n,
err: () => {
return {
message: '[error message]',
name: 'AssertionError',
stack: 'match.string',
parsedStack: 'match.array',
}
},
startTime: new Date(0),
start: () => 'match.date',
end: () => 'match.date',
timings: (arg: MochaLifecycleData): SanitizedMochaLifecycleData => {
let sanitizedLifecycleData: SanitizedMochaLifecycleData = {
lifecycle: 'match.number',
}
for (const hook of hooks.before) {
const hooksToSanitize = arg[hook]
if (!hooksToSanitize) {
continue
}
sanitizedLifecycleData[hook] = hooksToSanitize.map((oldHook) => {
return {
hookId: oldHook.hookId,
fnDuration: 'match.number',
afterFnDuration: 'match.number',
}
})
}
if (arg.test) {
sanitizedLifecycleData.test = {
fnDuration: 'match.number',
afterFnDuration: 'match.number',
}
}
for (const hook of hooks.after) {
const hooksToSanitize = arg[hook]
if (!hooksToSanitize) {
continue
}
sanitizedLifecycleData[hook] = hooksToSanitize.map((oldHook) => {
return {
hookId: oldHook.hookId,
fnDuration: 'match.number',
afterFnDuration: 'match.number',
}
})
}
return sanitizedLifecycleData
},
}
const keysToEliminate = ['codeFrame', '_testConfig'] as const
function removeUnusedKeysForTestSnapshot<T> (obj: T): T {
for (const key of keysToEliminate) {
delete obj[key]
}
for (const [key, value] of Object.entries(obj)) {
if (key in obj) {
const transform = eventCleanseMap[key]?.(value)
if (!transform) {
continue
}
obj[key] = transform
} else {
delete obj[key]
}
}
return obj
}
declare global {
interface Window {
bus: EventEmitter
}
}
export function runCypressInCypressMochaEventsTest<T> (snapshots: T, snapToCompare: keyof T, done: Mocha.Done) {
const bus = new EventEmitter()
const outerRunner = window.top!.window
outerRunner.bus = bus
// TODO: Can we automate writing the snapshots to disk?
// For some reason `cy.task('getSnapshot')` has problems when executed in
// "cypress in cypress"
bus.on('assert:cypress:in:cypress', (snapshot: CypressInCypressMochaEvent[]) => {
const expected = snapshots[snapToCompare]
const diff = disparity.unifiedNoColor(JSON.stringify(snapshot, null, 2), JSON.stringify(expected, null, 2), {})
if (diff !== '') {
/* eslint-disable no-console */
console.info('Received snapshot:', JSON.stringify(snapshot, null, 2))
return cy.fail(new Error(`The captured mocha events did not match the "${String(snapToCompare)}" snapshot.\n${diff}`), { async: false })
}
Cypress.log({
name: 'assert',
message: `The captured mocha events for the spec matched the "${String(snapToCompare)}" snapshot!`,
state: 'passed',
})
return done()
})
const assertMatchingSnapshot = (win: Cypress.AUTWindow) => {
win.getEventManager().on('cypress:in:cypress:run:complete', (args: CypressInCypressMochaEvent[]) => {
const data = sanitizeMochaEvents(args)
bus.emit('assert:cypress:in:cypress', data)
})
}
return { assertMatchingSnapshot }
}
function sanitizeMochaEvents (args: CypressInCypressMochaEvent[]) {
return args.map((mochaEvent) => {
return mochaEvent.map((payload) => {
if (typeof payload === 'string') {
return payload
}
return removeUnusedKeysForTestSnapshot(payload)
})
})
}