From f37bde8938783558a503dc1facbe8664b200eea8 Mon Sep 17 00:00:00 2001 From: "Micael Levi L. Cavalcante" Date: Sun, 23 Apr 2023 18:41:08 -0400 Subject: [PATCH] test(lib): add unit tests for `PackageManagerFactory.prototype.find` --- .../package-manager.factory.spec.ts | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 test/lib/package-managers/package-manager.factory.spec.ts diff --git a/test/lib/package-managers/package-manager.factory.spec.ts b/test/lib/package-managers/package-manager.factory.spec.ts new file mode 100644 index 000000000..01748ddb3 --- /dev/null +++ b/test/lib/package-managers/package-manager.factory.spec.ts @@ -0,0 +1,64 @@ +import * as fs from 'fs'; +import { + NpmPackageManager, + PackageManagerFactory, + PnpmPackageManager, + YarnPackageManager, +} from '../../../lib/package-managers'; + +jest.mock('fs', () => ({ + promises: { + readdir: jest.fn(), + }, +})); + +describe('PackageManagerFactory', () => { + afterAll(() => { + jest.clearAllMocks(); + }); + + describe('.prototype.find()', () => { + it('should return NpmPackageManager when no lock file is found', async () => { + (fs.promises.readdir as jest.Mock).mockResolvedValue([]); + + const whenPackageManager = PackageManagerFactory.find(); + await expect(whenPackageManager).resolves.toBeInstanceOf( + NpmPackageManager, + ); + }); + + it('should return YarnPackageManager when "yarn.lock" file is found', async () => { + (fs.promises.readdir as jest.Mock).mockResolvedValue(['yarn.lock']); + + const whenPackageManager = PackageManagerFactory.find(); + await expect(whenPackageManager).resolves.toBeInstanceOf( + YarnPackageManager, + ); + }); + + it('should return PnpmPackageManager when "pnpm-lock.yaml" file is found', async () => { + (fs.promises.readdir as jest.Mock).mockResolvedValue(['pnpm-lock.yaml']); + + const whenPackageManager = PackageManagerFactory.find(); + await expect(whenPackageManager).resolves.toBeInstanceOf( + PnpmPackageManager, + ); + }); + + describe('when there are all supported lock files', () => { + it('should prioritize "yarn.lock" file over all the others lock files', async () => { + (fs.promises.readdir as jest.Mock).mockResolvedValue([ + 'pnpm-lock.yaml', + 'package-lock.json', + // This is intentionally the last element in this array + 'yarn.lock', + ]); + + const whenPackageManager = PackageManagerFactory.find(); + await expect(whenPackageManager).resolves.toBeInstanceOf( + YarnPackageManager, + ); + }); + }); + }); +});