import type { InputOptions, NullValue, OutputOptions, Plugin, PluginContext, } from "rollup"; import { inputOptions, outputOptions } from "../../src/options"; describe("Options plugins", () => { describe("inputOptions plugin", () => { const testInputOptions = async ( updateFn: (options: InputOptions) => InputOptions | NullValue, rawOptions: InputOptions, ): Promise => { const plugin: Plugin = inputOptions(updateFn); if (typeof plugin.options !== "function") { throw new Error("Plugin does not have an options hook"); } const result = await plugin.options.call({} as PluginContext, rawOptions); return result; }; test("should modify input options", async () => { const updateFn = (options: InputOptions) => ({ ...options, external: ["react"], }); const rawOptions: InputOptions = { input: "src/index.ts", }; const result = await testInputOptions(updateFn, rawOptions); expect(result).toEqual({ input: "src/index.ts", external: ["react"], }); }); test("should return null if update function returns null", async () => { const updateFn = () => null; const rawOptions: InputOptions = { input: "src/index.ts", }; const result = await testInputOptions(updateFn, rawOptions); expect(result).toBeNull(); }); test("should preserve original options when not modified", async () => { const updateFn = (options: InputOptions) => options; const rawOptions: InputOptions = { input: "src/index.ts", external: ["lodash"], treeshake: false, }; const result = await testInputOptions(updateFn, rawOptions); expect(result).toEqual(rawOptions); }); }); describe("outputOptions plugin", () => { const testOutputOptions = async ( updateFn: (options: OutputOptions) => OutputOptions | NullValue, rawOptions: OutputOptions, ): Promise => { const plugin: Plugin = outputOptions(updateFn); if (typeof plugin.outputOptions !== "function") { throw new Error("Plugin does not have an outputOptions hook"); } const result = await plugin.outputOptions.call( {} as PluginContext, rawOptions, ); return result; }; test("should modify output options", async () => { const updateFn = (options: OutputOptions) => ({ ...options, format: "es" as const, }); const rawOptions: OutputOptions = { file: "dist/bundle.js", }; const result = await testOutputOptions(updateFn, rawOptions); expect(result).toEqual({ file: "dist/bundle.js", format: "es", }); }); test("should return null if update function returns null", async () => { const updateFn = () => null; const rawOptions: OutputOptions = { file: "dist/bundle.js", }; const result = await testOutputOptions(updateFn, rawOptions); expect(result).toBeNull(); }); test("should preserve original options when not modified", async () => { const updateFn = (options: OutputOptions) => options; const rawOptions: OutputOptions = { file: "dist/bundle.js", format: "cjs", sourcemap: true, }; const result = await testOutputOptions(updateFn, rawOptions); expect(result).toEqual(rawOptions); }); }); });