1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { checkForUpdates } from './updateCheck.js';
const getPackageJson = vi.hoisted(() => vi.fn());
vi.mock('../../utils/package.js', () => ({
getPackageJson,
}));
const updateNotifier = vi.hoisted(() => vi.fn());
vi.mock('update-notifier', () => ({
default: updateNotifier,
}));
describe('checkForUpdates', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('should return null if package.json is missing', async () => {
getPackageJson.mockResolvedValue(null);
const result = await checkForUpdates();
expect(result).toBeNull();
});
it('should return null if there is no update', async () => {
getPackageJson.mockResolvedValue({
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({ update: null });
const result = await checkForUpdates();
expect(result).toBeNull();
});
it('should return a message if a newer version is available', async () => {
getPackageJson.mockResolvedValue({
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
update: { current: '1.0.0', latest: '1.1.0' },
});
const result = await checkForUpdates();
expect(result).toContain('1.0.0 → 1.1.0');
});
it('should return null if the latest version is the same as the current version', async () => {
getPackageJson.mockResolvedValue({
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
update: { current: '1.0.0', latest: '1.0.0' },
});
const result = await checkForUpdates();
expect(result).toBeNull();
});
it('should return null if the latest version is older than the current version', async () => {
getPackageJson.mockResolvedValue({
name: 'test-package',
version: '1.1.0',
});
updateNotifier.mockReturnValue({
update: { current: '1.1.0', latest: '1.0.0' },
});
const result = await checkForUpdates();
expect(result).toBeNull();
});
it('should handle errors gracefully', async () => {
getPackageJson.mockRejectedValue(new Error('test error'));
const result = await checkForUpdates();
expect(result).toBeNull();
});
});
|