-
-
Notifications
You must be signed in to change notification settings - Fork 736
/
Copy pathhooks.js
112 lines (96 loc) · 2.61 KB
/
hooks.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
const event = require('../event')
const { serializeError } = require('../utils')
// const { serializeTest } = require('./test')
/**
* Represents a test hook in the testing framework
* @class
* @property {Object} suite - The test suite this hook belongs to
* @property {Object} test - The test object associated with this hook
* @property {Object} runnable - The current test being executed
* @property {Object} ctx - The context object
* @property {Error|null} err - The error that occurred during hook execution, if any
*/
class Hook {
/**
* Creates a new Hook instance
* @param {Object} context - The context object containing suite and test information
* @param {Object} context.suite - The test suite
* @param {Object} context.test - The test object
* @param {Object} context.ctx - The context object
* @param {Error} error - The error object if hook execution failed
*/
constructor(context, error) {
this.suite = context.suite
this.test = context.test
this.runnable = context?.ctx?.test
this.ctx = context.ctx
this.err = error
}
get hookName() {
return this.constructor.name.replace('Hook', '')
}
simplify() {
return {
hookName: this.hookName,
title: this.title,
// test: this.test ? serializeTest(this.test) : null,
// suite: this.suite ? serializeSuite(this.suite) : null,
error: this.err ? serializeError(this.err) : null,
}
}
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, error))
break
case 'after each':
event.emit(eventType, new AfterHook(suite, error))
break
case 'before all':
event.emit(eventType, new BeforeSuiteHook(suite, error))
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,
}