Skip to content

Commit a185447

Browse files
committed
test: add test for passive behavior in build mode with compiler.run
1 parent 73a2468 commit a185447

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

test/e2e/api-plugin.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict";
22

3+
const os = require("node:os");
4+
const path = require("node:path");
35
const webpack = require("webpack");
46
const Server = require("../../lib/Server");
57
const config = require("../fixtures/client-config/webpack.config");
@@ -95,6 +97,42 @@ describe("API (plugin)", () => {
9597
stopSpy.mockRestore();
9698
});
9799

100+
it("should stay passive in build mode (compiler.run)", async () => {
101+
// The shared fixture writes output to "/", which would be unwritable
102+
// outside of webpack-dev-middleware's in-memory FS. Use a tmp dir so the
103+
// real `compiler.run()` can flush its assets.
104+
const compiler = webpack({
105+
...config,
106+
output: {
107+
...config.output,
108+
path: path.join(os.tmpdir(), `wds-build-mode-${Date.now()}`),
109+
},
110+
});
111+
const server = new Server({ port });
112+
const setupSpy = jest.spyOn(server, "setup");
113+
const listenSpy = jest.spyOn(server, "listen");
114+
115+
server.apply(compiler);
116+
117+
// `compiler.run()` is a one-shot build (no watch). The plugin must stay
118+
// passive so the build can finish and the process can exit normally.
119+
await new Promise((resolve, reject) => {
120+
compiler.run((error) => {
121+
if (error) reject(error);
122+
else resolve();
123+
});
124+
});
125+
126+
expect(setupSpy).not.toHaveBeenCalled();
127+
expect(listenSpy).not.toHaveBeenCalled();
128+
129+
setupSpy.mockRestore();
130+
listenSpy.mockRestore();
131+
await new Promise((resolve) => {
132+
compiler.close(resolve);
133+
});
134+
});
135+
98136
describe("plugin in webpack config", () => {
99137
it("should work when added to webpack config plugins array", async () => {
100138
const server = new Server({ port });

0 commit comments

Comments
 (0)