Skip to content

Commit c070ba9

Browse files
committed
test: refactor few tests with collectStream/collectChildStreams utils
1 parent 42a65c2 commit c070ba9

File tree

4 files changed

+14
-36
lines changed

4 files changed

+14
-36
lines changed

test/addons/openssl-client-cert-engine/test.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const common = require('../../common');
3+
const { collectStream } = require('../../common/streams');
34
const fixture = require('../../common/fixtures');
45

56
if (!common.hasCrypto)
@@ -44,13 +45,7 @@ const server = https.createServer(serverOptions, common.mustCall((req, res) => {
4445
};
4546

4647
const req = https.request(clientOptions, common.mustCall((response) => {
47-
let body = '';
48-
response.setEncoding('utf8');
49-
response.on('data', (chunk) => {
50-
body += chunk;
51-
});
52-
53-
response.on('end', common.mustCall(() => {
48+
collectStream(response).then(common.mustCall((body) => {
5449
assert.strictEqual(body, 'hello world');
5550
server.close();
5651
}));

test/addons/openssl-key-engine/test.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const common = require('../../common');
3+
const { collectStream } = require('../../common/streams');
34
const fixture = require('../../common/fixtures');
45

56
if (!common.hasCrypto)
@@ -46,13 +47,7 @@ const server = https.createServer(serverOptions, common.mustCall((req, res) => {
4647
};
4748

4849
const req = https.request(clientOptions, common.mustCall((response) => {
49-
let body = '';
50-
response.setEncoding('utf8');
51-
response.on('data', (chunk) => {
52-
body += chunk;
53-
});
54-
55-
response.on('end', common.mustCall(() => {
50+
collectStream(response).then(common.mustCall((body) => {
5651
assert.strictEqual(body, 'hello world');
5752
server.close();
5853
}));

test/parallel/test-cli-node-options.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const assert = require('assert');
99
const exec = require('child_process').execFile;
1010
const { Worker } = require('worker_threads');
1111

12+
const { collectStream } = require('../common/streams');
1213
const tmpdir = require('../common/tmpdir');
1314
tmpdir.refresh();
1415

@@ -112,15 +113,6 @@ function expect(
112113
workerTest(opts, command, wantsError, test('worker'));
113114
}
114115

115-
async function collectStream(readable) {
116-
readable.setEncoding('utf8');
117-
let data = '';
118-
for await (const chunk of readable) {
119-
data += chunk;
120-
}
121-
return data;
122-
}
123-
124116
function workerTest(opts, command, wantsError, test) {
125117
let workerError = null;
126118
const worker = new Worker(command, {
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
'use strict';
22
const common = require('../common');
3+
const { collectChildStreams } = require('../common/streams');
34
const assert = require('assert');
5+
const { once } = require('events');
46
const { spawn } = require('child_process');
57

6-
function CheckNoSignalAndErrorCodeOne(code, signal) {
7-
assert.strictEqual(signal, null);
8-
assert.strictEqual(code, 1);
9-
}
10-
118
const child = spawn(process.execPath, [
129
'--trace-event-categories', 'madeup', '-e', 'throw new Error()'
1310
], { stdio: [ 'inherit', 'inherit', 'pipe' ] });
14-
child.on('exit', common.mustCall(CheckNoSignalAndErrorCodeOne));
1511

16-
let stderr;
17-
child.stderr.setEncoding('utf8');
18-
child.stderr.on('data', (chunk) => stderr += chunk);
19-
child.stderr.on('end', common.mustCall(() => {
20-
assert(stderr.includes('throw new Error()'), stderr);
21-
assert(!stderr.includes('Could not open trace file'), stderr);
22-
}));
12+
collectChildStreams(child, once(child, 'exit'))
13+
.then(common.mustCall(({ stderr, data: [code, signal] }) => {
14+
assert(stderr.includes('throw new Error()'), stderr);
15+
assert(!stderr.includes('Could not open trace file'), stderr);
16+
assert.strictEqual(signal, null);
17+
assert.strictEqual(code, 1);
18+
}));

0 commit comments

Comments
 (0)