Mocking axios requests on specific urls with jest

  javascript, jestjs, mocking, testing, typescript

I’d like to know if it’s possible to mock axios requests with different values depending on url. I use jest and typescript. Now I have this working example:

import axios from 'axios';
import { mocked } from 'ts-jest';
jest.mock('axios');
    
const mAxios = mocked(axios, true);
mAxios.get.mockResolvedValue({
  data: [
    {
      id: 1,
      title: 'john doe',
    },
    {
      id: 2,
      title: 'keyboard cat',
    },
  ],
});

It works but as you can see it always returns the same value no matter to where request was send. I’m wondering how can I achieve my goal.

Source: Ask Javascript Questions

LEAVE A COMMENT