forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-runner-run.mjs
384 lines (341 loc) · 12.8 KB
/
test-runner-run.mjs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { join } from 'node:path';
import { describe, it, run } from 'node:test';
import { dot, spec, tap } from 'node:test/reporters';
import assert from 'node:assert';
const testFixtures = fixtures.path('test-runner');
describe('require(\'node:test\').run', { concurrency: true }, () => {
it('should run with no tests', async () => {
const stream = run({ files: [] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should fail with non existing file', async () => {
const stream = run({ files: ['a-random-file-that-does-not-exist.js'] });
stream.on('test:fail', common.mustCall(1));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should succeed with a file', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs')] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should run same file twice', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs'), join(testFixtures, 'test/random.cjs')] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(2));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should run a failed test', async () => {
const stream = run({ files: [testFixtures] });
stream.on('test:fail', common.mustCall(1));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should support timeout', async () => {
const stream = run({ timeout: 50, files: [
fixtures.path('test-runner', 'never_ending_sync.js'),
fixtures.path('test-runner', 'never_ending_async.js'),
] });
stream.on('test:fail', common.mustCall(2));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should validate files', async () => {
[Symbol(), {}, () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false]
.forEach((files) => assert.throws(() => run({ files }), {
code: 'ERR_INVALID_ARG_TYPE'
}));
});
it('should be piped with dot', async () => {
const result = await run({ files: [join(testFixtures, 'test/random.cjs')] }).compose(dot).toArray();
assert.deepStrictEqual(result, [
'.',
'\n',
]);
});
it('should be piped with spec', async () => {
const specReporter = new spec();
const result = await run({ files: [join(testFixtures, 'test/random.cjs')] }).compose(specReporter).toArray();
const stringResults = result.map((bfr) => bfr.toString());
assert.match(stringResults[0], /this should pass/);
assert.match(stringResults[1], /tests 1/);
assert.match(stringResults[1], /pass 1/);
});
it('should be piped with tap', async () => {
const result = await run({ files: [join(testFixtures, 'test/random.cjs')] }).compose(tap).toArray();
assert.strictEqual(result.length, 13);
assert.strictEqual(result[0], 'TAP version 13\n');
assert.strictEqual(result[1], '# Subtest: this should pass\n');
assert.strictEqual(result[2], 'ok 1 - this should pass\n');
assert.match(result[3], /duration_ms: \d+\.?\d*/);
assert.strictEqual(result[4], '1..1\n');
assert.strictEqual(result[5], '# tests 1\n');
assert.strictEqual(result[6], '# suites 0\n');
assert.strictEqual(result[7], '# pass 1\n');
assert.strictEqual(result[8], '# fail 0\n');
assert.strictEqual(result[9], '# cancelled 0\n');
assert.strictEqual(result[10], '# skipped 0\n');
assert.strictEqual(result[11], '# todo 0\n');
assert.match(result[12], /# duration_ms \d+\.?\d*/);
});
it('should skip tests not matching testNamePatterns - RegExp', async () => {
const result = await run({ files: [join(testFixtures, 'test/skip_by_name.cjs')], testNamePatterns: [/executed/] })
.compose(tap)
.toArray();
assert.strictEqual(result[2], 'ok 1 - this should be skipped # SKIP test name does not match pattern\n');
assert.strictEqual(result[5], 'ok 2 - this should be executed\n');
});
it('should skip tests not matching testNamePatterns - string', async () => {
const result = await run({ files: [join(testFixtures, 'test/skip_by_name.cjs')], testNamePatterns: ['executed'] })
.compose(tap)
.toArray();
assert.strictEqual(result[2], 'ok 1 - this should be skipped # SKIP test name does not match pattern\n');
assert.strictEqual(result[5], 'ok 2 - this should be executed\n');
});
it('should emit "test:watch:drained" event on watch mode', async () => {
const controller = new AbortController();
await run({ files: [join(testFixtures, 'test/random.cjs')], watch: true, signal: controller.signal })
.on('data', function({ type }) {
if (type === 'test:watch:drained') {
controller.abort();
}
});
});
describe('AbortSignal', () => {
it('should stop watch mode when abortSignal aborts', async () => {
const controller = new AbortController();
const result = await run({
files: [join(testFixtures, 'test/random.cjs')],
watch: true,
signal: controller.signal
})
.compose(async function* (source) {
for await (const chunk of source) {
if (chunk.type === 'test:pass') {
controller.abort();
yield chunk.data.name;
}
}
})
.toArray();
assert.deepStrictEqual(result, ['this should pass']);
});
it('should abort when test succeeded', async () => {
const stream = run({
files: [fixtures.path('test-runner', 'aborts', 'successful-test-still-call-abort.js')]
});
let passedTestCount = 0;
let failedTestCount = 0;
let output = '';
for await (const data of stream) {
if (data.type === 'test:stdout') {
output += data.data.message.toString();
}
if (data.type === 'test:fail') {
failedTestCount++;
}
if (data.type === 'test:pass') {
passedTestCount++;
}
}
assert.match(output, /abort called for test 1/);
assert.match(output, /abort called for test 2/);
assert.strictEqual(failedTestCount, 0, new Error('no tests should fail'));
assert.strictEqual(passedTestCount, 2);
});
it('should abort when test failed', async () => {
const stream = run({
files: [fixtures.path('test-runner', 'aborts', 'failed-test-still-call-abort.js')]
});
let passedTestCount = 0;
let failedTestCount = 0;
let output = '';
for await (const data of stream) {
if (data.type === 'test:stdout') {
output += data.data.message.toString();
}
if (data.type === 'test:fail') {
failedTestCount++;
}
if (data.type === 'test:pass') {
passedTestCount++;
}
}
assert.match(output, /abort called for test 1/);
assert.match(output, /abort called for test 2/);
assert.strictEqual(passedTestCount, 0, new Error('no tests should pass'));
assert.strictEqual(failedTestCount, 2);
});
});
describe('sharding', () => {
const shardsTestsFixtures = fixtures.path('test-runner', 'shards');
const shardsTestsFiles = [
'a.cjs',
'b.cjs',
'c.cjs',
'd.cjs',
'e.cjs',
'f.cjs',
'g.cjs',
'h.cjs',
'i.cjs',
'j.cjs',
].map((file) => join(shardsTestsFixtures, file));
describe('validation', () => {
it('should require shard.total when having shard option', () => {
assert.throws(() => run({ files: shardsTestsFiles, shard: {} }), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.shard.total" property must be of type number. Received undefined'
});
});
it('should require shard.index when having shards option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
shard: {
total: 5
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.shard.index" property must be of type number. Received undefined'
});
});
it('should require shard.total to be greater than 0 when having shard option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
shard: {
total: 0,
index: 1
}
}), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.shard.total" is out of range. It must be >= 1 && <= 9007199254740991. Received 0'
});
});
it('should require shard.index to be greater than 0 when having shard option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
shard: {
total: 6,
index: 0
}
}), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
// eslint-disable-next-line max-len
message: 'The value of "options.shard.index" is out of range. It must be >= 1 && <= 6 ("options.shard.total"). Received 0'
});
});
it('should require shard.index to not be greater than the shards total when having shard option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
shard: {
total: 6,
index: 7
}
}), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
// eslint-disable-next-line max-len
message: 'The value of "options.shard.index" is out of range. It must be >= 1 && <= 6 ("options.shard.total"). Received 7'
});
});
it('should require watch mode to be disabled when having shard option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
watch: true,
shard: {
total: 6,
index: 1
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_VALUE',
message: 'The property \'options.shard\' shards not supported with watch mode. Received true'
});
});
});
it('should run only the tests files matching the shard index', async () => {
const stream = run({
files: shardsTestsFiles,
shard: {
total: 5,
index: 1
}
});
const executedTestFiles = [];
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
assert.deepStrictEqual(executedTestFiles, [
join(shardsTestsFixtures, 'a.cjs'),
join(shardsTestsFixtures, 'f.cjs'),
]);
});
it('different shards should not run the same file', async () => {
const executedTestFiles = [];
const testStreams = [];
const shards = 5;
for (let i = 1; i <= shards; i++) {
const stream = run({
files: shardsTestsFiles,
shard: {
total: shards,
index: i
}
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
testStreams.push(stream);
}
await Promise.all(testStreams.map(async (stream) => {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
}));
assert.deepStrictEqual(executedTestFiles, [...new Set(executedTestFiles)]);
});
it('combination of all shards should be all the tests', async () => {
const executedTestFiles = [];
const testStreams = [];
const shards = 5;
for (let i = 1; i <= shards; i++) {
const stream = run({
files: shardsTestsFiles,
shard: {
total: shards,
index: i
}
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
testStreams.push(stream);
}
await Promise.all(testStreams.map(async (stream) => {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
}));
assert.deepStrictEqual(executedTestFiles.sort(), [...shardsTestsFiles].sort());
});
});
});