-
Notifications
You must be signed in to change notification settings - Fork 1
/
testRunner.js
146 lines (122 loc) · 4.81 KB
/
testRunner.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { styleText } from 'node:util';
import { EventEmitter } from 'events';
import { AsyncLocalStorage } from 'async_hooks';
import TestSuite from './testSuite.js';
import { events } from './../shared/vars.js';
const green = msg => styleText('green', msg);
const asyncLocalStorage = new AsyncLocalStorage();
class TestRunner extends EventEmitter {
constructor() {
super();
// todo: fix output
// console.log(green('running executing: ' + this.#getFileOriginName()))
}
describe(name, fn) {
const suite = new TestSuite(name);
const context = asyncLocalStorage.getStore() || {};
const currentStack = context.suiteStack ? [...context.suiteStack, suite] : [suite];
this.#emit(events.suiteStart, suite);
asyncLocalStorage.run({ suiteStack: currentStack }, async () => {
await fn();
await this.#runSuite(suite);
this.#emit(events.suiteEnd, suite);
});
}
it(description, testFn) {
const suite = this.#getCurrentSuite();
suite.tests.push(this.#wrapTest({ name: description, type: 'it' }, testFn));
}
before(hookFn) {
const suite = this.#getCurrentSuite();
suite.beforeHooks.push(this.#wrapTest({ name: 'before', type: 'before' }, hookFn));
}
#emit(event, data) {
super.emit(event, data);
process.send?.({ event, data });
}
beforeEach(hookFn) {
const suite = this.#getCurrentSuite();
suite.beforeEachHooks.push(this.#wrapTest({ name: 'beforeEach', type: 'beforeEach' }, hookFn));
}
after(hookFn) {
const suite = this.#getCurrentSuite();
suite.afterHooks.push(this.#wrapTest({ name: 'after', type: 'after' }, hookFn));
}
afterEach(hookFn) {
const suite = this.#getCurrentSuite();
suite.afterEachHooks.push(this.#wrapTest({ name: 'afterEach', type: 'afterEach' }, hookFn));
}
#getCurrentSuite() {
const context = asyncLocalStorage.getStore() || {};
const suiteStack = context.suiteStack;
const currentSuite = suiteStack[suiteStack.length - 1];
return currentSuite;
}
async #runSuite(suite) {
for (const before of suite.beforeHooks) {
await before();
}
for (const test of suite.tests) {
for (const beforeEach of suite.beforeEachHooks) {
await beforeEach();
}
await test();
for (const afterEach of suite.afterEachHooks) {
await afterEach();
}
}
for (const after of suite.afterHooks) {
await after();
}
}
#wrapTest(data, testFn) {
const that = this;
const logger = (info) => ({
log(message) {
that.emit(events.log, { ...info, message });
}
})
return async () => {
const startedAt = process.hrtime.bigint();
const currentContext = asyncLocalStorage.getStore() || {};
const info = { ...data, tree: this.#buildDependencyTree(currentContext.suiteStack) };
this.#emit(events.testStart, info);
const mergedContext = {
suiteStack: [...currentContext.suiteStack, info],
fileOriginName: this.fileOriginName,
};
await asyncLocalStorage.run(mergedContext, async () => {
try {
await testFn({ diagnostic: logger(info) });
const elapsedTimeMs = this.#calcElapsed(startedAt);
this.#emit(events.testPass, { ...info, elapsedTimeMs });
} catch (error) {
const elapsedTimeMs = this.#calcElapsed(startedAt);
this.#emit(events.testFail, { error: { ...error, stack: error.stack }, ...info, elapsedTimeMs });
}
const elapsedTimeMs = this.#calcElapsed(startedAt);
this.#emit(events.testEnd, { ...info, elapsedTimeMs });
});
};
}
#buildDependencyTree(suiteStack) {
let formattedPath = '';
suiteStack.forEach((suite, index) => {
formattedPath += `${' '.repeat(index * 4)}${suite.name}\n`;
});
return formattedPath.trimEnd();
}
#calcElapsed(startedAt) {
const endedAt = process.hrtime.bigint();
return (Number(endedAt - startedAt) / 1000000).toFixed(2);
}
#getFileOriginName() {
const stack = new Error().stack;
const fileOrigin = stack.split('\n').filter(line => !line.includes('(node:')).reverse()[0];
const fileStart = 'file://';
const fileEnd = '.js';
const [indexStart, indexEnd] = [fileOrigin.indexOf(fileStart), fileOrigin.indexOf(fileEnd)];
return fileOrigin.slice(indexStart, indexEnd) + '.js';
}
}
export default TestRunner;