-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathupload-files.test.ts
More file actions
85 lines (69 loc) · 2.46 KB
/
upload-files.test.ts
File metadata and controls
85 lines (69 loc) · 2.46 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
import { v4 as generateUUID } from 'uuid'
import { afterAll, expect, test, vi } from 'vitest'
import type { NetlifyAPI } from '@netlify/api'
import uploadFiles, { type UploadFileObj } from '../../../../src/utils/deploy/upload-files.js'
vi.mock('../../../../src/utils/deploy/constants.js', async () => {
const actual = await vi.importActual('../../../../src/utils/deploy/constants.js')
// Reduce the delay, so these tests do not wait for 10 seconds
return { ...actual, UPLOAD_INITIAL_DELAY: 100, UPLOAD_MAX_DELAY: 200 }
})
afterAll(() => {
vi.restoreAllMocks()
})
test('Adds a retry count to function upload requests', async () => {
const uploadDeployFunction = vi.fn()
const mockError = new Error('Uh-oh')
Object.assign(mockError, { status: 500 })
uploadDeployFunction.mockRejectedValueOnce(mockError)
uploadDeployFunction.mockRejectedValueOnce(mockError)
uploadDeployFunction.mockResolvedValueOnce(undefined)
const mockApi = {
uploadDeployFunction,
} as unknown as NetlifyAPI
const deployId = generateUUID()
const files: UploadFileObj[] = [
{
assetType: 'function',
filepath: '/some/path/func1.zip',
normalizedPath: 'func1.zip',
runtime: 'js',
},
]
const options = {
concurrentUpload: 1,
maxRetry: 3,
statusCb: vi.fn(),
}
await uploadFiles(mockApi, deployId, files, options)
expect(uploadDeployFunction).toHaveBeenCalledTimes(3)
expect(uploadDeployFunction).toHaveBeenNthCalledWith(1, expect.not.objectContaining({ xNfRetryCount: 1 }))
expect(uploadDeployFunction).toHaveBeenNthCalledWith(2, expect.objectContaining({ xNfRetryCount: 1 }))
expect(uploadDeployFunction).toHaveBeenNthCalledWith(3, expect.objectContaining({ xNfRetryCount: 2 }))
})
test('Does not retry on 400 response from function upload requests', async () => {
const uploadDeployFunction = vi.fn()
const mockError = new Error('Uh-oh')
Object.assign(mockError, { status: 400 })
uploadDeployFunction.mockRejectedValue(mockError)
const mockApi = {
uploadDeployFunction,
} as unknown as NetlifyAPI
const deployId = generateUUID()
const files: UploadFileObj[] = [
{
assetType: 'function',
filepath: '/some/path/func1.zip',
normalizedPath: 'func1.zip',
runtime: 'js',
},
]
const options = {
concurrentUpload: 1,
maxRetry: 3,
statusCb: vi.fn(),
}
try {
await uploadFiles(mockApi, deployId, files, options)
} catch {}
expect(uploadDeployFunction).toHaveBeenCalledTimes(1)
})