https://github.com/codex-central/mock-axios-request
Functions to allow test the request and responses in Axios format
https://github.com/codex-central/mock-axios-request
axios javascript jest mock testing typescript
Last synced: 11 months ago
JSON representation
Functions to allow test the request and responses in Axios format
- Host: GitHub
- URL: https://github.com/codex-central/mock-axios-request
- Owner: Codex-Central
- License: apache-2.0
- Created: 2023-12-18T21:18:01.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-21T23:16:49.000Z (over 2 years ago)
- Last Synced: 2025-05-27T23:49:24.819Z (about 1 year ago)
- Topics: axios, javascript, jest, mock, testing, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@codexcentral/mock-axios-request
- Size: 33.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MockAxiosRequest
Functions to allow test the request and responses in Axios format.
## Installation
> `npm install @codexcentral/mock-axios-request`
## Usage
### 1. Importing and creating a new instance
```javascript
import { MockApiClient, MockApiClientConfig } from '@codexcentral/mock-axios-request';
const baseUrl = 'https://example.com/api';
const mockApi = new MockApiClient(baseUrl);
```
### 2. Mocking a request
#### GET
```javascript
try {
const config: MockApiClientConfig = {
endpoint: '/users',
status: 200, // Change this for the status you want to test
response: [
{ id: 1, name: 'User 1' },
{ id: 2, name: 'User 2' },
{ id: 3, name: 'User 3' },
{ id: 4, name: 'User 4' },
],
};
const data = await mockApi.get<[{ id: string; name: string }]>(config);
console.log('Data:', data);
} catch (error) {
console.error('Error:', error);
}
```
#### POST
```javascript
try {
const config: MockApiClientConfig = {
endpoint: '/users/5',
status: 201, // Change this for the status you want to test
request: { name: 'User 5' },
response: { id: 5, name: 'User 5' },
};
const data = await mockApi.post<{ id: string; name: string }>(config);
console.log('Data:', data);
} catch (error) {
console.error('Error:', error);
}
```
#### PUT
```javascript
try {
const config: MockApiClientConfig = {
endpoint: '/users/5',
status: 200, // Change this for the status you want to test
request: { name: 'User 5' },
response: { id: 5, name: 'User 5' },
};
const data = await mockApi.put<{ id: string; name: string }>(config);
console.log('Data:', data);
} catch (error) {
console.error('Error:', error);
}
```
#### DELETE
```javascript
try {
const config: MockApiClientConfig = {
endpoint: '/users/5',
status: 200, // Change this for the status you want to test
response: { id: 5, name: 'User 5' },
};
const data = await mockApi.delete<{ id: string; name: string }>(config);
console.log('Data:', data);
} catch (error) {
console.error('Error:', error);
}
```
### MockApiClientConfig
| Attribute | Type | Mandatory |
| ------ | ------ | ------ |
| endpoint | `string` | true |
| status | `number` | true (100 to 599) |
| headers | `array` | false |
| response | `array` | false |
| response | `array` | false |
| delayResponse | `number` | false (default: 1000 - in milliseconds) |
#### Example of MockApiClientConfig
```json
{
"endpoint": "/users",
"status": 200,
"headers": [],
"response": [],
"response": [],
"delayResponse": 1000
}
```
# Credits
These code was written by [Roberto Silva Z.](https://www.linkedin.com/in/robertosilvazuniga/)