Skip to content

Commit

Permalink
test(lib): add unit tests for PackageManagerFactory.prototype.find
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Apr 23, 2023
1 parent ea9cdf5 commit f37bde8
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions 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,
);
});
});
});
});

0 comments on commit f37bde8

Please sign in to comment.