Files
vercel/internals/get-package-json/tests/unit/cache.test.ts
Ethan Arrowood 07a09b7880 [cli] refactor pkg.ts into @vercel-internals/get-package-json (#9719)
Extracts the logic from `pkg.ts` into a new utility function
`getPackageJSON` from `@vercel-internals/utils`.

---------

Co-authored-by: Nathan Rajlich <n@n8.io>
2023-04-06 12:18:04 -06:00

21 lines
643 B
TypeScript

import { getPackageJSON } from '../../src/index';
import fs from 'fs';
import path from 'path';
test('getPackageJSON caches read operations', () => {
const expected = JSON.parse(
fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf-8')
);
expect(expected.name).toBe('@vercel-internals/get-package-json');
const readFileSyncSpy = jest.spyOn(fs, 'readFileSync');
const actual = getPackageJSON();
expect(actual).toStrictEqual(expected);
expect(readFileSyncSpy).toBeCalledTimes(1);
const cacheHit = getPackageJSON();
expect(cacheHit).toStrictEqual(expected);
expect(readFileSyncSpy).toBeCalledTimes(1);
});