forked from evanw/esbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-unref-tests.js
111 lines (94 loc) · 2.99 KB
/
node-unref-tests.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
109
110
111
// This test verifies that:
// - a running service will not prevent NodeJS to exit if there is no compilation in progress.
// - the NodeJS process will continue running if there is a serve() active or a transform or build in progress.
const assert = require('assert')
const { fork } = require('child_process');
// The tests to run in the child process
async function tests() {
const esbuild = require('./esbuild').installForTests()
async function testTransform() {
const t1 = await esbuild.transform(`1+2`)
const t2 = await esbuild.transform(`1+3`)
assert.strictEqual(t1.code, `1 + 2;\n`)
assert.strictEqual(t2.code, `1 + 3;\n`)
}
async function testServe() {
const context = await esbuild.context({})
try {
const server = await context.serve({})
assert.strictEqual(server.host, '0.0.0.0')
assert.strictEqual(typeof server.port, 'number')
} finally {
await context.dispose()
}
}
async function testBuild() {
const context = await esbuild.context({
stdin: { contents: '1+2' },
write: false,
})
try {
const result = await context.rebuild()
assert.deepStrictEqual(result.outputFiles.length, 1);
assert.deepStrictEqual(result.outputFiles[0].text, '1 + 2;\n');
const result2 = await context.rebuild()
assert.deepStrictEqual(result2.outputFiles.length, 1);
assert.deepStrictEqual(result2.outputFiles[0].text, '1 + 2;\n');
} finally {
await context.dispose()
}
}
async function testWatchAndIncremental() {
const context = await esbuild.context({
stdin: { contents: '1+2' },
write: false,
})
try {
await context.watch()
const result = await context.rebuild()
assert.deepStrictEqual(result.outputFiles.length, 1);
assert.deepStrictEqual(result.outputFiles[0].text, '1 + 2;\n');
} finally {
await context.dispose()
}
}
await testTransform()
await testServe()
await testBuild()
await testWatchAndIncremental()
}
// Called when this is the child process to run the tests.
function runTests() {
process.exitCode = 1;
tests().then(() => {
process.exitCode = 0;
}, (error) => {
console.error('❌', error)
});
}
// A child process need to be started to verify that a running service is not hanging node.
function startChildProcess() {
const child = fork(__filename, ['__forked__'], { stdio: 'inherit', env: process.env });
const timeout = setTimeout(() => {
console.error('❌ node unref test timeout - child_process.unref() broken?')
process.exit(1);
}, 5 * 60 * 1000);
child.on('error', (error) => {
console.error('❌', error);
process.exit(1);
})
child.on('exit', (code) => {
clearTimeout(timeout);
if (code) {
console.error(`❌ node unref tests failed: child exited with code ${code}`)
process.exit(1);
} else {
console.log(`✅ node unref tests passed`)
}
})
}
if (process.argv[2] === '__forked__') {
runTests();
} else {
startChildProcess();
}