import { afterEach, beforeEach, describe, it, mock } from "node:test"; import { expect } from "expect"; import { fn, spyOn } from "jest-mock"; import webpack from "webpack"; import Server from "../../lib/Server.js"; import config from "../fixtures/simple-config/webpack.config.js"; import portsMap from "../ports-map.js"; const port = portsMap["open-option"]; const internalIPv4 = Server.findIp("v4", false); describe('"open" option', () => { let compiler; let open; let openMock; beforeEach(async () => { compiler = webpack(config); openMock = mock.module("open", { defaultExport: fn(() => Promise.resolve()), }); open = (await import("open")).default; }); afterEach(async () => { open.mockClear(); openMock.restore(); }); it("should work with unspecified host", async () => { const server = new Server( { open: true, port, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://localhost:${port}/`, { wait: false, }); }); it("should work with the server: 'https' option", async () => { const server = new Server( { open: true, port, server: "https", }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`https://localhost:${port}/`, { wait: false, }); }); it("should work with '0.0.0.0' host but open localhost", async () => { const host = "0.0.0.0"; const server = new Server( { host, port, open: true, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://localhost:${port}/`, { wait: false, }); }); it("should work with '::' host", async () => { const host = "::"; const server = new Server( { host, port, open: true, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://localhost:${port}/`, { wait: false, }); }); it("should work with 'localhost' host", async () => { const host = "localhost"; const server = new Server( { host, port, open: true, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { wait: false, }); }); it("should work with '127.0.0.1' host", async () => { const host = "127.0.0.1"; const server = new Server( { host, port, open: true, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { wait: false, }); }); it("should work with '::1' host", async () => { const host = "::1"; const server = new Server( { host, port, open: true, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://[${host}]:${port}/`, { wait: false, }); }); it(`should work with '${internalIPv4}' host`, async () => { const server = new Server( { host: internalIPv4, port, open: true, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${internalIPv4}:${port}/`, { wait: false, }); }); it("should work with boolean", async () => { const host = "localhost"; const server = new Server( { host, port, open: true, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { wait: false, }); }); it("should work with boolean but don't close with 'false' value", async () => { const host = "localhost"; const server = new Server( { host, port, open: false, }, compiler, ); await server.start(); await server.stop(); expect(open).not.toHaveBeenCalled(); }); it("should work with relative string", async () => { const host = "localhost"; const server = new Server( { host, port, open: "index.html", }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/index.html`, { wait: false, }); }); it('should work with "" pattern', async () => { const host = "localhost"; const server = new Server( { host, port, open: "", }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { wait: false, }); }); it('should work with relative string starting with "/"', async () => { const host = "localhost"; const server = new Server( { host, port, open: "/index.html", }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/index.html`, { wait: false, }); }); it("should work with absolute string", async () => { const host = "localhost"; const server = new Server( { open: `http://${host}:${port}/index.html`, port, host: "localhost", }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/index.html`, { wait: false, }); }); it("should work with multiple relative strings", async () => { const host = "localhost"; const server = new Server( { host: "localhost", port, open: ["first.html", "second.html"], }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenNthCalledWith( 1, `http://${host}:${port}/first.html`, { wait: false, }, ); expect(open).toHaveBeenNthCalledWith( 2, `http://${host}:${port}/second.html`, { wait: false, }, ); }); it("should work with multiple absolute strings", async () => { const host = "localhost"; const server = new Server( { host: "localhost", port, open: [ `http://${host}:${port}/first.html`, `http://${host}:${port}/second.html`, ], }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenNthCalledWith( 1, `http://${host}:${port}/first.html`, { wait: false, }, ); expect(open).toHaveBeenNthCalledWith( 2, `http://${host}:${port}/second.html`, { wait: false, }, ); }); it('should work with "" pattern in multiple strings', async () => { const host = "localhost"; const server = new Server( { host: "localhost", port, open: ["", "second.html"], }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenNthCalledWith(1, `http://${host}:${port}/`, { wait: false, }); expect(open).toHaveBeenNthCalledWith( 2, `http://${host}:${port}/second.html`, { wait: false, }, ); }); it("should work with empty object", async () => { const host = "localhost"; const server = new Server( { host, port, open: {}, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { wait: false, }); }); it("should work with object and with the 'target' option", async () => { const host = "localhost"; const server = new Server( { host, port, open: { target: "index.html", }, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/index.html`, { wait: false, }); }); it("should work with object and with multiple values of the 'target' option", async () => { const host = "localhost"; const server = new Server( { host, port, open: { target: ["first.html", "second.html"], }, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenNthCalledWith( 1, `http://${host}:${port}/first.html`, { wait: false, }, ); expect(open).toHaveBeenNthCalledWith( 2, `http://${host}:${port}/second.html`, { wait: false, }, ); }); it("should work with object and with the 'app' option", async () => { const host = "localhost"; const server = new Server( { host, port, open: { app: "google-chrome", }, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { app: { name: "google-chrome" }, wait: false, }); }); it("should work with object and with the 'app' and 'arguments' options", async () => { const host = "localhost"; const server = new Server( { host, port, open: { app: { name: "google-chrome", arguments: ["--incognito"] }, }, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { app: { name: "google-chrome", arguments: ["--incognito"] }, wait: false, }); }); it('should work with object with "target" and "app" options', async () => { const host = "localhost"; const server = new Server( { host, port, open: { target: "index.html", app: "google-chrome", }, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/index.html`, { app: { name: "google-chrome" }, wait: false, }); }); it('should work with pattern in "target" and "app" options', async () => { const host = "localhost"; const server = new Server( { host, port, open: { target: "", app: "google-chrome", }, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { app: { name: "google-chrome" }, wait: false, }); }); it("should work with object, with multiple value of the 'target' option and with the 'app' and 'arguments' options", async () => { const host = "localhost"; const server = new Server( { host, port, open: { target: ["first.html", "second.html"], app: { name: "google-chrome", arguments: ["--incognito"] }, }, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenNthCalledWith( 1, `http://${host}:${port}/first.html`, { wait: false, app: { name: "google-chrome", arguments: ["--incognito"] }, }, ); expect(open).toHaveBeenNthCalledWith( 2, `http://${host}:${port}/second.html`, { wait: false, app: { name: "google-chrome", arguments: ["--incognito"] }, }, ); }); it("should work with object, with multiple value of the 'target' option (relative and absolute URLs) and with the 'app' option with arguments", async () => { const host = "localhost"; const server = new Server( { host, port, open: { target: ["first.html", `http://${host}:${port}/second.html`], app: { name: "google-chrome", arguments: ["--incognito"] }, }, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenNthCalledWith( 1, `http://${host}:${port}/first.html`, { wait: false, app: { name: "google-chrome", arguments: ["--incognito"] }, }, ); expect(open).toHaveBeenNthCalledWith( 2, `http://${host}:${port}/second.html`, { wait: false, app: { name: "google-chrome", arguments: ["--incognito"] }, }, ); }); it("should work with pattern in multiple open options", async () => { const host = "localhost"; const server = new Server( { host, port, open: [ { target: "", app: "google-chrome", }, { target: "", app: "firefox", }, ], }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { app: { name: "google-chrome" }, wait: false, }); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { app: { name: "firefox" }, wait: false, }); }); it("should work with multiple open options without target", async () => { const host = "localhost"; const server = new Server( { host, port, open: [ { app: "google-chrome", }, { app: "firefox", }, ], }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { app: { name: "google-chrome" }, wait: false, }); expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { app: { name: "firefox" }, wait: false, }); }); it("should log warning when can't open", async () => { open.mockRejectedValue(undefined); const loggerWarnSpy = fn(); const getInfrastructureLoggerSpy = spyOn( compiler, "getInfrastructureLogger", ).mockImplementation(() => ({ warn: loggerWarnSpy, info: () => {}, log: () => {}, })); const server = new Server( { port, open: true, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://localhost:${port}/`, { wait: false, }); expect(loggerWarnSpy).toHaveBeenCalledWith( `Unable to open "http://localhost:${port}/" page. If you are running in a headless environment, please do not use the "open" option or related flags like "--open", "--open-target", and "--open-app-name".`, ); getInfrastructureLoggerSpy.mockRestore(); loggerWarnSpy.mockRestore(); }); it("should log warning when can't open with string", async () => { open.mockRejectedValue(undefined); const loggerWarnSpy = fn(); const getInfrastructureLoggerSpy = spyOn( compiler, "getInfrastructureLogger", ).mockImplementation(() => ({ warn: loggerWarnSpy, info: () => {}, log: () => {}, })); const server = new Server( { open: "index.html", port, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://localhost:${port}/index.html`, { wait: false, }); expect(loggerWarnSpy).toHaveBeenCalledWith( `Unable to open "http://localhost:${port}/index.html" page. If you are running in a headless environment, please do not use the "open" option or related flags like "--open", "--open-target", and "--open-app-name".`, ); getInfrastructureLoggerSpy.mockRestore(); loggerWarnSpy.mockRestore(); }); it("should log warning when can't open with object", async () => { open.mockRejectedValue(undefined); const loggerWarnSpy = fn(); const getInfrastructureLoggerSpy = spyOn( compiler, "getInfrastructureLogger", ).mockImplementation(() => ({ warn: loggerWarnSpy, info: () => {}, log: () => {}, })); const server = new Server( { open: { target: "index.html", app: "google-chrome", }, port, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://localhost:${port}/index.html`, { app: { name: "google-chrome" }, wait: false, }); expect(loggerWarnSpy).toHaveBeenCalledWith( `Unable to open "http://localhost:${port}/index.html" page in "google-chrome" app. If you are running in a headless environment, please do not use the "open" option or related flags like "--open", "--open-target", and "--open-app-name".`, ); loggerWarnSpy.mockRestore(); getInfrastructureLoggerSpy.mockRestore(); }); it("should log warning when can't open with object with the 'app' option with arguments", async () => { open.mockRejectedValue(undefined); const loggerWarnSpy = fn(); const getInfrastructureLoggerSpy = spyOn( compiler, "getInfrastructureLogger", ).mockImplementation(() => ({ warn: loggerWarnSpy, info: () => {}, log: () => {}, })); const server = new Server( { open: { target: "index.html", app: { name: "google-chrome", arguments: ["--incognito", "--new-window"], }, }, port, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenCalledWith(`http://localhost:${port}/index.html`, { app: { name: "google-chrome", arguments: ["--incognito", "--new-window"], }, wait: false, }); expect(loggerWarnSpy).toHaveBeenCalledWith( `Unable to open "http://localhost:${port}/index.html" page in "google-chrome" app with "--incognito --new-window" arguments. If you are running in a headless environment, please do not use the "open" option or related flags like "--open", "--open-target", and "--open-app-name".`, ); getInfrastructureLoggerSpy.mockRestore(); loggerWarnSpy.mockRestore(); }); it("should log warning when can't open with object with the 'app' option with arguments #2", async () => { open.mockRejectedValue(undefined); const loggerWarnSpy = fn(); const getInfrastructureLoggerSpy = spyOn( compiler, "getInfrastructureLogger", ).mockImplementation(() => ({ warn: loggerWarnSpy, info: () => {}, log: () => {}, })); const server = new Server( { open: { target: ["first.html", `http://localhost:${port}/second.html`], app: { name: "google-chrome", arguments: ["--incognito", "--new-window"], }, }, port, }, compiler, ); await server.start(); await server.stop(); expect(open).toHaveBeenNthCalledWith( 1, `http://localhost:${port}/first.html`, { wait: false, app: { name: "google-chrome", arguments: ["--incognito", "--new-window"], }, }, ); expect(open).toHaveBeenNthCalledWith( 2, `http://localhost:${port}/second.html`, { wait: false, app: { name: "google-chrome", arguments: ["--incognito", "--new-window"], }, }, ); expect(loggerWarnSpy).toHaveBeenNthCalledWith( 1, `Unable to open "http://localhost:${port}/first.html" page in "google-chrome" app with "--incognito --new-window" arguments. If you are running in a headless environment, please do not use the "open" option or related flags like "--open", "--open-target", and "--open-app-name".`, ); expect(loggerWarnSpy).toHaveBeenNthCalledWith( 2, `Unable to open "http://localhost:${port}/second.html" page in "google-chrome" app with "--incognito --new-window" arguments. If you are running in a headless environment, please do not use the "open" option or related flags like "--open", "--open-target", and "--open-app-name".`, ); 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(); }); });