Files
vercel/packages/build-utils/test/unit.read-config-file.test.ts
Chris Barber 222710f612 Add type-check and unify tsconfig (#10667)
This adds a new `pnpm type-check` that leverages `turbo` to validate the TypeScript code. This can be run at the top-level or for an individual package.

The `test-lint` workflow will run it after linting and doing the prettier check.

As apart of this effort, each package's `tsconfig.json` has been simplified. There's a new top-level `tsconfig.base.json` file that extends the Vercel Style Guide for TypeScript. Each package's `tsconfig.json` has been audited and previously suppressed rules that no longer apply have been removed. The result is each package's `tsconfig.json` is greatly simplified and we can control common settings in the base config while keeping the flexibility of package-level overrides.

Lastly, in `package/cli`, `pnpm build` calls `scripts/build.mjs` which calls `scripts/compile-templates.mjs`. The `compile-templates.mjs` file was generating invalid TypeScript code. I've fixed it and now it's happier than ever.

Note: In order to run `pnpm type-check`, you must first `pnpm build` because we need the `.d.ts` definition files.
2023-10-09 11:58:23 +00:00

78 lines
2.1 KiB
TypeScript
Vendored

import { join } from 'path';
import { writeFile, rm } from 'fs/promises';
import { readConfigFile } from '../src';
describe('Test `readConfigFile()`', () => {
let logMessages: string[];
const originalConsoleLog = console.log;
beforeEach(() => {
logMessages = [];
console.log = m => {
logMessages.push(m);
};
});
afterEach(() => {
console.log = originalConsoleLog;
});
const doesnotexist = join(__dirname, 'does-not-exist.json');
const tsconfig = join(__dirname, '../tsconfig.json');
const invalid = join(__dirname, 'invalid.json');
it('should return null when file does not exist', async () => {
expect(await readConfigFile(doesnotexist)).toBeNull();
expect(logMessages).toEqual([]);
});
it('should return parsed object when file exists', async () => {
expect(await readConfigFile(tsconfig)).toMatchObject({
compilerOptions: {
outDir: './dist',
},
});
expect(logMessages).toEqual([]);
});
it('should return parsed object when at least one file exists', async () => {
const files = [doesnotexist, tsconfig];
expect(await readConfigFile(files)).toMatchObject({
compilerOptions: {
outDir: './dist',
},
});
expect(logMessages).toEqual([]);
});
it('should return null when parse fails', async () => {
try {
await writeFile(invalid, 'borked');
expect(await readConfigFile(invalid)).toBeNull();
} finally {
await rm(invalid);
}
expect(logMessages.length).toBe(1);
expect(logMessages[0]).toMatch(
/^Error while parsing config file.+invalid.json/
);
});
it('should return parsed object when at least one file is valid', async () => {
try {
await writeFile(invalid, 'borked');
expect(await readConfigFile([invalid, tsconfig])).toMatchObject({
compilerOptions: {
outDir: './dist',
},
});
} finally {
await rm(invalid);
}
expect(logMessages.length).toBe(1);
expect(logMessages[0]).toMatch(
/^Error while parsing config file.+invalid.json/
);
});
});