/
integration.test.js
109 lines (93 loc) · 2.85 KB
/
integration.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
96
97
98
99
100
101
102
103
104
105
106
107
108
const os = require("os");
const fs = require("fs");
const path = require("path");
const coverage = global.coverage;
// the twilio test can take a while (large codebase)
jest.setTimeout(200000);
let nccRun;
if (coverage) {
nccRun = require(__dirname + "/../src/cli.js");
}
else {
nccRun = require(__dirname + "/../dist/ncc/cli.js");
}
const { Writable } = require('stream');
class StoreStream extends Writable {
constructor (options) {
super(options);
this.data = [];
}
_write(chunk, encoding, callback) {
this.data.push(chunk);
callback();
}
}
it('should have a test', function () {
expect('hello world');
});
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 execute "ncc run ${integrationTest}"`, async () => {
let expectedStdout;
try {
expectedStdout = fs.readFileSync(`${__dirname}/integration/${integrationTest}.stdout`).toString();
}
catch (e) {}
if (global.gc) global.gc();
const stdout = new StoreStream();
const stderr = new StoreStream();
try {
await nccRun(["run", "--no-cache", "--no-asset-builds", `${__dirname}/integration/${integrationTest}`], stdout, stderr);
}
catch (e) {
if (e.silent) {
let lastErr = stderr.data[stderr.data.length - 1];
if (lastErr)
throw new Error(lastErr);
else
throw new Error('Process exited with code ' + e.exitCode);
}
throw e;
}
if (expectedStdout) {
let stdoutStr = '';
for (const chunk of stdout.data)
stdoutStr += chunk.toString();
expect(stdoutStr.startsWith(expectedStdout));
}
});
}
it(`should execute "ncc build web-vitals" with target config`, async () => {
if (global.gc) global.gc();
const stdout = new StoreStream();
const stderr = new StoreStream();
const tmpOut = path.join(os.tmpdir(), `ncc_${Math.random()}`)
try {
await nccRun(["build", "-o", tmpOut, "--target", "es5", require.resolve('web-vitals/dist/web-vitals.es5.min.js')], stdout, stderr);
}
catch (e) {
if (e.silent) {
let lastErr = stderr.data[stderr.data.length - 1];
if (lastErr)
throw new Error(lastErr);
else
throw new Error('Process exited with code ' + e.exitCode);
}
throw e;
}
const outFile = path.join(tmpOut, 'index.js')
const output = fs.readFileSync(outFile, 'utf8')
// cleanup tmp output
fs.unlinkSync(outFile)
fs.rmdirSync(tmpOut)
expect(output).toContain('function')
// make sure es6 wrapper wasn't used
expect(output).not.toContain('=>')
});
// remove me when node.js makes this the default behavior
process.on("unhandledRejection", e => {
throw e;
});