Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/acdcjunior/axios-stubber
Simplified axios request and response stubbing
https://github.com/acdcjunior/axios-stubber
Last synced: 26 days ago
JSON representation
Simplified axios request and response stubbing
- Host: GitHub
- URL: https://github.com/acdcjunior/axios-stubber
- Owner: acdcjunior
- License: mit
- Created: 2019-07-30T00:32:27.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-08T18:12:10.000Z (almost 2 years ago)
- Last Synced: 2024-10-01T07:32:59.848Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 356 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# axios-stubber
[![axios-stubber version](https://img.shields.io/npm/v/axios-stubber?color=green)](https://www.npmjs.com/package/axios-stubber)
Simplified axios request and response stubbing.
Stubs `axios` globally, making its calls return as specified in stubs files (or objects).
```sh
npm i -D axios-stubber
yarn add --dev axios-stubber
```# axiosStubber
```js
import axios from 'axios';
import axiosStubber from 'axios-stubber';test('axios-stubber example', async () => {
axiosStubber(axios, {
request: {
method: "GET",
url: "https://w00t.com"
},
response: {
status: 201,
body: {
yes: "It is"
}
}
}); // the argument could also be an array, a json file, or js filelet {data, status} = await axios.get("https://w00t.com");
expect(data).toStrictEqual({yes: "It is"});
expect(status).toStrictEqual(201);
});
```Test file:
```js
const go = require('./go');import axios from 'axios';
import axiosStubber from 'axios-stubber';describe('axiosStubber', () => {
let axiosMockControl;
afterEach(() => {
if (axiosMockControl) axiosMockControl.restore();
});it('get', async () => {
axiosMockControl = axiosStubber(axios, __dirname + '/stubs/exampleApi.stubs.json');let {data} = await axios.get("http://master.example.com/info?code=123");
expect(data).toStrictEqual({result: 123456});
});it('post', async () => {
axiosMockControl = axiosStubber(axios, __dirname + '/stubs/exampleApi.stubs.json');let r = await go();
expect(r).toStrictEqual({yay: true});
});it('get', async () => {
axiosMockControl = axiosStubber(axios, [__dirname + '/stubs/exampleApi.stubs.json']);let {data} = await axios.get("http://master.example.com/info?code=123");
expect(data).toStrictEqual({result: 123456});
});it('get', async () => {
axiosMockControl = axiosStubber(axios, [
{
request: {
method: "GET",
url: "https://w00t.com"
},
response: {
status: 201,
body: {
yes: "It is"
}
}
}
]);let {data, status} = await axios.get("https://w00t.com");
expect(data).toStrictEqual({yes: "It is"});
expect(status).toStrictEqual(201);
});});
```# axiosStubsRecorder
Intercepts all axios calls and generates a stubs file for later use.
```js
import { axiosStubsRecorder } from 'axios-stubber';test('t1 - save to new file', async () => {
const axiosMockControl = axiosStubsRecorder(axios, 'my.stubs.json');let {data} = await axios.get("https://reqres.in/api/users/3");
expect(data.data.first_name).toEqual('Emma');
expect(data.data.last_name).toEqual('Wong');// you can now check that 'my.stubs.json' has all requests and responses recorded
axiosMockControl.restore();
});
```