/
test-repl-built-in-modules.js
57 lines (43 loc) · 1.47 KB
/
test-repl-built-in-modules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const spawn = require('child_process').spawn;
const path = require('path');
tmpdir.refresh();
const requirePath = JSON.stringify(path.join(tmpdir.path, 'non-existent.json'));
function run(wasiPreview) {
let resolve;
const promise = new Promise((_resolve) => {
resolve = _resolve;
});
let out = '';
// Use -i to force node into interactive mode, despite stdout not being a TTY
const args = ['-i'];
if (wasiPreview) args.push('--experimental-wasi-unstable-preview1');
const child = spawn(process.execPath, args);
const input = `require('events');
require('wasi');`;
let stdout = '';
let stderr = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (c) => stdout += c);
child.stderr.setEncoding('utf8');
child.stderr.on('data', (c) => stderr += c);
child.stdin.end(input);
child.on('exit', () => {
resolve({ stdout, stderr });
});
return promise;
}
(async function() {
const ret1 = await run(false);
const ret2 = await run(true);
assert(/\[Function: EventEmitter\] {/.test(ret1.stdout));
assert(/Uncaught Error: Cannot find module 'wasi'[\w\W]+- <repl>\n/.test(
ret1.stdout));
assert(/\[Function: EventEmitter\] {/.test(ret2.stdout));
assert(!/Uncaught Error: Cannot find module 'wasi'[\w\W]+- <repl>\n/.test(
ret2.stdout));
assert(/{ WASI: \[class WASI\] }/.test(ret2.stdout));
})();