Skip to content

Commit 8889cd7

Browse files
authored
chore: fix flaky Vitest tests in browser environment (#3883)
* include optimize deps * add try/catch block to server listen * add changeset
1 parent f778fe4 commit 8889cd7

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

.changeset/eighty-lies-end.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
---
3+
4+
chore: fix flaky Vitest tests in browser environment

packages/fuels/src/setup-launch-node-server.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,35 @@ server.on('request', async (req, res) => {
9696

9797
const port = process.argv[2] ? parseInt(process.argv[2], 10) : 49342;
9898

99-
server.listen(port);
100-
101-
server.on('listening', () => {
102-
const usedPort = (server.address() as AddressInfo).port;
103-
const serverUrl = `http://localhost:${usedPort}`;
104-
console.log(serverUrl);
105-
console.log(`Server is listening on: ${serverUrl}`);
106-
console.log("To launch a new fuel-core node and get its url, make a POST request to '/'.");
107-
console.log(
108-
"To kill the node, make a POST request to '/cleanup/<url>' where <url> is the url of the node you want to kill."
109-
);
110-
console.log('All nodes will be killed when the server is closed.');
111-
console.log('You can close the server by sending a request to /close-server.');
112-
});
99+
try {
100+
server.listen(port);
101+
102+
server.on('error', (error: NodeJS.ErrnoException) => {
103+
if (error.code === 'EADDRINUSE') {
104+
console.log(`Port ${port} is already in use. Another server may be running.`);
105+
process.exit(1);
106+
} else {
107+
console.error('Server error:', error);
108+
process.exit(1);
109+
}
110+
});
111+
112+
server.on('listening', () => {
113+
const usedPort = (server.address() as AddressInfo).port;
114+
const serverUrl = `http://localhost:${usedPort}`;
115+
console.log(serverUrl);
116+
console.log(`Server is listening on: ${serverUrl}`);
117+
console.log("To launch a new fuel-core node and get its url, make a POST request to '/'.");
118+
console.log(
119+
"To kill the node, make a POST request to '/cleanup/<url>' where <url> is the url of the node you want to kill."
120+
);
121+
console.log('All nodes will be killed when the server is closed.');
122+
console.log('You can close the server by sending a request to /close-server.');
123+
});
124+
} catch (err) {
125+
console.error('Error starting server:', err);
126+
process.exit(1);
127+
}
113128

114129
process.on('exit', closeServer('exit'));
115130
process.on('SIGINT', closeServer('SIGINT'));

vitest.browser.config.mts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@ const config: ViteUserConfig = {
3636
"@vitest/coverage-istanbul",
3737
"chromium-bidi",
3838
],
39-
include: ["events", "timers/promises"],
39+
include: [
40+
"events",
41+
"timers/promises",
42+
"vite-plugin-node-polyfills/shims/buffer",
43+
"vite-plugin-node-polyfills/shims/global",
44+
"vite-plugin-node-polyfills/shims/process",
45+
"memfs",
46+
"path",
47+
"os",
48+
"crypto",
49+
],
4050
entries: ["**/*.test.ts"],
4151
},
4252
test: {
@@ -51,9 +61,14 @@ const config: ViteUserConfig = {
5161
provider: "playwright",
5262
headless: true,
5363
enabled: true,
54-
name: "chromium",
5564
// Avoids taking screenshots
5665
screenshotFailures: false,
66+
instances: [
67+
{
68+
browser: "chromium",
69+
headless: true,
70+
},
71+
],
5772
},
5873
},
5974
};

0 commit comments

Comments
 (0)