Skip to content

Commit 05b0092

Browse files
thefourtheyeevanlucas
authored andcommitted
test: refactor test-debugger-remote
1. The test doesn't attach an event listener for `exit` events and removes them before killing. The intention is to fail the tests if the processes exit normally. This patch attaches the `exit` event handlers. 2. Replace `var`s with `let`s and `const`s. 3. Replace `==` based assertion with `strictEqual` assertion. 4. Use `common.PORT` instead of `5959`. 5. The test used to expect only one string "connecting to localhost:5959 ... ok", but the debugger actually emits another string, "break in test/fixtures/empty.js:2". This patch asserts if both of them are received in the same order. Refer: #10361 PR-URL: #10455 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 82575f9 commit 05b0092

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

test/debugger/test-debugger-remote.js

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var spawn = require('child_process').spawn;
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const spawn = require('child_process').spawn;
5+
const path = require('path');
56

6-
var buffer = '';
7-
var scriptToDebug = common.fixturesDir + '/empty.js';
8-
9-
function fail() {
10-
assert(0); // `--debug-brk script.js` should not quit
11-
}
7+
const PORT = common.PORT;
8+
const scriptToDebug = path.join(common.fixturesDir, 'empty.js');
129

1310
// running with debug agent
14-
var child = spawn(process.execPath, ['--debug-brk=5959', scriptToDebug]);
15-
16-
console.error(process.execPath, '--debug-brk=5959', scriptToDebug);
11+
const child = spawn(process.execPath, [`--debug-brk=${PORT}`, scriptToDebug]);
1712

1813
// connect to debug agent
19-
var interfacer = spawn(process.execPath, ['debug', 'localhost:5959']);
20-
21-
console.error(process.execPath, 'debug', 'localhost:5959');
14+
const interfacer = spawn(process.execPath, ['debug', `localhost:${PORT}`]);
2215
interfacer.stdout.setEncoding('utf-8');
16+
17+
// fail the test if either of the processes exit normally
18+
const debugBreakExit = common.fail.bind(null, 'child should not exit normally');
19+
const debugExit = common.fail.bind(null, 'interfacer should not exit normally');
20+
child.on('exit', debugBreakExit);
21+
interfacer.on('exit', debugExit);
22+
23+
let buffer = '';
24+
const expected = [
25+
`\bconnecting to localhost:${PORT} ... ok`,
26+
'\bbreak in test/fixtures/empty.js:2'
27+
];
28+
const actual = [];
2329
interfacer.stdout.on('data', function(data) {
2430
data = (buffer + data).split('\n');
2531
buffer = data.pop();
@@ -30,22 +36,26 @@ interfacer.stdout.on('data', function(data) {
3036

3137
interfacer.on('line', function(line) {
3238
line = line.replace(/^(debug> *)+/, '');
33-
console.log(line);
34-
var expected = '\bconnecting to localhost:5959 ... ok';
35-
assert.ok(expected == line, 'Got unexpected line: ' + line);
39+
if (expected.includes(line)) {
40+
actual.push(line);
41+
}
3642
});
3743

3844
// allow time to start up the debugger
3945
setTimeout(function() {
40-
child.removeListener('exit', fail);
46+
// remove the exit handlers before killing the processes
47+
child.removeListener('exit', debugBreakExit);
48+
interfacer.removeListener('exit', debugExit);
49+
4150
child.kill();
42-
interfacer.removeListener('exit', fail);
4351
interfacer.kill();
44-
}, 2000);
52+
}, common.platformTimeout(2000));
4553

4654
process.on('exit', function() {
55+
// additional checks to ensure that both the processes were actually killed
4756
assert(child.killed);
4857
assert(interfacer.killed);
58+
assert.deepStrictEqual(actual, expected);
4959
});
5060

5161
interfacer.stderr.pipe(process.stderr);

0 commit comments

Comments
 (0)