incrementNodeInspectorPort.test.cjs
This file
Path
tests/incrementNodeInspectorPort.test.cjs
146 lines · 5.2 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147const { test, describe, after } = require('node:test');const assert = require('node:assert/strict');const childProcess = require('child_process');const path = require('path');const commander = require('../');
// Using mocking of child_process.spawn. Not fully supported yet in esm by node:test so run as cjs.
describe('increment node inspector port in executable subcommands', () => { function makeSpies(t) { const spawnSpy = t.mock.method(childProcess, 'spawn', () => { return { on: () => {}, }; }); const signalSpy = test.mock.method(process, 'on', () => {}); return { spawnSpy, signalSpy }; }
const oldExecArgv = process.execArgv;
after(() => { process.execArgv = oldExecArgv; });
function makeProgram() { const program = new commander.Command(); const fileWhichExists = path.join(__dirname, './fixtures/pm-cache.js'); program.command('cache', 'stand-alone command', { executableFile: fileWhichExists, }); return program; }
function extractMockExecArgs(spawnSpy) { return spawnSpy.mock.calls[0].arguments[1].slice(0, -1); }
test('when --inspect then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect=127.0.0.1:9230']); });
test('when --inspect=100 then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect=100']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect=127.0.0.1:101']); });
test('when --inspect=1.2.3.4:100 then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect=1.2.3.4:100']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect=1.2.3.4:101']); });
test('when --inspect=1.2.3.4 then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect=1.2.3.4']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect=1.2.3.4:9230']); });
test('when --inspect-brk then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect-brk']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect-brk=127.0.0.1:9230']); });
test('when --inspect-brk=100 then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect-brk=100']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect-brk=127.0.0.1:101']); });
test('when --inspect-brk=1.2.3.4 then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect-brk=1.2.3.4']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect-brk=1.2.3.4:9230']); });
test('when --inspect-brk=1.2.3.4:100 then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect-brk=1.2.3.4:100']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect-brk=1.2.3.4:101']); });
test('when --inspect-port=100 then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect-port=100']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect-port=127.0.0.1:101']); });
test('when --inspect-port=1.2.3.4:100 then bump port', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect-port=1.2.3.4:100']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect-port=1.2.3.4:101']); });
test('when --inspect-unexpected then unchanged', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--inspect-unexpected']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--inspect-unexpected']); });
test('when --frozen-intrinsics then unchanged', (t) => { const { spawnSpy } = makeSpies(t); const program = makeProgram(); process.execArgv = ['--frozen-intrinsics ']; program.parse(['node', 'test', 'cache']); const execArgs = extractMockExecArgs(spawnSpy); assert.deepEqual(execArgs, ['--frozen-intrinsics ']); });});