Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-mock-axios

Package Overview
Dependencies
Maintainers
0
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-axios

Axios mock for Jest

  • 4.8.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created

What is jest-mock-axios?

jest-mock-axios is a Jest mock for Axios, a popular HTTP client for making requests. It allows developers to mock Axios requests in their Jest tests, making it easier to test code that relies on HTTP requests without actually making those requests.

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

Mocking GET Requests

This feature allows you to mock GET requests made with Axios. The code sample demonstrates how to mock a GET request and test a function that fetches data using Axios.

const mockAxios = require('jest-mock-axios');
const axios = require('axios');

// Function to test
async function fetchData() {
  const response = await axios.get('/data');
  return response.data;
}

// Jest test
it('fetches data successfully', async () => {
  const data = { name: 'John Doe' };
  mockAxios.get.mockResolvedValue({ data });

  const result = await fetchData();
  expect(result).toEqual(data);
});

Mocking POST Requests

This feature allows you to mock POST requests made with Axios. The code sample demonstrates how to mock a POST request and test a function that posts data using Axios.

const mockAxios = require('jest-mock-axios');
const axios = require('axios');

// Function to test
async function postData(data) {
  const response = await axios.post('/data', data);
  return response.data;
}

// Jest test
it('posts data successfully', async () => {
  const data = { name: 'John Doe' };
  const responseData = { success: true };
  mockAxios.post.mockResolvedValue({ data: responseData });

  const result = await postData(data);
  expect(result).toEqual(responseData);
});

Mocking Error Responses

This feature allows you to mock error responses from Axios requests. The code sample demonstrates how to mock an error response and test a function that handles errors when fetching data using Axios.

const mockAxios = require('jest-mock-axios');
const axios = require('axios');

// Function to test
async function fetchData() {
  try {
    const response = await axios.get('/data');
    return response.data;
  } catch (error) {
    throw new Error('Failed to fetch data');
  }
}

// Jest test
it('handles error response', async () => {
  mockAxios.get.mockRejectedValue(new Error('Network Error'));

  await expect(fetchData()).rejects.toThrow('Failed to fetch data');
});

Other packages similar to jest-mock-axios

Keywords

FAQs

Package last updated on 05 Nov 2024

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