fix(update): replace update-notifier with latest-version (#11989)

This commit is contained in:
Gal Zahavi
2025-10-24 14:23:39 -07:00
committed by GitHub
parent 7e2642b9f1
commit 810d940e57
7 changed files with 114 additions and 403 deletions
+19 -50
View File
@@ -13,9 +13,9 @@ vi.mock('../../utils/package.js', () => ({
getPackageJson,
}));
const updateNotifier = vi.hoisted(() => vi.fn());
vi.mock('update-notifier', () => ({
default: updateNotifier,
const latestVersion = vi.hoisted(() => vi.fn());
vi.mock('latest-version', () => ({
default: latestVersion,
}));
describe('checkForUpdates', () => {
@@ -46,7 +46,7 @@ describe('checkForUpdates', () => {
const result = await checkForUpdates(mockSettings);
expect(result).toBeNull();
expect(getPackageJson).not.toHaveBeenCalled();
expect(updateNotifier).not.toHaveBeenCalled();
expect(latestVersion).not.toHaveBeenCalled();
});
it('should return null when running from source (DEV=true)', async () => {
@@ -55,15 +55,11 @@ describe('checkForUpdates', () => {
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.0.0', latest: '1.1.0' }),
});
latestVersion.mockResolvedValue('1.1.0');
const result = await checkForUpdates(mockSettings);
expect(result).toBeNull();
expect(getPackageJson).not.toHaveBeenCalled();
expect(updateNotifier).not.toHaveBeenCalled();
expect(latestVersion).not.toHaveBeenCalled();
});
it('should return null if package.json is missing', async () => {
@@ -77,9 +73,7 @@ describe('checkForUpdates', () => {
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
fetchInfo: vi.fn().mockResolvedValue(null),
});
latestVersion.mockResolvedValue('1.0.0');
const result = await checkForUpdates(mockSettings);
expect(result).toBeNull();
});
@@ -89,15 +83,13 @@ describe('checkForUpdates', () => {
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.0.0', latest: '1.1.0' }),
});
latestVersion.mockResolvedValue('1.1.0');
const result = await checkForUpdates(mockSettings);
expect(result?.message).toContain('1.0.0 → 1.1.0');
expect(result?.update).toEqual({ current: '1.0.0', latest: '1.1.0' });
expect(result?.update.current).toEqual('1.0.0');
expect(result?.update.latest).toEqual('1.1.0');
expect(result?.update.name).toEqual('test-package');
});
it('should return null if the latest version is the same as the current version', async () => {
@@ -105,11 +97,7 @@ describe('checkForUpdates', () => {
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.0.0', latest: '1.0.0' }),
});
latestVersion.mockResolvedValue('1.0.0');
const result = await checkForUpdates(mockSettings);
expect(result).toBeNull();
});
@@ -119,23 +107,17 @@ describe('checkForUpdates', () => {
name: 'test-package',
version: '1.1.0',
});
updateNotifier.mockReturnValue({
fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.1.0', latest: '1.0.0' }),
});
latestVersion.mockResolvedValue('1.0.0');
const result = await checkForUpdates(mockSettings);
expect(result).toBeNull();
});
it('should return null if fetchInfo rejects', async () => {
it('should return null if latestVersion rejects', async () => {
getPackageJson.mockResolvedValue({
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
fetchInfo: vi.fn().mockRejectedValue(new Error('Timeout')),
});
latestVersion.mockRejectedValue(new Error('Timeout'));
const result = await checkForUpdates(mockSettings);
expect(result).toBeNull();
@@ -154,26 +136,13 @@ describe('checkForUpdates', () => {
version: '1.2.3-nightly.1',
});
const fetchInfoMock = vi.fn().mockImplementation(({ distTag }) => {
if (distTag === 'nightly') {
return Promise.resolve({
latest: '1.2.3-nightly.2',
current: '1.2.3-nightly.1',
});
latestVersion.mockImplementation(async (name, options) => {
if (options?.version === 'nightly') {
return '1.2.3-nightly.2';
}
if (distTag === 'latest') {
return Promise.resolve({
latest: '1.2.3',
current: '1.2.3-nightly.1',
});
}
return Promise.resolve(null);
return '1.2.3';
});
updateNotifier.mockImplementation(({ pkg, distTag }) => ({
fetchInfo: () => fetchInfoMock({ pkg, distTag }),
}));
const result = await checkForUpdates(mockSettings);
expect(result?.message).toContain('1.2.3-nightly.1 → 1.2.3-nightly.2');
expect(result?.update.latest).toBe('1.2.3-nightly.2');