Skip to content

Commit

Permalink
test: add axios error response test (#1891)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Nov 26, 2023
1 parent 2ab2f23 commit 8100e6d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/node/third-party/axios-error-response.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @vitest-environment node
import axios, { AxiosError } from 'axios'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'

const server = setupServer(
http.get('https://example.com/resource', () => {
return HttpResponse.json({ errorMessage: 'Custom error' }, { status: 400 })
}),
)

beforeAll(() => {
server.listen()
})

afterAll(() => {
server.close()
})

it('responds with an error response to axios request', async () => {
const error = await axios('https://example.com/resource')
.then(() => {
throw new Error('Must reject the request Promise')
})
.catch((error) => error as AxiosError)

expect(error.response?.status).toBe(400)
expect(error.response?.data).toEqual({ errorMessage: 'Custom error' })
})

0 comments on commit 8100e6d

Please sign in to comment.