Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 2857879

Browse files
committed
test: stream download error cases
1 parent e44d6d1 commit 2857879

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

test/storageFileApiErrorHandling.test.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ describe('File API Error Handling', () => {
2525
global.fetch = jest.fn().mockImplementation(() => Promise.reject(mockError))
2626
const storage = new StorageClient(URL, { apikey: KEY })
2727

28-
const { data, error } = await storage.from(BUCKET_ID).download('test.jpg')
29-
expect(data).toBeNull()
30-
expect(error).not.toBeNull()
31-
expect(error?.message).toBe('Network failure')
28+
const blobDownload = await storage.from(BUCKET_ID).download('test.jpg')
29+
expect(blobDownload.data).toBeNull()
30+
expect(blobDownload.error).not.toBeNull()
31+
expect(blobDownload.error?.message).toBe('Network failure')
32+
33+
const streamDownload = await storage.from(BUCKET_ID).download('test.jpg').asStream()
34+
expect(streamDownload.data).toBeNull()
35+
expect(streamDownload.error).not.toBeNull()
36+
expect(streamDownload.error?.message).toBe('Network failure')
3237
})
3338

3439
it('wraps non-Response errors as StorageUnknownError', async () => {
@@ -37,18 +42,23 @@ describe('File API Error Handling', () => {
3742

3843
const storage = new StorageClient(URL, { apikey: KEY })
3944

40-
const { data, error } = await storage.from(BUCKET_ID).download('test.jpg')
41-
expect(data).toBeNull()
42-
expect(error).toBeInstanceOf(StorageUnknownError)
43-
expect(error?.message).toBe('Invalid download format')
45+
const blobDownload = await storage.from(BUCKET_ID).download('test.jpg')
46+
expect(blobDownload.data).toBeNull()
47+
expect(blobDownload.error).toBeInstanceOf(StorageUnknownError)
48+
expect(blobDownload.error?.message).toBe('Invalid download format')
49+
50+
const streamDownload = await storage.from(BUCKET_ID).download('test.jpg').asStream()
51+
expect(streamDownload.data).toBeNull()
52+
expect(streamDownload.error).toBeInstanceOf(StorageUnknownError)
53+
expect(streamDownload.error?.message).toBe('Invalid download format')
4454
})
4555

4656
it('throws non-StorageError exceptions', async () => {
4757
// Create a storage client
4858
const storage = new StorageClient(URL, { apikey: KEY })
4959

5060
// Create a spy on the fetch method that will throw a non-StorageError
51-
const mockFn = jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
61+
const mockFn = jest.spyOn(global, 'fetch').mockImplementation(() => {
5262
const error = new Error('Unexpected error in download')
5363
Object.defineProperty(error, 'name', { value: 'CustomError' })
5464
throw error
@@ -58,6 +68,10 @@ describe('File API Error Handling', () => {
5868
'Unexpected error in download'
5969
)
6070

71+
await expect(storage.from(BUCKET_ID).download('test.jpg').asStream()).rejects.toThrow(
72+
'Unexpected error in download'
73+
)
74+
6175
mockFn.mockRestore()
6276
})
6377
})

0 commit comments

Comments
 (0)