Skip to content

Commit ec2940b

Browse files
cjihrigaduh95
authored andcommittedFeb 3, 2025
test: update test-child-process-windows-hide to use node:test
This commit updates test/parallel/test-child-process-windows-hide.js to use node:test. This allows the test to use the built in mocking functionality instead of managing spies manually. It also prevents multiple child processes from being spawned in parallel, which can be problematic in the CI. PR-URL: #56437 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3e729ce commit ec2940b

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed
 

‎test/parallel/test-child-process-windows-hide.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,48 @@
33
const common = require('../common');
44
const assert = require('assert');
55
const cp = require('child_process');
6+
const { test } = require('node:test');
67
const internalCp = require('internal/child_process');
78
const cmd = process.execPath;
89
const args = ['-p', '42'];
910
const options = { windowsHide: true };
1011

11-
// Since windowsHide isn't really observable, monkey patch spawn() and
12-
// spawnSync() to verify that the flag is being passed through correctly.
13-
const originalSpawn = internalCp.ChildProcess.prototype.spawn;
14-
const originalSpawnSync = internalCp.spawnSync;
12+
// Since windowsHide isn't really observable, this test relies on monkey
13+
// patching spawn() and spawnSync() to verify that the flag is being passed
14+
// through correctly.
1515

16-
internalCp.ChildProcess.prototype.spawn = common.mustCall(function(options) {
17-
assert.strictEqual(options.windowsHide, true);
18-
return originalSpawn.apply(this, arguments);
19-
}, 2);
20-
21-
internalCp.spawnSync = common.mustCall(function(options) {
22-
assert.strictEqual(options.windowsHide, true);
23-
return originalSpawnSync.apply(this, arguments);
24-
});
25-
26-
{
16+
test('spawnSync() passes windowsHide correctly', (t) => {
17+
const spy = t.mock.method(internalCp, 'spawnSync');
2718
const child = cp.spawnSync(cmd, args, options);
2819

2920
assert.strictEqual(child.status, 0);
3021
assert.strictEqual(child.signal, null);
3122
assert.strictEqual(child.stdout.toString().trim(), '42');
3223
assert.strictEqual(child.stderr.toString().trim(), '');
33-
}
24+
assert.strictEqual(spy.mock.calls.length, 1);
25+
assert.strictEqual(spy.mock.calls[0].arguments[0].windowsHide, true);
26+
});
3427

35-
{
28+
test('spawn() passes windowsHide correctly', (t, done) => {
29+
const spy = t.mock.method(internalCp.ChildProcess.prototype, 'spawn');
3630
const child = cp.spawn(cmd, args, options);
3731

3832
child.on('exit', common.mustCall((code, signal) => {
3933
assert.strictEqual(code, 0);
4034
assert.strictEqual(signal, null);
35+
assert.strictEqual(spy.mock.calls.length, 1);
36+
assert.strictEqual(spy.mock.calls[0].arguments[0].windowsHide, true);
37+
done();
4138
}));
42-
}
39+
});
4340

44-
{
45-
const callback = common.mustSucceed((stdout, stderr) => {
41+
test('execFile() passes windowsHide correctly', (t, done) => {
42+
const spy = t.mock.method(internalCp.ChildProcess.prototype, 'spawn');
43+
cp.execFile(cmd, args, options, common.mustSucceed((stdout, stderr) => {
4644
assert.strictEqual(stdout.trim(), '42');
4745
assert.strictEqual(stderr.trim(), '');
48-
});
49-
50-
cp.execFile(cmd, args, options, callback);
51-
}
46+
assert.strictEqual(spy.mock.calls.length, 1);
47+
assert.strictEqual(spy.mock.calls[0].arguments[0].windowsHide, true);
48+
done();
49+
}));
50+
});

0 commit comments

Comments
 (0)
Please sign in to comment.