/
index.test.js
95 lines (87 loc) · 2.95 KB
/
index.test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const fs = require("fs");
const ncc = global.coverage ? require("../src/index") : require("../");
const mkdirp = require("mkdirp");
const rimraf = require("rimraf");
const { dirname } = require("path");
for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
it(`should generate correct output for ${unitTest}`, async () => {
const testDir = `${__dirname}/unit/${unitTest}`;
const expected = fs
.readFileSync(`${testDir}/output.js`)
.toString()
.trim()
// Windows support
.replace(/\r/g, "");
// set env variable so tsconfig-paths can find the config
process.env.TS_NODE_PROJECT = `${testDir}/tsconfig.json`;
// find the name of the input file (e.g input.ts)
const inputFile = fs.readdirSync(testDir).find(file => file.includes("input"));
await ncc(`${testDir}/${inputFile}`, { minify: false }).then(
async ({ code, assets }) => {
// very simple asset validation in unit tests
if (unitTest.startsWith("asset-")) {
expect(Object.keys(assets).length).toBeGreaterThan(0);
expect(assets[Object.keys(assets)[0]] instanceof Buffer);
}
const actual = code
.trim()
// Windows support
.replace(/\r/g, "");
try {
expect(actual).toBe(expected);
} catch (e) {
// useful for updating fixtures
fs.writeFileSync(`${testDir}/actual.js`, actual);
throw e;
}
}
);
});
}
// the twilio test can take a while (large codebase)
jest.setTimeout(100000);
function clearDir (dir) {
try {
rimraf.sync(dir);
}
catch (e) {
if (e.code !== "ENOENT") throw e;
}
}
for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
// ignore e.g.: `.json` files
if (!/\.(mjs|tsx?|js)$/.test(integrationTest)) continue;
// disabled pending https://github.com/zeit/ncc/issues/141
if (integrationTest.endsWith('loopback.js')) continue;
it(`should evaluate ${integrationTest} without errors`, async () => {
const { code, map, assets } = await ncc(
__dirname + "/integration/" + integrationTest
);
const tmpDir = `${__dirname}/tmp/${integrationTest}/`;
clearDir(tmpDir);
mkdirp.sync(tmpDir);
for (const asset of Object.keys(assets)) {
const assetPath = tmpDir + asset;
mkdirp.sync(dirname(assetPath));
fs.writeFileSync(assetPath, assets[asset]);
}
fs.writeFileSync(tmpDir + "index.js", code);
fs.writeFileSync(tmpDir + "index.js.map", map);
await new Promise((resolve, reject) => {
const ps = require("child_process").fork(tmpDir + "index.js", {
execArgv: ["-r", "source-map-support/register.js"]
});
ps.on("close", (code) => {
if (code === 0)
resolve();
else
reject(new Error(`Test failed.`));
});
});
clearDir(tmpDir);
});
}
// remove me when node.js makes this the default behavior
process.on("unhandledRejection", e => {
throw e;
});