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

135
Views
Extract jest.mock to seperate util function

I want to make a general jest.mock function to use in multiple test files. The first example works when I use jest.mock directly inside the test file. However the second example doesn't

// EXAMPLE 1
// this works
jest.mock("third-paty-module", () => {
  return {
    MyComponent: props => {
      return <input {...props} />;
    }
  };
});
test("my test", () => {
  // then assert
});

// EXAMPLE 2
// this doesn't work

// test.config.js
export function mockCustom() {
  jest.mock("third-paty-module-which-uses-webcomponents", () => {
    return {
      MyComponent: props => {
        return <input {...props} />;
      }
    };
  });
}

// file.test.js
import { mockCustom } from "../../config/test.config.js";

mockCustom();

test("my test", () => {
  // then assert
});

I get no errors when using jest.doMock but that doesn't mock my component at all.

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!