UNPKG

depguard

Version:

A powerful CLI tool to check and update npm/yarn dependencies in your projects

61 lines (60 loc) 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const npm_1 = require("../../package-managers/npm"); jest.mock('child_process', () => ({ execSync: jest.fn() })); describe('NpmPackageManager', () => { let manager; beforeEach(() => { manager = new npm_1.NpmPackageManager(); 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('npm view 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 npm install command', async () => { child_process_1.execSync.mockReturnValue(undefined); await manager.updatePackage('test-package', '2.0.0'); expect(child_process_1.execSync).toHaveBeenCalledWith('npm install test-package@2.0.0', { stdio: 'inherit' }); }); }); describe('updateAllPackages', () => { it('should execute npm install 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('npm install package1@1.0.0 package2@2.0.0', { stdio: 'inherit' }); }); }); describe('install', () => { it('should execute npm install command', async () => { child_process_1.execSync.mockReturnValue(undefined); await manager.install(); expect(child_process_1.execSync).toHaveBeenCalledWith('npm install', { stdio: 'inherit' }); }); }); describe('getLockFile', () => { it('should return package-lock.json', () => { expect(manager.getLockFile()).toBe('package-lock.json'); }); }); });