forked from FuelLabs/fuels-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vitest.global-browser-setup.ts
61 lines (54 loc) · 1.59 KB
/
vitest.global-browser-setup.ts
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
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-floating-promises */
import { spawn } from 'node:child_process';
export default function setup() {
return new Promise((resolve, reject) => {
const server = {
closed: false,
};
const teardown = async () => {
if (server.closed) {
return;
}
server.closed = true;
const serverUrl = `http://localhost:49342`;
try {
await fetch(`${serverUrl}/close-server`);
} catch (e) {
console.log('closing of server failed', e);
}
process.exit();
};
const cp = spawn('pnpm tsx packages/fuels/src/setup-launch-node-server.ts', {
detached: true,
shell: 'sh',
});
cp.stderr?.on('data', (chunk) => {
console.log(chunk.toString());
});
cp.stdout?.on('data', (data) => {
console.log(data.toString());
// Return teardown function to be called when tests finish
// It will kill the server
resolve(teardown);
});
cp.on('error', (err) => {
console.log(err);
// Ensure server is killed if there's an error
teardown();
reject(err);
});
cp.on('exit', (code, signal) => {
console.log('error code', code, signal);
if (code !== 0) {
reject(new Error(`Server process exited with code ${code}`));
}
});
process.on('SIGINT', teardown);
process.on('SIGUSR1', teardown);
process.on('SIGUSR2', teardown);
process.on('uncaughtException', teardown);
process.on('unhandledRejection', teardown);
process.on('beforeExit', teardown);
});
}