Skip to content

Commit 16659ec

Browse files
authored
Split spawn into multiple files (#255)
- [`test/spawn`] Split `index` into multiple files - [`test/utils`] Replaced directory of files with a single module that contains two methods: `spawn` and `touchFile` - [`test/utils/run`] Moved `run` function directly into the `run` file
1 parent 0de2efe commit 16659ec

27 files changed

Lines changed: 408 additions & 336 deletions

test/run.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
const tap = require('tap');
22

3-
const run = require('./utils/run');
3+
const { spawn, touchFile } = require('./utils');
4+
5+
const run = (cmd, exit) => {
6+
return spawn(cmd, out => {
7+
let touched = false;
8+
if (!touched && out.match(/touch message\.js/)) {
9+
touchFile('message.js');
10+
touched = true;
11+
return out2 => {
12+
if (out2.match(/Restarting/)) {
13+
return { exit };
14+
}
15+
};
16+
}
17+
});
18+
};
419

520
tap.test('Restart the server', t => {
621
run('server.js', t.end.bind(t));

test/spawn/argv.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const tap = require('tap');
2+
3+
const { spawn } = require('../utils');
4+
5+
tap.test('should not show up in argv', t => {
6+
spawn('argv.js foo', out => {
7+
const argv = JSON.parse(out.replace(/'/g, '"'));
8+
t.match(argv[0], /.*?node(js|\.exe)?$/);
9+
t.equal(argv[1], 'argv.js');
10+
t.equal(argv[2], 'foo');
11+
return { exit: t.end.bind(t) };
12+
});
13+
});

test/spawn/caught.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const tap = require('tap');
2+
3+
const { spawn } = require('../utils');
4+
5+
tap.test('should ignore caught errors', t => {
6+
spawn('catch-no-such-module.js', out => {
7+
t.match(out, /Caught/);
8+
return { exit: t.end.bind(t) };
9+
});
10+
});

test/spawn/clear.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const tap = require('tap');
22

3-
const spawn = require('../utils/spawn');
4-
const touchFile = require('../utils/touch-file');
3+
const { spawn, touchFile } = require('../utils');
54

65
const { control } = require('../../lib/clear');
76

test/spawn/cli-require.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const tap = require('tap');
2+
3+
const { spawn, touchFile } = require('../utils');
4+
5+
tap.test('Supports require from the command-line (ts-node/register)', t => {
6+
spawn('--require=ts-node/register typescript/index.ts', out => {
7+
if (out.match(/touch message.js/)) {
8+
touchFile('message.js');
9+
return out2 => {
10+
if (out2.match(/Restarting/)) {
11+
t.match(out2, /\[INFO\] \d{2}:\d{2}:\d{2} Restarting/);
12+
return { exit: t.end.bind(t) };
13+
}
14+
};
15+
}
16+
});
17+
});

test/spawn/cluster.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const tap = require('tap');
2+
3+
const { spawn, touchFile } = require('../utils');
4+
5+
tap.test('Restart the cluster', t => {
6+
spawn('cluster.js', out => {
7+
if (out.match(/touch message\.js/m)) {
8+
touchFile('message.js');
9+
return out2 => {
10+
if (out2.match(/Restarting/m)) {
11+
return out3 => {
12+
if (out3.match(/All workers disconnected/m)) {
13+
let shuttingDown = false;
14+
return out4 => {
15+
if (out4.match(/touch message\.js/) && !shuttingDown) {
16+
shuttingDown = true;
17+
return { exit: t.end.bind(t) };
18+
}
19+
};
20+
}
21+
};
22+
}
23+
};
24+
}
25+
});
26+
});

test/spawn/conceal.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const tap = require('tap');
2+
3+
const { spawn } = require('../utils');
4+
5+
tap.test('should conceal the wrapper', t => {
6+
// require.main should be main.js not wrap.js!
7+
spawn('main.js').on('exit', code => {
8+
t.equal(code, 0);
9+
t.end();
10+
});
11+
});

test/spawn/errors.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const tap = require('tap');
2+
3+
const { spawn, touchFile } = require('../utils');
4+
5+
tap.test('should handle errors', t => {
6+
spawn('error.js', out => {
7+
if (out.match(/ERROR/)) {
8+
touchFile('message.js');
9+
return out2 => {
10+
if (out2.match(/Restarting/)) {
11+
return { exit: t.end.bind(t) };
12+
}
13+
};
14+
}
15+
});
16+
});
17+
18+
tap.test('should handle null errors', t => {
19+
spawn('error-null.js', out => {
20+
if (out.match(/ERROR/)) {
21+
touchFile('message.js');
22+
return out2 => {
23+
if (out2.match(/Restarting/)) {
24+
return { exit: t.end.bind(t) };
25+
}
26+
};
27+
}
28+
});
29+
});

test/spawn/esmodule.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const semver = require('semver');
2+
const tap = require('tap');
3+
4+
const { spawn, touchFile } = require('../utils');
5+
6+
tap.test('Supports ECMAScript modules with experimental-specifier-resolution', t => {
7+
if (semver.satisfies(process.version, '<12.17')) return t.skip();
8+
9+
spawn('--experimental-specifier-resolution=node resolution.mjs', out => {
10+
if (out.match(/touch message.js/)) {
11+
touchFile('message.js');
12+
return out2 => {
13+
if (out2.match(/Restarting/)) {
14+
t.match(out2, /\[INFO\] \d{2}:\d{2}:\d{2} Restarting/);
15+
return { exit: t.end.bind(t) };
16+
}
17+
};
18+
}
19+
});
20+
});
21+
22+
tap.test('Supports ECMAScript modules', t => {
23+
spawn('ecma-script-modules.mjs', out => {
24+
if (out.match(/touch message.mjs/)) {
25+
touchFile('message.mjs');
26+
return out2 => {
27+
if (out2.match(/Restarting/)) {
28+
t.match(out2, /\[INFO\] \d{2}:\d{2}:\d{2} Restarting/);
29+
return { exit: t.end.bind(t) };
30+
}
31+
};
32+
}
33+
});
34+
});

test/spawn/exit-code.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const tap = require('tap');
2+
3+
const { spawn } = require('../utils');
4+
5+
tap.test('should pass through the exit code', t => {
6+
spawn('exit.js').on('exit', code => {
7+
t.equal(code, 101);
8+
t.end();
9+
});
10+
});

0 commit comments

Comments
 (0)