Skip to content

Commit

Permalink
test(timeout): fix test timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
srod committed Nov 11, 2024
1 parent 05eb686 commit 34f9df8
Showing 1 changed file with 64 additions and 23 deletions.
87 changes: 64 additions & 23 deletions tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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<void> => {
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<MinifyResult> => {
return new Promise<MinifyResult>((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<string, { it: string; minify: Settings }[]>;

const tests: Tests = {
Expand Down

0 comments on commit 34f9df8

Please sign in to comment.