Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/await-open-browser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webpack-dev-server": patch
---

Await `openBrowser` completion before resolving `server.start()`.
2 changes: 1 addition & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3170,7 +3170,7 @@ class Server {
async openBrowser(defaultOpenTarget) {
const open = (await import("open")).default;

Promise.all(
await Promise.all(
/** @type {NormalizedOpen[]} */
(this.options.open).map((item) => {
/**
Expand Down
25 changes: 25 additions & 0 deletions test/server/open-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,4 +920,29 @@ describe('"open" option', () => {
getInfrastructureLoggerSpy.mockRestore();
loggerWarnSpy.mockRestore();
});

it("should await asynchronous completion of open() before openBrowser() resolves", async () => {
let completed = false;
open.mockImplementation(
() =>
new Promise((resolve) => {
setTimeout(() => {
completed = true;
resolve();
}, 50);
}),
);

const server = new Server(
{
port,
open: true,
},
compiler,
);

await server.start();
expect(completed).toBe(true);
await server.stop();
Comment on lines +945 to +946

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Stability & Availability · Quick win

Stop the server when the assertion fails.

If the missing-await regression returns, Line 945 throws before server.stop() runs. The suite's afterEach only clears and restores the mock; it does not close this server. The failed test therefore leaves the listening server and compiler watcher active. (raw.githubusercontent.com)

Put the assertion inside try and call await server.stop() in finally. This preserves cleanup on both passing and failing assertions.

});
});