-
-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathhooks.js
More file actions
84 lines (69 loc) · 1.51 KB
/
hooks.js
File metadata and controls
84 lines (69 loc) · 1.51 KB
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
const event = require('../event')
class Hook {
constructor(context, error) {
this.suite = context.suite
this.test = context.test
this.runnable = context?.ctx?.test
this.ctx = context.ctx
this.error = error
this.steps = []
}
get hookName() {
return this.constructor.name.replace('Hook', '')
}
toString() {
return this.hookName
}
toCode() {
return this.toString() + '()'
}
retry(n) {
this.suite.opts[`retry${this.hookName}`] = n
}
get title() {
return this.ctx?.test?.title || this.name
}
get name() {
return this.constructor.name
}
}
class BeforeHook extends Hook {}
class AfterHook extends Hook {}
class BeforeSuiteHook extends Hook {}
class AfterSuiteHook extends Hook {}
function fireHook(eventType, suite, error) {
const hook = suite.ctx?.test?.title?.match(/"([^"]*)"/)[1]
switch (hook) {
case 'before each':
event.emit(eventType, new BeforeHook(suite))
break
case 'after each':
event.emit(eventType, new AfterHook(suite, error))
break
case 'before all':
event.emit(eventType, new BeforeSuiteHook(suite))
break
case 'after all':
event.emit(eventType, new AfterSuiteHook(suite, error))
break
default:
event.emit(eventType, suite, error)
}
}
class HookConfig {
constructor(hook) {
this.hook = hook
}
retry(n) {
this.hook.retry(n)
return this
}
}
module.exports = {
BeforeHook,
AfterHook,
BeforeSuiteHook,
AfterSuiteHook,
fireHook,
HookConfig,
}