|
1 | 1 | "use strict"; |
2 | 2 |
|
| 3 | +const os = require("node:os"); |
| 4 | +const path = require("node:path"); |
3 | 5 | const webpack = require("webpack"); |
4 | 6 | const Server = require("../../lib/Server"); |
5 | 7 | const config = require("../fixtures/client-config/webpack.config"); |
@@ -95,6 +97,42 @@ describe("API (plugin)", () => { |
95 | 97 | stopSpy.mockRestore(); |
96 | 98 | }); |
97 | 99 |
|
| 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 | + |
98 | 136 | describe("plugin in webpack config", () => { |
99 | 137 | it("should work when added to webpack config plugins array", async () => { |
100 | 138 | const server = new Server({ port }); |
|
0 commit comments