|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +import { existsSync, readFileSync, writeFileSync } from 'fs'; |
| 4 | +import { inc } from 'semver'; |
| 5 | + |
| 6 | +// Mock the fs module |
| 7 | +vi.mock('fs', () => { |
| 8 | + const mockFs = { |
| 9 | + existsSync: vi.fn(), |
| 10 | + readFileSync: vi.fn(), |
| 11 | + writeFileSync: vi.fn(), |
| 12 | + }; |
| 13 | + return { |
| 14 | + __esModule: true, |
| 15 | + default: mockFs, |
| 16 | + ...mockFs, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +// Mock the semver module |
| 21 | +vi.mock('semver', () => { |
| 22 | + const mockInc = vi.fn(); |
| 23 | + return { |
| 24 | + __esModule: true, |
| 25 | + default: { |
| 26 | + inc: mockInc, |
| 27 | + }, |
| 28 | + inc: mockInc, |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +import { NodeUpdater } from '../../src/updaters/nodeUpdater'; |
| 33 | + |
| 34 | +describe('NodeUpdater', () => { |
| 35 | + let nodeUpdater: NodeUpdater; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + nodeUpdater = new NodeUpdater(); |
| 39 | + // Reset mocks before each test |
| 40 | + vi.clearAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('canHandle', () => { |
| 44 | + it('should return true if package.json exists', () => { |
| 45 | + (existsSync as vi.Mock).mockReturnValue(true); |
| 46 | + expect(nodeUpdater.canHandle()).toBe(true); |
| 47 | + expect(existsSync).toHaveBeenCalledWith('package.json'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return false if package.json does not exist', () => { |
| 51 | + (existsSync as vi.Mock).mockReturnValue(false); |
| 52 | + expect(nodeUpdater.canHandle()).toBe(false); |
| 53 | + expect(existsSync).toHaveBeenCalledWith('package.json'); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('getCurrentVersion', () => { |
| 58 | + it('should return the version from package.json if it exists', () => { |
| 59 | + (existsSync as vi.Mock).mockReturnValue(true); |
| 60 | + nodeUpdater.canHandle(); // Ensure manifestPath is set |
| 61 | + (readFileSync as vi.Mock).mockReturnValueOnce(JSON.stringify({ version: '1.0.0' })); |
| 62 | + expect(nodeUpdater.getCurrentVersion()).toBe('1.0.0'); |
| 63 | + expect(readFileSync).toHaveBeenCalledWith('package.json', 'utf8'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return null if package.json does not exist', () => { |
| 67 | + (existsSync as vi.Mock).mockReturnValue(false); |
| 68 | + expect(nodeUpdater.getCurrentVersion()).toBeNull(); |
| 69 | + expect(readFileSync).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('bumpVersion', () => { |
| 74 | + const mockPackageJson = { name: 'test-pkg', version: '1.0.0' }; |
| 75 | + const newVersion = '1.0.1'; |
| 76 | + |
| 77 | + beforeEach(() => { |
| 78 | + (readFileSync as vi.Mock).mockReturnValue(JSON.stringify(mockPackageJson)); |
| 79 | + (inc as vi.Mock).mockReturnValue(newVersion); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should increment the version and write to package.json', () => { |
| 83 | + (existsSync as vi.Mock).mockReturnValue(true); |
| 84 | + nodeUpdater.canHandle(); |
| 85 | + const result = nodeUpdater.bumpVersion('patch'); |
| 86 | + |
| 87 | + expect(inc).toHaveBeenCalledWith(mockPackageJson.version, 'patch'); |
| 88 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 89 | + 'package.json', |
| 90 | + JSON.stringify({ ...mockPackageJson, version: newVersion }, null, 2), |
| 91 | + ); |
| 92 | + expect(result).toBe(newVersion); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should handle major release type', () => { |
| 96 | + (existsSync as vi.Mock).mockReturnValue(true); |
| 97 | + nodeUpdater.canHandle(); |
| 98 | + const majorVersion = '2.0.0'; |
| 99 | + (inc as vi.Mock).mockReturnValue(majorVersion); |
| 100 | + const result = nodeUpdater.bumpVersion('major'); |
| 101 | + expect(inc).toHaveBeenCalledWith(mockPackageJson.version, 'major'); |
| 102 | + expect(result).toBe(majorVersion); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should handle minor release type', () => { |
| 106 | + (existsSync as vi.Mock).mockReturnValue(true); |
| 107 | + nodeUpdater.canHandle(); |
| 108 | + const minorVersion = '1.1.0'; |
| 109 | + (inc as vi.Mock).mockReturnValue(minorVersion); |
| 110 | + const result = nodeUpdater.bumpVersion('minor'); |
| 111 | + expect(inc).toHaveBeenCalledWith(mockPackageJson.version, 'minor'); |
| 112 | + expect(result).toBe(minorVersion); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments