diff --git a/tests/fixtures.ts b/tests/fixtures.ts index d2c5c5b40..f085e9688 100644 --- a/tests/fixtures.ts +++ b/tests/fixtures.ts @@ -3,46 +3,87 @@ import { expect, test } from "vitest"; import minify from "../packages/core/src/index.ts"; import { filesCSS, filesHTML, filesJS, filesJSON } from "./files-path.ts"; +interface TestOptions { + it: string; + minify: Settings; +} + +interface TestConfig { + options: TestOptions; + compressorLabel: string; + compressor: any; + sync?: boolean; +} + +interface MinifyResult { + err: Error | null; + min: string; +} + const runOneTest = ({ options, compressorLabel, compressor, - sync, -}: { - options: { it: string; minify: Settings }; - compressorLabel: string; - compressor: any; - sync?: boolean; -}) => { + sync = false, +}: TestConfig): boolean => { if (!options) { return false; } - const clonedOptions = JSON.parse(JSON.stringify(options)); + const testOptions = createTestOptions(options, compressor, sync); + const testName = formatTestName(testOptions.it, compressorLabel); - clonedOptions.minify.compressor = compressor; + test(testName, () => executeMinifyTest(testOptions)); + + return true; +}; + +const createTestOptions = ( + options: TestOptions, + compressor: any, + sync: boolean +): TestOptions => { + const testOptions = JSON.parse(JSON.stringify(options)); + testOptions.minify.compressor = compressor; if (sync) { - clonedOptions.minify.sync = true; + testOptions.minify.sync = true; } - test(clonedOptions.it.replace( - "{compressor}", - compressorLabel - ), (): Promise => { - return new Promise<{ err: Error; min: string }>((resolve) => { - clonedOptions.minify.callback = (err: Error, min: string) => { - resolve({ err, min }); - }; + return testOptions; +}; + +const formatTestName = ( + testString: string, + compressorLabel: string +): string => { + return testString.replace("{compressor}", compressorLabel); +}; + +const executeMinifyTest = async (options: TestOptions): Promise => { + const result = await runMinify(options); - minify(clonedOptions.minify); - }).then(({ err, min }) => { - expect(err).toBeNull(); - expect(min).not.toBeNull(); - }); + validateMinifyResult(result); +}; + +const runMinify = (options: TestOptions): Promise => { + return new Promise((resolve) => { + options.minify.callback = (err: unknown, min?: string) => { + resolve({ + err: err instanceof Error ? err : null, + min: min || "", + }); + }; + + minify(options.minify); }); }; +const validateMinifyResult = (result: MinifyResult): void => { + expect(result.err).toBeNull(); + expect(result.min).not.toBeNull(); +}; + export type Tests = Record; const tests: Tests = {