Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Jest: Can't extract function that mocks a module

I'm trying to find a simple and reusable way to mock responses from http requests. Right now I have:

// foo.test.ts 

jest.mock('axios');
import axios from 'axios';

describe('when updating user', () => {
  beforeEach(() => {
    (axios.post as jest.Mock).mockReturnValue({
      data: updatedUser,
    });
    result = foo();

But I'd rather simply pass my expected return value to a function I can reuse throughout my application:

// foo.test.ts

import { mockPost } from './mock'

describe('when updating user', () => {
  beforeEach(() => {
    mockPost(updatedUser);
    result = foo();
  })

So I tried to extract this mock into another module like so:

// mock.ts

jest.mock('axios');
import axios from 'axios';

export function mockPost(item: unknown) {
  (axios.post as jest.Mock).mockReturnValue({
    data: item,
  });
}

But alas, the module is no longer mocked. Is this impossible and if so, why?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!