https://github.com/remcohaszing/jest-axios-snapshot
Test axios HTTP responses using Jest snapshots
https://github.com/remcohaszing/jest-axios-snapshot
axios jest testing vitest
Last synced: about 1 year ago
JSON representation
Test axios HTTP responses using Jest snapshots
- Host: GitHub
- URL: https://github.com/remcohaszing/jest-axios-snapshot
- Owner: remcohaszing
- License: mit
- Created: 2021-10-30T12:27:34.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-10T15:48:56.000Z (over 3 years ago)
- Last Synced: 2025-01-08T18:52:17.281Z (over 1 year ago)
- Topics: axios, jest, testing, vitest
- Language: TypeScript
- Homepage:
- Size: 289 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Jest Axios Snapshot
> Test Axios responses using Jest snapshots
[](https://github.com/remcohaszing/jest-axios-snapshot/actions)
[](https://codecov.io/gh/remcohaszing/jest-axios-snapshot)
[](https://www.npmjs.com/package/jest-axios-snapshot)
[](https://prettier.io)
[](https://jestjs.io)
## Installation
```sh
npm install jest-axios-snapshot
```
## Usage
Add the following to your Jest configuration:
```json
{
"snapshotSerializers": ["jest-axios-snapshot"]
}
```
Now use an Axios response to match a Jest snapshot.
```js
import axios from 'axios';
it('should download example.com', async () => {
const response = await axios.get('https://example.com');
expect(response).toMatchSnapshot();
});
```
This was written with [axios-test-instance](https://github.com/remcohaszing/axios-test-instance) and
inline snapshots in mind.
```ts
import { request, setTestApp } from 'axios-test-instance';
import { app } from './app';
beforeAll(async () => {
await setTestApp(app);
});
it('should get data', async () => {
const response = await request.get('/');
expect(response).tpMatchInlineSnapshot(`
HTTP/1.1 200 OK
Connection: close
Content-Length: 17
Content-Type: application/json; charset=utf-8
{
"hello": "world"
}
`);
});
```
By default the `Date` header will be stripped. To set a custom response transformer, use
`setResponseTransformer`
```js
import { setResponseTransformer } from 'jest-axios-snapshot';
setResponseTransformer((response) => {
// Modify response
const { etag, ...headers } = response.headers;
return {
...response,
headers,
};
});
```
## See also
- [axios-test-instance](https://github.com/remcohaszing/axios-test-instance) enables you to test a
Node.js server using axios.
## License
[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)