-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathspawn-test.js
More file actions
48 lines (44 loc) · 2.01 KB
/
Copy pathspawn-test.js
File metadata and controls
48 lines (44 loc) · 2.01 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
// spawnTest is a function
if (typeof spawnTest !== 'function') {
throw new Error('Expected a global spawnTest function');
}
// Successful child: exits 0, stderr empty, and harness globals are available
// inside the child (the child checks `typeof assert === 'function'` itself).
{
const result = spawnTest('spawn-test-ok-child.mjs');
assert.strictEqual(result.status, 0, `ok child exited with status ${result.status}; stderr:\n${result.stderr}`);
assert.strictEqual(result.aborted, false);
assert.strictEqual(result.stderr, '');
}
// Failing child: non-zero status and stderr contains the thrown marker.
{
const result = spawnTest('spawn-test-fail-child.mjs');
assert.notStrictEqual(result.status, 0, 'fail child should exit non-zero');
// A thrown error is a clean non-zero exit, not an abnormal termination.
assert.strictEqual(result.aborted, false);
if (!result.stderr.includes('spawn-test-fail-marker')) {
throw new Error(`Expected stderr to include the failure marker, got:\n${result.stderr}`);
}
}
// Result shape: all four fields are present.
{
const result = spawnTest('spawn-test-ok-child.mjs');
for (const key of ['status', 'aborted', 'stdout', 'stderr']) {
if (!(key in result)) {
throw new Error(`Expected spawnTest result to have "${key}" field`);
}
}
assert.strictEqual(typeof result.aborted, 'boolean');
assert.strictEqual(typeof result.stdout, 'string');
assert.strictEqual(typeof result.stderr, 'string');
}
// cwd is forwarded to the child: running from the parent of tests/harness
// makes the bare child filename unresolvable. The child's stderr must name
// the specific file Node tried to load, proving cwd actually shifted.
{
const result = spawnTest('spawn-test-ok-child.mjs', { cwd: '..' });
assert.notStrictEqual(result.status, 0, 'expected cwd ".." to make the child filename unresolvable');
if (!result.stderr.includes('spawn-test-ok-child.mjs')) {
throw new Error(`Expected stderr to reference the unresolved child filename, got:\n${result.stderr}`);
}
}