import type { NormalizedOutputOptions, OutputBundle, OutputChunk, } from "rollup"; import { header } from "../../src/header"; /** Creates a minimal mock bundle with a single chunk. */ function mockBundle(code: string): OutputBundle { const chunk: OutputChunk = { type: "chunk", code, fileName: "test.js", map: null, } as unknown as OutputChunk; return { "test.js": chunk } as unknown as OutputBundle; } /** Runs the header plugin's generateBundle hook and returns the mutated chunk. */ function runHeader(headerValue: string, code: string): OutputChunk { const plugin = header(headerValue); const bundle = mockBundle(code); plugin.generateBundle.handler({} as NormalizedOutputOptions, bundle); return bundle["test.js"] as unknown as OutputChunk; } describe("Header plugin", () => { test("should prepend header to code", () => { const chunk = runHeader( "/* Copyright 2024 */\n", 'console.log("Hello, world!");', ); expect(chunk.code).toBe( '/* Copyright 2024 */\nconsole.log("Hello, world!");', ); expect(chunk.map).toMatchSnapshot(); }); test("should prepend multiline header", () => { const chunk = runHeader( "/*\n * Copyright 2024\n * All rights reserved\n */\n", "const x = 42;", ); expect(chunk.code).toBe( "/*\n * Copyright 2024\n * All rights reserved\n */\nconst x = 42;", ); }); test("should work with empty code", () => { const chunk = runHeader("// Header\n", ""); expect(chunk.code).toBe("// Header\n"); }); test("should work with empty header", () => { const chunk = runHeader("", 'console.log("test");'); expect(chunk.code).toBe('console.log("test");'); }); });