Skip to content

Commit

Permalink
test: adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Apr 17, 2024
1 parent c782b6a commit 00eef04
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
Expand Up @@ -27,20 +27,14 @@ afterAll(() => {
server.close()
})

test('prevents a request when a custom callback throws an exception', async () => {
const makeRequest = () => {
return fetch('https://example.com')
.then(() => {
throw new Error('Must not resolve')
})
.catch<Error>((error) => error)
}
test('handles exceptions in "onUnhandledRequest" callback as 500 responses', async () => {
const response = await fetch('https://example.com')

const requestError = await makeRequest()

// Request should be cancelled with a fetch error, since the callback threw.
expect(requestError.message).toBe('Failed to fetch')
expect(requestError.cause).toEqual(
new Error('Custom error for GET https://example.com/'),
)
expect(response.status).toBe(500)
expect(response.statusText).toBe('Unhandled Exception')
expect(await response.json()).toEqual({
name: 'Error',
message: 'Custom error for GET https://example.com/',
stack: expect.any(String),
})
})
19 changes: 10 additions & 9 deletions test/node/rest-api/response/throw-response.node.test.ts
Expand Up @@ -95,19 +95,20 @@ it('supports middleware-style responses', async () => {
expect(await errorResponse.text()).toBe('must have id')
})

it('throws non-Response errors as-is', async () => {
it('handles non-response errors as 500 error responses', async () => {
server.use(
http.get('https://example.com/', () => {
throw new Error('Oops!')
throw new Error('Custom error')
}),
)

const requestError = await fetch('https://example.com/')
.then(() => null)
.catch((error) => error)
const response = await fetch('https://example.com')

expect(requestError.name).toBe('TypeError')
expect(requestError.message).toBe('Failed to fetch')
// Undici forwards the original error in the "cause" property.
expect(requestError.cause).toEqual(new Error('Oops!'))
expect(response.status).toBe(500)
expect(response.statusText).toBe('Unhandled Exception')
expect(await response.json()).toEqual({
name: 'Error',
message: 'Custom error',
stack: expect.any(String),
})
})

0 comments on commit 00eef04

Please sign in to comment.