depguard
Version:
A powerful CLI tool to check and update npm/yarn dependencies in your projects
61 lines (60 loc) • 2.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const yarn_1 = require("../../package-managers/yarn");
jest.mock('child_process', () => ({
execSync: jest.fn()
}));
describe('YarnPackageManager', () => {
let manager;
beforeEach(() => {
manager = new yarn_1.YarnPackageManager();
jest.clearAllMocks();
});
describe('getLatestVersion', () => {
it('should return latest version when successful', async () => {
child_process_1.execSync.mockReturnValue('2.0.0\n');
const result = await manager.getLatestVersion('test-package');
expect(result).toBe('2.0.0');
expect(child_process_1.execSync).toHaveBeenCalledWith('yarn info test-package version', { encoding: 'utf-8' });
});
it('should return null when version fetch fails', async () => {
child_process_1.execSync.mockImplementation(() => {
throw new Error('Failed to fetch version');
});
const result = await manager.getLatestVersion('test-package');
expect(result).toBeNull();
// Reset mock after error test
child_process_1.execSync.mockReset();
});
});
describe('updatePackage', () => {
it('should execute yarn add command', async () => {
child_process_1.execSync.mockReturnValue(undefined);
await manager.updatePackage('test-package', '2.0.0');
expect(child_process_1.execSync).toHaveBeenCalledWith('yarn add test-package@2.0.0', { stdio: 'inherit' });
});
});
describe('updateAllPackages', () => {
it('should execute yarn add command with multiple packages', async () => {
child_process_1.execSync.mockReturnValue(undefined);
await manager.updateAllPackages([
{ name: 'package1', version: '1.0.0' },
{ name: 'package2', version: '2.0.0' }
]);
expect(child_process_1.execSync).toHaveBeenCalledWith('yarn add package1@1.0.0 package2@2.0.0', { stdio: 'inherit' });
});
});
describe('install', () => {
it('should execute yarn install command', async () => {
child_process_1.execSync.mockReturnValue(undefined);
await manager.install();
expect(child_process_1.execSync).toHaveBeenCalledWith('yarn install', { stdio: 'inherit' });
});
});
describe('getLockFile', () => {
it('should return yarn.lock', () => {
expect(manager.getLockFile()).toBe('yarn.lock');
});
});
});