dimanche 18 avril 2021

Mocking axios requests on specific urls with jest

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.

Aucun commentaire:

Enregistrer un commentaire