Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test server error handling #1824

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/support/runPlaywrightTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const playwrightBrowsers = ['chromium', 'firefox', 'webkit'];
const mochaServer = new MochaServer(/* playwrightTest: */ true);

const runTests = async (browserType) => {
mochaServer.listen();
await mochaServer.listen();
const browser = await browserType.launch();
const context = await browser.newContext();
const page = await context.newPage();
Expand Down
23 changes: 20 additions & 3 deletions test/web_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var express = require('express'),
*/

class MochaServer {
servers = [];

constructor(playwrightTest) {
this.playwrightTest = playwrightTest;
}
Expand Down Expand Up @@ -46,13 +48,28 @@ class MochaServer {
app.use(express.static(__dirname));

const port = process.env.PORT || 3000;
this.server = app.listen(port);
// Explicitly listen on the IPv4 and IPv6 loopback interfaces. If you don’t
// pass an address to `app.listen`, then it will bind in a manner that
// succeeds even if the IPv4 socket address is already in use, as long as
// the IPv6 socket address is free. This can lead to confusion.
await this.startServer(app, '127.0.0.1', port);
await this.startServer(app, '::1', port);

console.log(`Mocha test server listening on http://localhost:${port}/`);
}

console.log('Mocha test server listening on http://localhost:3000/');
async startServer(app, address, port) {
await new Promise((resolve, reject) => {
const server = app.listen(port, address, resolve);
this.servers.push(server);
server.once('error', reject);
});
}

close() {
this.server.close();
for (const server of this.servers) {
server.close();
}
}
}

Expand Down
Loading