Skip to content

Commit 9060fed

Browse files
priyanshu92claude
andcommitted
[Pages] Stub child_process.execSync directly in validation-helpers test
Follow-up to #132. The initial test stubbed `child_process` by swapping the entry in `require.cache`, which relies on Node's caching of built-in modules — implementation detail that may not hold across versions. Switch to monkeypatching `childProcess.execSync` directly before requiring `validation-helpers.js`, with cleanup via `t.after()`. This exercises the helper through its real `require('child_process')` call and is the idiomatic node:test approach. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 260127a commit 9060fed

1 file changed

Lines changed: 18 additions & 26 deletions

File tree

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
const test = require('node:test');
22
const assert = require('node:assert/strict');
33
const path = require('path');
4+
const childProcess = require('child_process');
45

56
const helpersPath = path.join(__dirname, '..', 'lib', 'validation-helpers.js');
6-
const childProcessId = require.resolve('child_process');
77

8-
test('getAuthToken passes --allow-no-subscriptions to az', () => {
9-
const originalChildProcess = require.cache[childProcessId];
8+
test('getAuthToken passes --allow-no-subscriptions to az', (t) => {
9+
const originalExecSync = childProcess.execSync;
1010
let capturedCommand = null;
1111

12-
require.cache[childProcessId] = {
13-
id: childProcessId,
14-
filename: childProcessId,
15-
loaded: true,
16-
exports: {
17-
execSync: (command, options) => {
18-
capturedCommand = command;
19-
const out = 'fake-token-value\n';
20-
return options && options.encoding ? out : Buffer.from(out);
21-
},
22-
},
12+
childProcess.execSync = (command, options) => {
13+
capturedCommand = command;
14+
const out = 'fake-token-value\n';
15+
return options && options.encoding ? out : Buffer.from(out);
2316
};
2417
delete require.cache[require.resolve(helpersPath)];
2518

26-
try {
27-
const { getAuthToken } = require(helpersPath);
28-
const token = getAuthToken('https://example.crm.dynamics.com');
29-
30-
assert.equal(token, 'fake-token-value');
31-
assert.match(capturedCommand, /^az account get-access-token /);
32-
assert.match(capturedCommand, /--allow-no-subscriptions/);
33-
assert.match(capturedCommand, /--resource "https:\/\/example\.crm\.dynamics\.com"/);
34-
} finally {
35-
if (originalChildProcess) require.cache[childProcessId] = originalChildProcess;
36-
else delete require.cache[childProcessId];
19+
t.after(() => {
20+
childProcess.execSync = originalExecSync;
3721
delete require.cache[require.resolve(helpersPath)];
38-
}
22+
});
23+
24+
const { getAuthToken } = require(helpersPath);
25+
const token = getAuthToken('https://example.crm.dynamics.com');
26+
27+
assert.equal(token, 'fake-token-value');
28+
assert.match(capturedCommand, /^az account get-access-token /);
29+
assert.match(capturedCommand, /--allow-no-subscriptions/);
30+
assert.match(capturedCommand, /--resource "https:\/\/example\.crm\.dynamics\.com"/);
3931
});

0 commit comments

Comments
 (0)