import { servicenowHtmlPlugin } from "../../src/vite/html";
type PluginRecord = Record;
// Stand-in for src/ui-page: it serialises the page, hands it to transformHtml,
// then emits the result with this.emitFile — the same order as the real plugin.
const mockUiPage: {
html: string;
transformHtml?: (html: string, fileName: string) => string;
} = { html: "Test" };
jest.mock("../../src/index", () => ({
uiPage: jest.fn(
(opts: { transformHtml?: (html: string, fileName: string) => string }) => {
mockUiPage.transformHtml = opts.transformHtml;
return {
options: jest.fn().mockReturnValue({ input: "mocked" }),
buildStart: jest.fn(),
generateBundle: jest.fn(function (this: {
emitFile?: (file: Record) => string;
}) {
const source = mockUiPage.transformHtml
? mockUiPage.transformHtml(mockUiPage.html, "index.html")
: mockUiPage.html;
this.emitFile?.({
type: "asset",
fileName: "index.html",
source,
});
}),
};
},
),
}));
const simulateMode = (plugin: PluginRecord, command: "serve" | "build") => {
plugin.configResolved({ command });
};
const makeChunk = (
fileName: string,
opts: {
isEntry?: boolean;
importedCss?: string[];
importedCssUrls?: Map;
} = {},
) => ({
type: "chunk" as const,
fileName,
isEntry: opts.isEntry ?? false,
code: "",
viteMetadata: {
importedCss: new Set(opts.importedCss ?? []),
importedCssUrls: opts.importedCssUrls,
},
});
const HTML = mockUiPage.html;
// Rollup adds assets emitted during generateBundle to the same `bundle` object
// the hook was handed; Rolldown (the bundler behind Vite 8) does not. Both are
// exercised so the plugin can never regress into depending on that difference.
type EmitFlavor = "rollup" | "rolldown";
const EMIT_FLAVORS: EmitFlavor[] = ["rollup", "rolldown"];
const runGenerateBundle = (
plugin: PluginRecord,
bundle: Record,
flavor: EmitFlavor,
): Array<{ fileName: string; source: string }> => {
const emitted: Array<{ fileName: string; source: string }> = [];
const ctx = {
emitFile(file: { fileName: string; source: string }) {
emitted.push({ fileName: file.fileName, source: file.source });
if (flavor === "rollup") {
bundle[file.fileName] = { type: "asset", ...file };
}
return "ref";
},
};
plugin.generateBundle.call(ctx, {}, bundle, false);
return emitted;
};
describe("servicenowHtmlPlugin", () => {
const defaultOptions = {
rootDir: "/app",
};
describe("transformIndexHtml", () => {
test("replaces sdk:now-ux-globals tag in serve mode", () => {
const plugin = servicenowHtmlPlugin(defaultOptions) as PluginRecord;
simulateMode(plugin, "serve");
const html = "";
const result = plugin.transformIndexHtml(html);
expect(result).toContain('