-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add axios error response test (#1891)
- Loading branch information
1 parent
2ab2f23
commit 8100e6d
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
}) |