/
Hooks.js
77 lines (64 loc) · 1.85 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
import hooksLog from './hooksLog';
// READ THIS! Disclaimer:
// Do not add any functionality to this class unless you want to expose it to the Hooks API.
// This class is only an interface for users of Dredd hooks.
class Hooks {
constructor(options = {}) {
this.before = this.before.bind(this);
this.beforeValidation = this.beforeValidation.bind(this);
this.after = this.after.bind(this);
this.beforeAll = this.beforeAll.bind(this);
this.afterAll = this.afterAll.bind(this);
this.beforeEach = this.beforeEach.bind(this);
this.beforeEachValidation = this.beforeEachValidation.bind(this);
this.afterEach = this.afterEach.bind(this);
this.log = this.log.bind(this);
this.logs = options.logs;
this.logger = options.logger;
this.beforeHooks = {};
this.beforeValidationHooks = {};
this.afterHooks = {};
this.beforeAllHooks = [];
this.afterAllHooks = [];
this.beforeEachHooks = [];
this.beforeEachValidationHooks = [];
this.afterEachHooks = [];
}
before(name, hook) {
this.addHook(this.beforeHooks, name, hook);
}
beforeValidation(name, hook) {
this.addHook(this.beforeValidationHooks, name, hook);
}
after(name, hook) {
this.addHook(this.afterHooks, name, hook);
}
beforeAll(hook) {
this.beforeAllHooks.push(hook);
}
afterAll(hook) {
this.afterAllHooks.push(hook);
}
beforeEach(hook) {
this.beforeEachHooks.push(hook);
}
beforeEachValidation(hook) {
this.beforeEachValidationHooks.push(hook);
}
afterEach(hook) {
this.afterEachHooks.push(hook);
}
addHook(hooks, name, hook) {
if (hooks[name]) {
hooks[name].push(hook);
} else {
hooks[name] = [hook];
}
}
// log(logVariant, content)
// log(content)
log(...args) {
this.logs = hooksLog(this.logs, this.logger, ...Array.from(args));
}
}
export default Hooks;