Skip to content

Commit c8a0d2e

Browse files
thefourtheyeMylesBorins
authored andcommitted
test: refactor test-init.js
1. Lot of repeating code has been refactored to a function 2. Errors in async calls are properly asserted 3. Fail the test if the callbacks are not fired PR-URL: #10384 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Julian Duque <[email protected]>
1 parent 529c96d commit c8a0d2e

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

test/sequential/test-init.js

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,40 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const child = require('child_process');
5-
const util = require('util');
5+
const path = require('path');
6+
67
if (process.env['TEST_INIT']) {
7-
util.print('Loaded successfully!');
8-
} else {
9-
// change CWD as we do this test so its not dependant on current CWD
10-
// being in the test folder
11-
process.chdir(__dirname);
8+
return process.stdout.write('Loaded successfully!');
9+
}
10+
11+
process.env.TEST_INIT = 1;
1212

13-
// slow but simple
14-
var envCopy = JSON.parse(JSON.stringify(process.env));
15-
envCopy.TEST_INIT = 1;
13+
function test(file, expected) {
14+
const path = `"${process.execPath}" ${file}`;
15+
child.exec(path, {env: process.env}, common.mustCall((err, out) => {
16+
assert.ifError(err);
17+
assert.strictEqual(out, expected, `'node ${file}' failed!`);
18+
}));
19+
}
1620

17-
child.exec('"' + process.execPath + '" test-init', {env: envCopy},
18-
function(err, stdout, stderr) {
19-
assert.equal(stdout, 'Loaded successfully!',
20-
'`node test-init` failed!');
21-
});
22-
child.exec('"' + process.execPath + '" test-init.js', {env: envCopy},
23-
function(err, stdout, stderr) {
24-
assert.equal(stdout, 'Loaded successfully!',
25-
'`node test-init.js` failed!');
26-
});
21+
{
22+
// change CWD as we do this test so it's not dependent on current CWD
23+
// being in the test folder
24+
process.chdir(__dirname);
25+
test('test-init', 'Loaded successfully!');
26+
test('test-init.js', 'Loaded successfully!');
27+
}
2728

29+
{
2830
// test-init-index is in fixtures dir as requested by ry, so go there
2931
process.chdir(common.fixturesDir);
32+
test('test-init-index', 'Loaded successfully!');
33+
}
3034

31-
child.exec('"' + process.execPath + '" test-init-index', {env: envCopy},
32-
function(err, stdout, stderr) {
33-
assert.equal(stdout, 'Loaded successfully!',
34-
'`node test-init-index failed!');
35-
});
36-
35+
{
3736
// ensures that `node fs` does not mistakenly load the native 'fs' module
3837
// instead of the desired file and that the fs module loads as
3938
// expected in node
40-
process.chdir(common.fixturesDir + '/test-init-native/');
41-
42-
child.exec('"' + process.execPath + '" fs', {env: envCopy},
43-
function(err, stdout, stderr) {
44-
assert.equal(stdout, 'fs loaded successfully',
45-
'`node fs` failed!');
46-
});
39+
process.chdir(path.join(common.fixturesDir, 'test-init-native'));
40+
test('fs', 'fs loaded successfully');
4741
}

0 commit comments

Comments
 (0)