-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtree-builder-decorator.js
More file actions
118 lines (88 loc) · 3.03 KB
/
tree-builder-decorator.js
File metadata and controls
118 lines (88 loc) · 3.03 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
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
const { Suite, Test, Hook } = require("../test-object");
const crypto = require("../../utils/crypto");
const { computeFile } = require("./utils");
class TreeBuilderDecorator {
#treeBuilder;
#suiteMap;
#suiteCounter;
static create(...args) {
return new this(...args);
}
constructor(treeBuilder) {
this.#treeBuilder = treeBuilder;
this.#suiteMap = new Map();
this.#suiteCounter = new Map();
}
addSuite(mochaSuite) {
const { id: mochaId } = mochaSuite;
const file = computeFile(mochaSuite) ?? "unknown-file";
const positionInFile = this.#suiteCounter.get(file) || 0;
const id = mochaSuite.root ? mochaId : crypto.getShortMD5(file) + positionInFile;
const suite = this.#mkTestObject(Suite, mochaSuite, { id });
this.#applyConfig(suite, mochaSuite);
this.#treeBuilder.addSuite(suite, this.#getParent(mochaSuite, null));
this.#suiteMap.set(mochaId, suite);
this.#suiteCounter.set(file, positionInFile + 1);
return this;
}
addTest(mochaTest) {
const { fn } = mochaTest;
const id = crypto.getShortMD5(mochaTest.fullTitle());
const test = this.#mkTestObject(Test, mochaTest, { id, fn });
this.#applyConfig(test, mochaTest);
this.#treeBuilder.addTest(test, this.#getParent(mochaTest));
return this;
}
addBeforeEachHook(mochaHook) {
return this.#addHook(mochaHook, (hook, parent) => this.#treeBuilder.addBeforeEachHook(hook, parent));
}
addAfterEachHook(mochaHook) {
return this.#addHook(mochaHook, (hook, parent) => this.#treeBuilder.addAfterEachHook(hook, parent));
}
#addHook(mochaHook, cb) {
const { fn, title } = mochaHook;
const hook = Hook.create({ fn, title });
cb(hook, this.#getParent(mochaHook));
return this;
}
#mkTestObject(Constructor, mochaObject, customOpts) {
const { title, file } = mochaObject;
return Constructor.create({ title, file, ...customOpts });
}
#applyConfig(testObject, mochaObject) {
const { pending, parent } = mochaObject;
if (!parent || mochaObject.timeout() !== parent.timeout()) {
testObject.timeout = mochaObject.timeout();
}
if (pending) {
testObject.skip({ reason: "Skipped by mocha interface" });
}
}
#getParent({ parent }, defaultValue) {
if (!parent) {
if (defaultValue !== undefined) {
return defaultValue;
}
throw new Error("Parent not set");
}
return this.#suiteMap.get(parent.id);
}
addTrap(fn) {
this.#treeBuilder.addTrap(fn);
return this;
}
addTestFilter(fn) {
this.#treeBuilder.addTestFilter(fn);
return this;
}
applyFilters() {
this.#treeBuilder.applyFilters();
return this;
}
getRootSuite() {
return this.#treeBuilder.getRootSuite();
}
}
module.exports = {
TreeBuilderDecorator,
};