-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathpostrun.test.ts
More file actions
96 lines (80 loc) · 2.95 KB
/
postrun.test.ts
File metadata and controls
96 lines (80 loc) · 2.95 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {autoUpgradeIfNeeded} from './postrun.js'
import {mockAndCaptureOutput} from '../testing/output.js'
import {getOutputUpdateCLIReminder, runCLIUpgrade, versionToAutoUpgrade} from '../upgrade.js'
import {isMajorVersionChange} from '../version.js'
import {describe, expect, test, vi, afterEach} from 'vitest'
vi.mock('../upgrade.js', async (importOriginal) => {
const actual: any = await importOriginal()
return {
...actual,
runCLIUpgrade: vi.fn(),
getOutputUpdateCLIReminder: vi.fn(),
versionToAutoUpgrade: vi.fn(),
}
})
vi.mock('../version.js', async (importOriginal) => {
const actual: any = await importOriginal()
return {
...actual,
isMajorVersionChange: vi.fn(),
}
})
// Always execute the task so the rate limit doesn't interfere with tests
vi.mock('../../../private/node/conf-store.js', async (importOriginal) => {
const actual: any = await importOriginal()
return {
...actual,
runAtMinimumInterval: vi.fn(async (_key: string, _interval: object, task: () => Promise<void>) => {
await task()
return true
}),
}
})
afterEach(() => {
mockAndCaptureOutput().clear()
})
describe('autoUpgradeIfNeeded', () => {
test('runs the upgrade when versionToAutoUpgrade returns a version', async () => {
// Given
vi.mocked(versionToAutoUpgrade).mockReturnValue('3.91.0')
vi.mocked(runCLIUpgrade).mockResolvedValue()
// When
await autoUpgradeIfNeeded()
// Then
expect(runCLIUpgrade).toHaveBeenCalled()
})
test('falls back to warning when the upgrade fails', async () => {
// Given
const outputMock = mockAndCaptureOutput()
vi.mocked(versionToAutoUpgrade).mockReturnValue('3.91.0')
vi.mocked(runCLIUpgrade).mockRejectedValue(new Error('upgrade failed'))
const installReminder = '💡 Version 3.91.0 available! Run `npm install @shopify/cli@latest`'
vi.mocked(getOutputUpdateCLIReminder).mockReturnValue(installReminder)
// When
await autoUpgradeIfNeeded()
// Then
expect(outputMock.warn()).toMatch(installReminder)
})
test('does nothing when versionToAutoUpgrade returns undefined', async () => {
// Given
vi.mocked(versionToAutoUpgrade).mockReturnValue(undefined)
// When
await autoUpgradeIfNeeded()
// Then
expect(runCLIUpgrade).not.toHaveBeenCalled()
})
test('shows warning instead of upgrading for a major version change', async () => {
// Given
const outputMock = mockAndCaptureOutput()
vi.mocked(versionToAutoUpgrade).mockReturnValue('4.0.0')
vi.mocked(isMajorVersionChange).mockReturnValue(true)
const installReminder = '💡 Version 4.0.0 available! Run `npm install @shopify/cli@latest`'
vi.mocked(getOutputUpdateCLIReminder).mockReturnValue(installReminder)
// When
await autoUpgradeIfNeeded()
// Then
expect(runCLIUpgrade).not.toHaveBeenCalled()
expect(getOutputUpdateCLIReminder).toHaveBeenCalledWith('4.0.0', true)
expect(outputMock.warn()).toMatch(installReminder)
})
})