|
| 1 | +import {autoUpgradeIfNeeded} from './postrun.js' |
| 2 | +import {mockAndCaptureOutput} from '../testing/output.js' |
| 3 | +import {getOutputUpdateCLIReminder, runCLIUpgrade, versionToAutoUpgrade} from '../upgrade.js' |
| 4 | +import {isMajorVersionChange} from '../version.js' |
| 5 | +import {describe, expect, test, vi, afterEach} from 'vitest' |
| 6 | + |
| 7 | +vi.mock('../upgrade.js', async (importOriginal) => { |
| 8 | + const actual: any = await importOriginal() |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + runCLIUpgrade: vi.fn(), |
| 12 | + getOutputUpdateCLIReminder: vi.fn(), |
| 13 | + versionToAutoUpgrade: vi.fn(), |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +vi.mock('../version.js', async (importOriginal) => { |
| 18 | + const actual: any = await importOriginal() |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + isMajorVersionChange: vi.fn(), |
| 22 | + } |
| 23 | +}) |
| 24 | + |
| 25 | +afterEach(() => { |
| 26 | + mockAndCaptureOutput().clear() |
| 27 | +}) |
| 28 | + |
| 29 | +describe('autoUpgradeIfNeeded', () => { |
| 30 | + test('runs the upgrade when versionToAutoUpgrade returns a version', async () => { |
| 31 | + // Given |
| 32 | + vi.mocked(versionToAutoUpgrade).mockReturnValue('3.91.0') |
| 33 | + vi.mocked(runCLIUpgrade).mockResolvedValue() |
| 34 | + |
| 35 | + // When |
| 36 | + await autoUpgradeIfNeeded() |
| 37 | + |
| 38 | + // Then |
| 39 | + expect(runCLIUpgrade).toHaveBeenCalled() |
| 40 | + }) |
| 41 | + |
| 42 | + test('falls back to warning when the upgrade fails', async () => { |
| 43 | + // Given |
| 44 | + const outputMock = mockAndCaptureOutput() |
| 45 | + vi.mocked(versionToAutoUpgrade).mockReturnValue('3.91.0') |
| 46 | + vi.mocked(runCLIUpgrade).mockRejectedValue(new Error('upgrade failed')) |
| 47 | + const installReminder = '💡 Version 3.91.0 available! Run `npm install @shopify/cli@latest`' |
| 48 | + vi.mocked(getOutputUpdateCLIReminder).mockReturnValue(installReminder) |
| 49 | + |
| 50 | + // When |
| 51 | + await autoUpgradeIfNeeded() |
| 52 | + |
| 53 | + // Then |
| 54 | + expect(outputMock.warn()).toMatch(installReminder) |
| 55 | + }) |
| 56 | + |
| 57 | + test('does nothing when versionToAutoUpgrade returns undefined', async () => { |
| 58 | + // Given |
| 59 | + vi.mocked(versionToAutoUpgrade).mockReturnValue(undefined) |
| 60 | + |
| 61 | + // When |
| 62 | + await autoUpgradeIfNeeded() |
| 63 | + |
| 64 | + // Then |
| 65 | + expect(runCLIUpgrade).not.toHaveBeenCalled() |
| 66 | + }) |
| 67 | + |
| 68 | + test('shows warning instead of upgrading for a major version change', async () => { |
| 69 | + // Given |
| 70 | + const outputMock = mockAndCaptureOutput() |
| 71 | + vi.mocked(versionToAutoUpgrade).mockReturnValue('4.0.0') |
| 72 | + vi.mocked(isMajorVersionChange).mockReturnValue(true) |
| 73 | + const installReminder = '💡 Version 4.0.0 available! Run `npm install @shopify/cli@latest`' |
| 74 | + vi.mocked(getOutputUpdateCLIReminder).mockReturnValue(installReminder) |
| 75 | + |
| 76 | + // When |
| 77 | + await autoUpgradeIfNeeded() |
| 78 | + |
| 79 | + // Then |
| 80 | + expect(runCLIUpgrade).not.toHaveBeenCalled() |
| 81 | + expect(outputMock.warn()).toMatch(installReminder) |
| 82 | + }) |
| 83 | +}) |
0 commit comments