jest.mock("../../src/dev-server/index", () => ({ __esModule: true, default: jest.fn((config: any) => ({ name: "dev", ...config })), })); const mockLivereloadServer = { watch: jest.fn(), close: jest.fn(), once: jest.fn(), server: { once: jest.fn((event: string, cb: () => void) => cb()) }, }; jest.mock( "livereload", () => ({ createServer: jest.fn(() => mockLivereloadServer) }), { virtual: true }, ); import type { IsomorphicRollupFs } from "../../src/fs-iface"; import { devServerPlugins } from "../../src/servicenow/dev-server-plugins"; const fs = { mkdir: jest.fn() } as unknown as IsomorphicRollupFs; const makeCredential = (url = "https://dev12345.service-now.com/") => ({ getUrl: () => new URL(url), getHeaders: async () => ({ Authorization: "Bearer token123" }), }); const getDevServerMock = () => require("../../src/dev-server/index").default as jest.Mock; describe("devServerPlugins", () => { let consoleWarnSpy: jest.SpyInstance; beforeEach(() => { jest.clearAllMocks(); consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); }); afterEach(() => { consoleWarnSpy.mockRestore(); }); describe("plugin structure", () => { test("should return undefined when watchPaths is empty", async () => { const result = await devServerPlugins({ fs, credential: undefined, proxyPaths: [], watchPaths: [], }); expect(result).toBeUndefined(); }); test("should warn when watchPaths is empty", async () => { await devServerPlugins({ fs, credential: undefined, proxyPaths: [], watchPaths: [], }); expect(consoleWarnSpy).toHaveBeenCalledWith( "No watch paths provided for dev server. Please provide at least one path to watch for changes.", ); }); test("should return an array of two plugins when watchPaths is provided", async () => { const result = await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/static"], }); expect(Array.isArray(result)).toBe(true); expect(result).toHaveLength(2); }); test("should return devServer as first plugin and livereload as second", async () => { const result = await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/static"], }); expect(result![0].name).toBe("dev"); expect(result![1].name).toBe("livereload"); }); }); describe("watchPaths handling", () => { test("should pass watchPaths as dirs to the dev server", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/esm"], }); expect(getDevServerMock()).toHaveBeenCalledWith( expect.objectContaining({ dirs: ["dist/esm"] }), ); }); test("should pass multiple watchPaths as dirs", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/esm", "dist/cjs"], }); expect(getDevServerMock()).toHaveBeenCalledWith( expect.objectContaining({ dirs: ["dist/esm", "dist/cjs"] }), ); }); test("should start the dev server on port 3000", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/static"], }); expect(getDevServerMock()).toHaveBeenCalledWith( expect.objectContaining({ port: 3000 }), ); }); test("should pass httpProxy to the dev server when provided", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/static"], httpProxy: "http://proxy.corp.example.com:8080", }); expect(getDevServerMock()).toHaveBeenCalledWith( expect.objectContaining({ httpProxy: "http://proxy.corp.example.com:8080", }), ); }); test("should not include httpProxy in dev server config when omitted", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/static"], }); const [config] = getDevServerMock().mock.calls[0]; expect(config).not.toHaveProperty("httpProxy"); }); test("should configure livereload to watch the watchPaths", async () => { const { createServer } = require("livereload"); await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/esm"], }); expect(createServer).toHaveBeenCalledWith( expect.objectContaining({ port: expect.any(Number) }), ); expect(mockLivereloadServer.watch).toHaveBeenCalledWith([ require("path").resolve(process.cwd(), "dist/esm"), ]); }); test("should inject the livereload client script into the bundle", async () => { const result = await devServerPlugins({ fs, credential: undefined, proxyPaths: [], watchPaths: ["dist/esm"], }); const banner = await (result![1] as any).banner(); expect(banner).toContain("livereload.js?snipver=1"); }); }); describe("credential handling", () => { test("should warn when credential is undefined and proxyPaths is non-empty", async () => { await devServerPlugins({ fs, credential: undefined, proxyPaths: ["/api"], watchPaths: ["dist/static"], }); expect(consoleWarnSpy).toHaveBeenCalledWith( "credential not provided. Dev server will start without proxying to ServiceNow instance.", ); }); test("should not warn when credential is undefined and proxyPaths is also empty", async () => { await devServerPlugins({ fs, credential: undefined, proxyPaths: [], watchPaths: ["dist/static"], }); expect(consoleWarnSpy).not.toHaveBeenCalled(); }); test("should warn when credential is missing getUrl", async () => { await devServerPlugins({ fs, credential: { getHeaders: async () => ({}) } as any, proxyPaths: [], watchPaths: ["dist/static"], }); expect(consoleWarnSpy).toHaveBeenCalled(); }); test("should warn when credential is missing getHeaders", async () => { await devServerPlugins({ fs, credential: { getUrl: () => new URL("https://dev.service-now.com"), } as any, proxyPaths: [], watchPaths: ["dist/static"], }); expect(consoleWarnSpy).toHaveBeenCalled(); }); test("should not warn when a valid credential is provided", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/static"], }); expect(consoleWarnSpy).not.toHaveBeenCalled(); }); }); describe("proxy configuration", () => { test("should create a proxy entry for each proxyPath", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: ["/api/now", "/x_app"], watchPaths: ["dist/static"], }); const [{ proxy }] = getDevServerMock().mock.calls[0]; expect(proxy).toHaveLength(2); expect(proxy[0].from).toBe("/api/now"); expect(proxy[1].from).toBe("/x_app"); }); test("should build proxy target from credential URL with trailing slash stripped", async () => { await devServerPlugins({ fs, credential: makeCredential("https://dev12345.service-now.com/"), proxyPaths: ["/api"], watchPaths: ["dist/static"], }); const [{ proxy }] = getDevServerMock().mock.calls[0]; expect(proxy[0].to).toBe("https://dev12345.service-now.com/api"); }); test("should add websocket:true for the /amb path", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: ["/amb", "/api"], watchPaths: ["dist/static"], }); const [{ proxy }] = getDevServerMock().mock.calls[0]; const ambEntry = proxy.find((p: any) => p.from === "/amb"); const apiEntry = proxy.find((p: any) => p.from === "/api"); expect(ambEntry.opts.websocket).toBe(true); expect(apiEntry.opts.websocket).toBeUndefined(); }); test("should call getHeaders exactly once regardless of number of proxy paths", async () => { const getHeaders = jest .fn() .mockResolvedValue({ Authorization: "Bearer token123" }); await devServerPlugins({ fs, credential: { getUrl: () => new URL("https://dev.service-now.com"), getHeaders, }, proxyPaths: ["/api", "/amb", "/scripts/"], watchPaths: ["dist/static"], }); expect(getHeaders).toHaveBeenCalledTimes(1); }); test("should build proxy target correctly when credential URL has no trailing slash", async () => { await devServerPlugins({ fs, credential: makeCredential("https://dev12345.service-now.com"), proxyPaths: ["/api"], watchPaths: ["dist/static"], }); const [{ proxy }] = getDevServerMock().mock.calls[0]; expect(proxy[0].to).toBe("https://dev12345.service-now.com/api"); }); test("should pass an empty proxy array when credential is provided but proxyPaths is empty", async () => { await devServerPlugins({ fs, credential: makeCredential(), proxyPaths: [], watchPaths: ["dist/static"], }); const [{ proxy }] = getDevServerMock().mock.calls[0]; expect(proxy).toHaveLength(0); }); test("should auth headers override conflicting keys in incoming request headers", async () => { await devServerPlugins({ fs, credential: { getUrl: () => new URL("https://dev.service-now.com"), getHeaders: async () => ({ Authorization: "Bearer fresh-token" }), }, proxyPaths: ["/api"], watchPaths: ["dist/static"], }); const [{ proxy }] = getDevServerMock().mock.calls[0]; const rewrite = proxy[0].opts.replyOptions.rewriteRequestHeaders; const result = rewrite({} as Request, { Authorization: "Bearer stale-token", }); expect(result.Authorization).toBe("Bearer fresh-token"); }); test("should merge auth headers into existing request headers", async () => { await devServerPlugins({ fs, credential: { getUrl: () => new URL("https://dev.service-now.com"), getHeaders: async () => ({ Authorization: "Bearer token123", "X-Custom": "header", }), }, proxyPaths: ["/api"], watchPaths: ["dist/static"], }); const [{ proxy }] = getDevServerMock().mock.calls[0]; const rewrite = proxy[0].opts.replyOptions.rewriteRequestHeaders; const result = rewrite({} as Request, { "Content-Type": "application/json", }); expect(result).toEqual({ "Content-Type": "application/json", Authorization: "Bearer token123", "X-Custom": "header", }); }); test("should pass an empty proxy array when no credential is provided", async () => { await devServerPlugins({ fs, credential: undefined, proxyPaths: ["/api"], watchPaths: ["dist/static"], }); const [{ proxy }] = getDevServerMock().mock.calls[0]; expect(proxy).toHaveLength(0); }); }); describe("missing optional dependencies", () => { afterEach(() => { jest.resetModules(); }); test("should throw when dev-server fails to load", async () => { jest.resetModules(); jest.doMock( "../../src/dev-server/index", () => { throw Object.assign(new Error("Cannot find module"), { code: "MODULE_NOT_FOUND", }); }, { virtual: true }, ); jest.doMock("livereload", () => ({ createServer: jest.fn() }), { virtual: true, }); const { devServerPlugins: freshFn, } = require("../../src/servicenow/dev-server-plugins"); await expect( freshFn({ credential: undefined, proxyPaths: [], watchPaths: ["dist/static"], }), ).rejects.toThrow("dev-server failed to load."); }); test("should throw when livereload is not installed", async () => { jest.resetModules(); jest.doMock( "../../src/dev-server/index", () => ({ __esModule: true, default: jest.fn() }), { virtual: true }, ); jest.doMock( "livereload", () => { throw Object.assign(new Error("Cannot find module 'livereload'"), { code: "MODULE_NOT_FOUND", }); }, { virtual: true }, ); const { devServerPlugins: freshFn, } = require("../../src/servicenow/dev-server-plugins"); await expect( freshFn({ credential: undefined, proxyPaths: [], watchPaths: ["dist/static"], }), ).rejects.toThrow( "livereload is not installed or failed to load. Please install it to enable the dev server.", ); }); }); });