-
Notifications
You must be signed in to change notification settings - Fork 14
/
jest-environment-fail-fast.js
55 lines (44 loc) · 1.63 KB
/
jest-environment-fail-fast.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
import NodeEnvironment from "jest-environment-node"
class NodeEnvironmentFailFast extends NodeEnvironment {
failedDescribeMap = {}
registeredEventHandler = []
async setup() {
await super.setup()
this.global.testEnvironment = this
}
registerTestEventHandler(registeredEventHandler) {
this.registeredEventHandler.push(registeredEventHandler)
}
async executeTestEventHandlers(event, state) {
for (let handler of this.registeredEventHandler) {
await handler(event, state)
}
}
async handleTestEvent(event, state) {
await this.executeTestEventHandlers(event, state)
switch (event.name) {
case "hook_failure": {
const describeBlockName = event.hook.parent.name
this.failedDescribeMap[describeBlockName] = true
// hook errors are not displayed if tests are skipped, so display them manually
console.error(`ERROR: ${describeBlockName} > ${event.hook.type}\n\n`, event.error, "\n")
break
}
case "test_fn_failure": {
this.failedDescribeMap[event.test.parent.name] = true
event.test.mode = "fail";
break
}
case "test_start": {
if (this.failedDescribeMap[event.test.parent.name] && event.test.mode === undefined) {
event.test.mode = "skip";
}
break
}
}
if (super.handleTestEvent) {
super.handleTestEvent(event, state)
}
}
}
export default NodeEnvironmentFailFast