-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesting.test.js
More file actions
50 lines (42 loc) · 1.81 KB
/
testing.test.js
File metadata and controls
50 lines (42 loc) · 1.81 KB
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
const fs = require('fs');
const cp = require('child_process');
const path = require('path');
const testDir = 'test_data';
// Normalize string.
function normalizeStr(text) {
return text.replace(/\r\n/g, '\n').trim();
}
function makeTest(filename) {
test(filename, () => {
// Note that cp.execSync throws on a non-zero exit code.
let jsPath = `${testDir}/${filename}`;
// Replace the extension .js or .notwasm with .wasm.
let wasmPath = jsPath.replace(/\.js$|\.notwasm$/, '.wasm');
let expectedOutputPath = jsPath.replace(/\.js$|\.notwasm$/, '.txt');
// Use -j for jankyscript output
// Use -n for notwasm output
let root = path.dirname(path.dirname(__dirname));
let jankscriptenPath = path.join(root, 'target', 'debug', 'jankscripten');
cp.spawnSync(jankscriptenPath, ['compile', '-o', path.join(wasmPath), path.join(jsPath)], { stdio: 'inherit' });
let output;
try {
output = String(cp.execSync(`node --experimental-wasm-return_call ../bin/run-node ${wasmPath}`, { stderr: 'inherit' })).trim();
} catch (e) {
// jest is very stubborn about printing in the right spot only
// if it's a thrown error, and the captured stdout won't be seen
// if we don't stick it in here
throw new Error(
`stdout:
${e.stdout}
stderr:
${e.stderr}`);
}
output = normalizeStr(output);
let expectedOutput = normalizeStr(String(fs.readFileSync(expectedOutputPath)));
expect(output).toBe(expectedOutput);
fs.unlinkSync(wasmPath);
});
}
fs.readdirSync(testDir)
.filter(filename => filename.endsWith('.js') || filename.endsWith('.notwasm'))
.forEach(filename => makeTest(filename));