Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /* eslint-disable no-console */
import { promises as fs } from 'node:fs';
import path from 'node:path';
import type { PreludeRecord } from '../vite-plugins';
export type UntransformedFiles = { sourcePath: string; buildPath: string }[];
type TestParams = {
outDir: string;
untransformedFiles: UntransformedFiles;
trustedPreludes: PreludeRecord;
};
/**
* Runs all build tests.
*
* @param params - The parameters for the build tests.
* @param params.outDir - The output directory.
* @param params.untransformedFiles - The untransformed files to check.
* @param params.trustedPreludes - The trusted preludes to check.
*/
export async function runTests({
outDir,
untransformedFiles,
trustedPreludes,
}: TestParams): Promise<void> {
try {
await checkUntransformed(untransformedFiles);
await checkTrustedPreludes(outDir, trustedPreludes);
console.log('✅ Build tests passed successfully!');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
console.error(`❌ ${error.message}`);
throw error;
}
}
/**
* Test that shims and preludes are packaged untransformed.
*
* @param untransformedFiles - The untransformed files to check.
*/
async function checkUntransformed(
untransformedFiles: UntransformedFiles,
): Promise<void> {
for (const { sourcePath, buildPath } of untransformedFiles) {
const [originalContent, builtContent] = await Promise.all([
fs.readFile(sourcePath, 'utf8'),
fs.readFile(buildPath, 'utf8'),
]);
if (originalContent.trim() !== builtContent.trim()) {
throw new Error(
`"${buildPath}" is transformed or differs from the original source.`,
);
}
}
}
/**
* Test that trusted preludes are loaded at the top of the file.
*
* @param outDir - The output directory.
* @param trustedPreludes - The trusted preludes to check.
*/
async function checkTrustedPreludes(
outDir: string,
trustedPreludes: PreludeRecord,
): Promise<void> {
for (const [outputFileName, prelude] of Object.entries(trustedPreludes)) {
const outputFilePath = path.join(outDir, `${outputFileName}.js`);
const outputFileContent = await fs.readFile(outputFilePath, 'utf8');
const expectedImportStatement =
'path' in prelude
? `import "./${path.basename(prelude.path)}";`
: prelude.content;
if (!outputFileContent.startsWith(expectedImportStatement)) {
throw new Error(
`The trusted prelude \`${expectedImportStatement}\` is not imported in the first position in "${outputFileName}.js"`,
);
}
}
}
|