New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

axios-json-mock

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

axios-json-mock

Axios helper that allows to easily mock requests

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

axios-json-mock

Axios helper that allows to easily mock request

Installation

Using npm:

npm install axios-json-mock --save

axios-json-mock works on Node as well as in a browser, it works with axios v0.16.0 and above.

Example

Mocking a GET request

import axios from 'axios';
import AxiosJsonMock from 'axios-json-mock';

// Set default axios instance
const instance = new AxiosJsonMock(axios);

const responseWith = {
  200: [
    { id: 23, name: 'John Lennon' }
  ],
  400: {
    message: 'Bad Request'
  }
}
// arguments for instance are (request config, mock config)
instance({ url: '/users' }, {
  useMock: true,
  responseWith
})
  .then(response => console.log(response.data))

Add a specify status code

// arguments for instance are (request config, mock config)
instance({ url: '/users' }, {
  useMock: true,
  statusCode: 400,
  responseWith
})
  .catch(response => console.error(response))

To add a delay to responses, specify amout (in milliseconds)

const instance = new AxiosJsonMock(axios, { timeout: 1000 });

Using a mocks folder with json files

├── __dirname
│   ├── __mocks__
│   │   ├── userlist.json
│   │   ├── *.json
│   │   ├── *.json

userlist.json

{
  "200": [
    { "id": 23, "name": "John Lennon" }
  ],
  "400": {
    "message": "Bad Request"
  }
}
import axios from 'axios';
import AxiosJsonMock from 'axios-json-mock';

// Set default axios instance with mocks folder
const instance = new AxiosJsonMock(axios, { mockFolder: __dirname + '__mocks__' });

instance({ url: '/users' }, {
  useMock: true,
  mockName: 'userlist' // set json filename
})
  .then(response => console.log(response.data))
  

Turn off mocks and make real request

instance({ url: '/users' }, {
  useMock: false,
  mockName: 'userlist' // set json filename
})
  .then(response => console.log(response.data)) // real data

Keywords

FAQs

Package last updated on 25 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc