79645490

Date: 2025-05-30 12:57:28
Score: 1
Natty:
Report link

I finally cracked it, I can't answer why my previous jest test worked on Windows and not my Mac, but https://stackoverflow.com/a/62777134/6260882, specifically: 'axios is a function, not an object'. I was able to get my mocks to work like this:

jest.mock('axios', () => Object.assign(jest.fn(), {
  post: jest.fn(() => Promise.resolve({ data: sessionKey })),
  isAxiosError: jest.fn(),
}));

But I had problems being able to change the mocks on the fly because jest.mock is hoisted to the top. So I eventually decided to use AxiosMockAdapter since it had built in what I was trying to replicate.

test.spec.ts

import AxiosMockAdapter from 'axios-mock-adapter';
...
const mockedAxios = new AxiosMockAdapter(axios);
...
mockedAxios.onPost().reply(200, sessionKey);
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: jstokes