Skip to content

Commit 4e33f01

Browse files
committed
test(updaters): add unit tests for CustomUpdater
- Added tests for canHandle method - Added tests for getCurrentVersion method - Added tests for bumpVersion method - Mocked dependencies using vitest - Improved test coverage
1 parent 5ec175e commit 4e33f01

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { CustomUpdater } from '../../src/updaters/customUpdater';
3+
import { FileHandler, ManifestParser } from '../../src/utils';
4+
import * as core from '@actions/core';
5+
6+
vi.mock('../../src/utils/fileHandler');
7+
vi.mock('../../src/utils/manifestParser');
8+
vi.mock('@actions/core');
9+
10+
describe('CustomUpdater', () => {
11+
let customUpdater: CustomUpdater;
12+
let mockFileHandler: vi.Mocked<FileHandler>;
13+
let mockManifestParser: vi.Mocked<ManifestParser>;
14+
15+
const filePath = 'test.txt';
16+
const variableName = 'APP_VERSION';
17+
18+
beforeEach(() => {
19+
mockFileHandler = new FileHandler() as vi.Mocked<FileHandler>;
20+
mockManifestParser = new ManifestParser(mockFileHandler) as vi.Mocked<ManifestParser>;
21+
customUpdater = new CustomUpdater(filePath, variableName);
22+
23+
// Mock the constructor's internal assignments
24+
(customUpdater as any).fileHandler = mockFileHandler;
25+
(customUpdater as any).manifestParser = mockManifestParser;
26+
});
27+
28+
it('should always return true for canHandle', () => {
29+
expect(customUpdater.canHandle()).toBe(true);
30+
});
31+
32+
describe('getCurrentVersion', () => {
33+
it('should return the current version if found', () => {
34+
mockManifestParser.getVersion.mockReturnValue('1.0.0');
35+
expect(customUpdater.getCurrentVersion()).toBe('1.0.0');
36+
expect(mockManifestParser.getVersion).toHaveBeenCalledWith(
37+
filePath,
38+
'regex',
39+
expect.objectContaining({ regex: expect.any(RegExp) }),
40+
);
41+
});
42+
43+
it('should return null if version is not found', () => {
44+
mockManifestParser.getVersion.mockReturnValue(null);
45+
expect(customUpdater.getCurrentVersion()).toBeNull();
46+
});
47+
48+
it('should return null and log debug message if manifestParser.getVersion throws an error', () => {
49+
const error = new Error('Parse error');
50+
mockManifestParser.getVersion.mockImplementation(() => {
51+
throw error;
52+
});
53+
const coreDebugSpy = vi.spyOn(core, 'debug').mockImplementation(() => {});
54+
55+
expect(customUpdater.getCurrentVersion()).toBeNull();
56+
expect(coreDebugSpy).toHaveBeenCalledWith(
57+
`Could not read or parse version from ${filePath}: ${error}`,
58+
);
59+
});
60+
});
61+
62+
describe('bumpVersion', () => {
63+
it('should successfully bump the version', () => {
64+
vi.spyOn(customUpdater, 'getCurrentVersion').mockReturnValue('1.0.0');
65+
mockManifestParser.updateVersion.mockReturnValue(undefined);
66+
const coreInfoSpy = vi.spyOn(core, 'info').mockImplementation(() => {});
67+
68+
const newVersion = customUpdater.bumpVersion('patch');
69+
70+
expect(newVersion).toBe('1.0.1');
71+
expect(mockManifestParser.updateVersion).toHaveBeenCalledWith(
72+
filePath,
73+
'1.0.1',
74+
'regex',
75+
expect.objectContaining({ regexReplace: expect.any(RegExp) }),
76+
);
77+
expect(coreInfoSpy).toHaveBeenCalledWith(
78+
`Bumped ${variableName} in ${filePath} from 1.0.0 to 1.0.1`,
79+
);
80+
});
81+
82+
it('should throw an error if current version is not found', () => {
83+
vi.spyOn(customUpdater, 'getCurrentVersion').mockReturnValue(null);
84+
85+
expect(() => customUpdater.bumpVersion('patch')).toThrow(
86+
`Could not find current version for variable '${variableName}' in file '${filePath}'`,
87+
);
88+
});
89+
});
90+
});

0 commit comments

Comments
 (0)