Socket
Socket
Sign inDemoInstall

axios-mock-adapter

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

axios-mock-adapter

Axios adapter that allows to easily mock requests


Version published
Weekly downloads
1.5M
decreased by-1.27%
Maintainers
1
Weekly downloads
 
Created

What is axios-mock-adapter?

axios-mock-adapter is a package for mocking Axios requests for testing purposes. It allows developers to simulate server responses, test error handling, and ensure that the front-end behaves as expected without the need for an actual backend during the development and testing phases.

What are axios-mock-adapter's main functionalities?

Mocking GET requests

This feature allows you to mock a GET request to a specific URL and provide a fake response that the axios call will receive.

const MockAdapter = require('axios-mock-adapter');
const axios = require('axios');

const mock = new MockAdapter(axios);

mock.onGet('/users').reply(200, [
  { id: 1, name: 'John Smith' }
]);

Mocking POST requests

This feature allows you to mock a POST request to a specific URL and provide a fake response, which can be used to simulate form submissions or other POST operations.

const MockAdapter = require('axios-mock-adapter');
const axios = require('axios');

const mock = new MockAdapter(axios);

mock.onPost('/login').reply(200, {
  user: 'admin',
  token: 'fake-token'
});

Simulating network errors

This feature allows you to simulate network errors to test how your application handles them.

const MockAdapter = require('axios-mock-adapter');
const axios = require('axios');

const mock = new MockAdapter(axios);

mock.onGet('/unreachable').networkError();

Delaying responses

This feature allows you to delay the mock response, which can be useful for testing loading states and asynchronous operations.

const MockAdapter = require('axios-mock-adapter');
const axios = require('axios');

const mock = new MockAdapter(axios);

mock.onGet('/users').reply(function() {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve([200, [{ id: 1, name: 'John Smith' }]]);
    }, 1000);
  });
});

Specifying request parameters

This feature allows you to specify request parameters for the mock, so you can test how your application handles different query strings or request configurations.

const MockAdapter = require('axios-mock-adapter');
const axios = require('axios');

const mock = new MockAdapter(axios);

mock.onGet('/users', { params: { search: 'John' } }).reply(200, [
  { id: 1, name: 'John Smith' }
]);

Other packages similar to axios-mock-adapter

Keywords

FAQs

Package last updated on 01 Jun 2022

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