es-toolkit/tests/check-dist.spec.ts

101 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-07-24 18:15:16 +03:00
import { execa } from 'execa';
import fs from 'node:fs';
import path from 'node:path';
import { Readable } from 'node:stream';
import { describe, expect, it } from 'vitest';
import { createPackageTarball } from './utils/createPackageTarball';
import { createTmpDir } from './utils/createTmpDir';
import { parseTar } from './utils/parseTar';
import { streamToBuffer } from './utils/streamToBuffer';
async function getPackageJsonOfTarball() {
const tarball = await createPackageTarball();
for await (const entry of parseTar(await fs.promises.readFile(tarball.path))) {
if (entry.path === 'package/package.json') {
const json = (await streamToBuffer(entry as unknown as Readable)).toString('utf-8');
return JSON.parse(json);
}
}
throw new Error(`package.json not found.`);
}
const ENTRYPOINTS = [
'.',
'./array',
'./function',
'./math',
'./object',
'./predicate',
'./promise',
'./string',
'./compat',
];
2024-07-24 18:15:16 +03:00
describe(`es-toolkit's package tarball`, () => {
it(
'configures all entrypoints correctly',
async () => {
const packageJson = await getPackageJsonOfTarball();
const entrypoints = Object.keys(packageJson.exports);
expect(entrypoints).toEqual([...ENTRYPOINTS, './package.json']);
},
{ timeout: 60_000 }
);
it(
'exports identical functions in CJS and ESM',
async () => {
const tarball = await createPackageTarball();
const tmpdir = await createTmpDir();
const packageJson = {
dependencies: {
'es-toolkit': tarball.path,
},
};
await fs.promises.writeFile(path.join(tmpdir, 'package.json'), JSON.stringify(packageJson, null, 2));
await execa('npm', ['install'], { cwd: tmpdir });
for (const entrypoint of ENTRYPOINTS) {
const cjsScript = `
const toolkit = require("${path.join('es-toolkit', entrypoint)}");
const exported = Object.entries(toolkit)
.map(([k, v]) => [k, typeof v])
.sort((x, y) => x[0].localeCompare(y[0]));
console.log("${path.join('es-toolkit', entrypoint)}");
2024-07-24 18:15:16 +03:00
console.log(exported);
`.trim();
const cjsScriptPath = path.join(tmpdir, 'script.cjs');
2024-07-24 18:15:16 +03:00
const esmScript = `
const toolkit = await import("${path.join('es-toolkit', entrypoint)}");
2024-07-24 18:15:16 +03:00
const exported = Object.entries(toolkit)
.map(([k, v]) => [k, typeof v])
.sort((x, y) => x[0].localeCompare(y[0]));
2024-07-24 18:15:16 +03:00
console.log("${path.join('es-toolkit', entrypoint)}");
2024-07-24 18:15:16 +03:00
console.log(exported);
`.trim();
const esmScriptPath = path.join(tmpdir, 'script.mjs');
2024-07-24 18:15:16 +03:00
await fs.promises.writeFile(cjsScriptPath, cjsScript);
await fs.promises.writeFile(esmScriptPath, esmScript);
2024-07-24 18:15:16 +03:00
const cjsResult = await execa('node', [cjsScriptPath]);
const esmResult = await execa('node', [esmScriptPath]);
2024-07-24 18:15:16 +03:00
expect(cjsResult.stdout).toEqual(esmResult.stdout);
}
},
{ timeout: 60_000 }
);
});