-
Notifications
You must be signed in to change notification settings - Fork 30k
/
tests_stream.js
78 lines (61 loc) Β· 1.76 KB
/
tests_stream.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
'use strict';
const {
ArrayPrototypePush,
ArrayPrototypeShift,
} = primordials;
const Readable = require('internal/streams/readable');
class TestsStream extends Readable {
#buffer;
#canPush;
constructor() {
super({ objectMode: true });
this.#buffer = [];
this.#canPush = true;
}
_read() {
this.#canPush = true;
while (this.#buffer.length > 0) {
const obj = ArrayPrototypeShift(this.#buffer);
if (!this.#tryPush(obj)) {
return;
}
}
}
fail(nesting, file, testNumber, name, details, directive) {
this.#emit('test:fail', { __proto__: null, name, nesting, file, testNumber, details, ...directive });
}
ok(nesting, file, testNumber, name, details, directive) {
this.#emit('test:pass', { __proto__: null, name, nesting, file, testNumber, details, ...directive });
}
plan(nesting, file, count) {
this.#emit('test:plan', { __proto__: null, nesting, file, count });
}
getSkip(reason = undefined) {
return { __proto__: null, skip: reason ?? true };
}
getTodo(reason = undefined) {
return { __proto__: null, todo: reason ?? true };
}
start(nesting, file, name) {
this.#emit('test:start', { __proto__: null, nesting, file, name });
}
diagnostic(nesting, file, message) {
this.#emit('test:diagnostic', { __proto__: null, nesting, file, message });
}
coverage(nesting, file, summary) {
this.#emit('test:coverage', { __proto__: null, nesting, file, summary });
}
#emit(type, data) {
this.emit(type, data);
this.#tryPush({ type, data });
}
#tryPush(message) {
if (this.#canPush) {
this.#canPush = this.push(message);
} else {
ArrayPrototypePush(this.#buffer, message);
}
return this.#canPush;
}
}
module.exports = { TestsStream };