|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const webpack = require("webpack"); |
| 4 | +const { test } = require("@playwright/test"); |
| 5 | +const { describe } = require("@playwright/test"); |
| 6 | +const { expect } = require("@playwright/test"); |
| 7 | +const { beforeEach, afterEach } = require("@playwright/test"); |
| 8 | +const jestMock = require("jest-mock"); |
| 9 | +const Server = require("../../lib/Server"); |
| 10 | +const config = require("../fixtures/simple-config/webpack.config"); |
| 11 | +const port = require("../ports-map")["setup-exit-signals-option"]; |
| 12 | + |
| 13 | +describe("setupExitSignals option", () => { |
| 14 | + describe("should handle 'SIGINT' and 'SIGTERM' signals", () => { |
| 15 | + let compiler; |
| 16 | + let server; |
| 17 | + let pageErrors; |
| 18 | + let consoleMessages; |
| 19 | + let doExit; |
| 20 | + let exitSpy; |
| 21 | + let stopCallbackSpy; |
| 22 | + let stdinResumeSpy; |
| 23 | + let closeCallbackSpy; |
| 24 | + |
| 25 | + const signals = ["SIGINT", "SIGTERM"]; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + compiler = webpack(config); |
| 29 | + |
| 30 | + server = new Server( |
| 31 | + { |
| 32 | + setupExitSignals: true, |
| 33 | + port, |
| 34 | + }, |
| 35 | + compiler, |
| 36 | + ); |
| 37 | + |
| 38 | + await server.start(); |
| 39 | + |
| 40 | + pageErrors = []; |
| 41 | + consoleMessages = []; |
| 42 | + doExit = false; |
| 43 | + |
| 44 | + exitSpy = jestMock.spyOn(process, "exit").mockImplementation(() => { |
| 45 | + doExit = true; |
| 46 | + }); |
| 47 | + |
| 48 | + stdinResumeSpy = jestMock |
| 49 | + .spyOn(process.stdin, "resume") |
| 50 | + .mockImplementation(() => {}); |
| 51 | + |
| 52 | + stopCallbackSpy = jestMock.spyOn(server, "stopCallback"); |
| 53 | + |
| 54 | + if (server.compiler.close) { |
| 55 | + closeCallbackSpy = jestMock.spyOn(server.compiler, "close"); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + afterEach(async () => { |
| 60 | + exitSpy.mockReset(); |
| 61 | + stdinResumeSpy.mockReset(); |
| 62 | + signals.forEach((signal) => { |
| 63 | + process.removeAllListeners(signal); |
| 64 | + }); |
| 65 | + process.stdin.removeAllListeners("end"); |
| 66 | + await server.stop(); |
| 67 | + }); |
| 68 | + |
| 69 | + signals.forEach((signal) => { |
| 70 | + test(`should close and exit on ${signal}`, async ({ page }) => { |
| 71 | + page |
| 72 | + .on("console", (message) => { |
| 73 | + consoleMessages.push(message); |
| 74 | + }) |
| 75 | + .on("pageerror", (error) => { |
| 76 | + pageErrors.push(error); |
| 77 | + }); |
| 78 | + |
| 79 | + const response = await page.goto(`http://127.0.0.1:${port}/`, { |
| 80 | + waitUntil: "networkidle0", |
| 81 | + }); |
| 82 | + |
| 83 | + expect(JSON.stringify(response.status())).toMatchSnapshot(); |
| 84 | + |
| 85 | + process.emit(signal); |
| 86 | + |
| 87 | + await new Promise((resolve) => { |
| 88 | + const interval = setInterval(() => { |
| 89 | + if (doExit) { |
| 90 | + expect(stopCallbackSpy.mock.calls.length).toEqual(1); |
| 91 | + |
| 92 | + if (server.compiler.close) { |
| 93 | + expect(closeCallbackSpy.mock.calls.length).toEqual(1); |
| 94 | + } |
| 95 | + |
| 96 | + clearInterval(interval); |
| 97 | + |
| 98 | + resolve(); |
| 99 | + } |
| 100 | + }, 100); |
| 101 | + }); |
| 102 | + |
| 103 | + consoleMessages = consoleMessages.filter( |
| 104 | + (message) => |
| 105 | + !( |
| 106 | + message.text().includes("Trying to reconnect...") || |
| 107 | + message.text().includes("Disconnected") |
| 108 | + ), |
| 109 | + ); |
| 110 | + |
| 111 | + expect( |
| 112 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 113 | + ).toMatchSnapshot(); |
| 114 | + |
| 115 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments