// src/index pulls in ESM-only transitive deps that this CJS jest project // cannot load, so the plugin barrel is mocked. That also sharpens the json() // regression guard below: if plugins.ts calls json() again, the mock records it. jest.mock("../../src/index", () => ({ seismic: jest.fn(() => ({ name: "seismic" })), replace: jest.fn(() => ({ name: "replace" })), json: jest.fn(() => ({ name: "json" })), swc: jest.fn(() => ({ name: "swc" })), minify: jest.fn(() => ({ name: "swc-minify" })), header: jest.fn(() => ({ name: "header" })), sourcemapUrl: jest.fn(() => ({ name: "sourcemap-url" })), outputOptions: jest.fn(() => ({ name: "output-options" })), collectSnTranslate: jest.fn(() => ({ name: "collect-sn-translate" })), uiPage: jest.fn(() => ({ options: jest.fn(), buildStart: jest.fn(), generateBundle: jest.fn(), })), })); import { json, swc } from "../../src/index"; import { servicenowVitePlugins } from "../../src/vite/plugins"; const makePlugins = (extra = {}) => servicenowVitePlugins({ scope: "x_test", rootDir: "/app", registerExplicitId: () => "ID0001", ...extra, }); beforeEach(() => { jest.clearAllMocks(); }); describe("servicenowVitePlugins", () => { test("every plugin in the composed set is named", async () => { const plugins = await makePlugins(); expect(plugins.length).toBeGreaterThan(0); for (const plugin of plugins) { expect(typeof plugin.name).toBe("string"); expect(plugin.name).not.toHaveLength(0); } }); // Regression guard. Vite has handled `.json` imports natively since v6 via // its core `vite:json` plugin, and core plugins transform before user // plugins, so @rollup/plugin-json receives already-generated ESM and dies // with "Could not parse JSON file". Verified against Vite 6.4.3, 7.3.6 and // 8.2.2: any `.json` import fails with json() present and succeeds without // it. Rolldown additionally reports it as PREFER_BUILTIN_FEATURE. json() // remains exported for standalone Rollup builds, which genuinely need it. test("does not include json() — vite handles .json natively", async () => { const plugins = await makePlugins(); expect(json).not.toHaveBeenCalled(); expect(plugins.map((p) => p.name)).not.toContain("json"); }); test("includes the servicenow plugins the build depends on", async () => { const names = (await makePlugins()).map((p) => p.name); expect(names).toEqual( expect.arrayContaining([ "servicenow-css-asset", "servicenow-html", "servicenow-image-asset", "collect-sn-translate", "swc", ]), ); }); test("includes swc by default", async () => { const names = (await makePlugins()).map((p) => p.name); expect(names).toContain("swc"); expect(swc).toHaveBeenCalled(); }); test("excludes swc when swc option is false", async () => { const names = (await makePlugins({ swc: false })).map((p) => p.name); expect(names).not.toContain("swc"); }); });