🌐
Jest
jestjs.io › es6 class mocks
ES6 Class Mocks · Jest
May 7, 2026 - We'll mock SoundPlayer in our tests for SoundPlayerConsumer. ... Calling jest.mock('./sound-player') returns a useful "automatic mock" you can use to spy on calls to the class constructor and all of its methods.
🌐
Stack Overflow
stackoverflow.com › questions › 74904911 › how-can-i-mock-a-class-using-jest
How can I mock a class using jest?
} ] } jest.mock('../somefile'); const mockActualExportHelper = jest.requireActual('../somefile').ExportHelper; const mockGetInstanceImpl = () => {}; // this says cannot read property instances of undefined const mockExportHelper = mockActualExportHelper.mock.instances[0]; mockExportHelper.getInstance.mockImplementation(mockGetInstanceImpl); mockExportHelper.transfer.mockImplementation(mockGetInstanceImpl); // Act await myPublish(eventMock, jasmine.any({})); // Assert expect(ExportHelper.getInstance).toBeCalled(); expect(ExportHelper.transfer).toBeCalled(); // also not sure if this is valid to use ExportHelper }); });
Discussions

I hate mocking Typescript classes with Jest
Jest is a pain to make it work with TypeScript anyway. Try using something like Vitest with TypeScript support out of the box More on reddit.com
🌐 r/typescript
49
64
May 30, 2023
Jest Mocking.. mocking an internal property of a factory function
Hi OP. As a general rule, whenever you find yourself in a situation where you are having to mock things in Jest, and especially when it isn't straightforward, you should consider refactoring your code to use Dependency Injection. It looks like the solution to your problem as-is is using `jest.requireActual` and mocking the return. I would not recommend this though, as it opens the door to side-effects appearing in your unit tests in the future without any change to this test on your part. More on reddit.com
🌐 r/typescript
6
1
June 19, 2024
Is changing the prototype of a class to mock a method a bad practice?
jest.mock('./DocRepo') already mocked your whole module, so you don't need to assign your functions manually, you can do: DocRepo.prototype.fetchDocs.mockResolvedValueOnce([]) Here fetchDocs is already a jest mock function. TS isn't aware of this, so I'm using such helper export const asMock = (fn: unknown) => fn as jest.Mock; And then: asMock(DocRepo.prototype.fetchDocs).mockResolvedValueOnce([]) Other person suggested to use DI instead, and you should know how DI works and what problems does it resolve, but it's not necessary, it's up to you whether to use DI or to mock with available functions. They suggested Sinon - but what's the point if you already have Jest that can do pretty everything. Testing implementation or not is also up to you, I prefer mocking as few things as possible, writing tests that interact with a running test db, so I'm sure my system is functioning as a whole, but most people prefer writing Unit tests that are coupled to the code, testing only a small part and making refactoring more cumbersome. Both approaches are valid and are heavily used in large scale systems. More on reddit.com
🌐 r/node
14
13
March 14, 2024
[AskJS] Any emerging new libraries to replace Jest?
While I agree with you on many of your points against Jest, I don't think there's anything that fundamentally fixes any of them. There's vitest but that's basically a rewrite of Jest with most of the same paradigms. What I've always done, regardless of the framework, is to wrap up any of those weird behaviors in helper functions which abstract any weirdness that is needed to make it work. That way you can create a nice API for whatever weird mocks, spies, etc that you might have and hide all of that messiness away from the rest of the code. You'll need to do that no matter what framework you are using. More on reddit.com
🌐 r/javascript
47
29
February 17, 2024
🌐
Meticulous
meticulous.ai › blog › mocking-a-javascript-class-with-jest-two-ways-to-make-it-easier
Mocking a JavaScript Class with Jest, Two Ways to Make it Easier
A guide on how to mock a JavaScript class using Jest, comparing dependency injection and mocking, along with guides on how to implement both.
🌐
Medium
medium.com › @davguij › mocking-typescript-classes-with-jest-8ef992170d1d
Mocking TypeScript classes with Jest | by David Guijarro | Medium
November 21, 2017 - So let’s mock the Http class and then use the mock for our User class test, instead of the real class. Fortunately, Jest makes this pretty easy, but there are a couple of gotchas to which we’ll get later.
🌐
Jest
jestjs.io › mock functions
Mock Functions · Jest
May 7, 2026 - Mock functions are also known as "spies", because they let you spy on the behavior of a function that is called indirectly by some other code, rather than only testing the output.
Find elsewhere
🌐
LinkedIn
linkedin.com › pulse › demystify-es6-class-mocking-jest-examples-gaurav-kumar-singh-ytvwf
Demystify ES6 class mocking in Jest (with examples)
March 27, 2024 - The factory function doesn't return a function rather it returns an object with the key that is same as the class export name i.e RedisClient. Hence, It will be able to import the mocked function correctly. If you want, you can try changing the module factory to return function like below · jest.mock("../db/redis", () => { return function () { return { getValue: () => "Mocked value", addValue: () => ({ status: "ok" }), }; }; });
🌐
Chris Boakes
chrisboakes.com › mocking-javascript-class-inner-functions-with-jest
Mocking JavaScript Class Inner Functions With Jest | Chris Boakes
// This will fail const barGreetWorld = jest.fn(); jest.mock( './bar', () => { return jest.fn().mockImplementation(() => { return { greetWorld: barGreetWorld }; }); }); You can read more about this way of mocking classes in the Jest documentation here.
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › ise developer blog › jest mocking best practices
Jest Mocking Best Practices - ISE Developer Blog
November 4, 2024 - By using the jest.mock() method, we can specify the library to mock and add the list of dependencies desired, including the classes that need to be constructed.
🌐
DEV Community
dev.to › andrewchaa › mocking-es6-class-instances-with-jest-p6c
Mocking ES6 Class Instances with Jest - DEV Community
May 3, 2024 - We mock the MongoClient class by creating a jest.fn that returns an object with the db method mocked. The db method is also mocked as a jest.fn that returns an object with the collection method mocked.
🌐
DhiWise
dhiwise.com › post › how-to-effectively-use-jest-mock-class-constructor-in-your-cod
Mastering Jest Mock Class Constructor
July 2, 2024 - A jest mock class constructor is a substitute for the actual constructor function of a class. It's used in unit tests to verify that the class is being correctly used by the rest of the application.
🌐
Vitest
vitest.dev › guide › mocking › classes
Mocking Classes | Vitest
March 23, 2026 - Generally speaking, you would re-create a class like this inside the module factory if the class is re-exported from another module: ... import { Dog } from './dog.js' vi.mock(import('./dog.js'), () => { const Dog = vi.fn(class { feed = vi.fn() ...
🌐
Medium
tobim-dev.medium.com › write-es6-class-mocks-for-your-jest-unit-tests-in-typescript-f926617e8bd0
Write ES6 Class mocks for your Jest Unit Tests in TypeScript | by Tobias Daffner | Medium
August 7, 2021 - Manual mock — Provide a mock implementation of the class in the __mocks__folder · Module factory function — Provide a mock implementation in a factory function, when calling jest.mock()in your test file.
🌐
Jest
jestjs.io › manual mocks
Manual Mocks · Jest
May 7, 2026 - Since we'd like our tests to avoid actually hitting the disk (that's pretty slow and fragile), we create a manual mock for the fs module by extending an automatic mock. Our manual mock will implement custom versions of the fs APIs that we can build on for our tests: ... Now we write our test. In this case jest.mock('fs') must be called explicitly, because fs is Node’s built-in module:
🌐
Jest
jestjs.io › the jest object
The Jest Object · Jest
May 7, 2026 - Creates a new mock function. The new function has no formal parameters and when called will return undefined. This functionality also applies to async functions. Creates a new class.
🌐
Domenico Luciani
domenicoluciani.com › 2022 › 06 › 17 › how-to-mock-with-jest-typescript.html
How to mock with Jest and Typescript · Domenico Luciani
June 17, 2022 - We can just create a new object which reflects the same properties composition of the dependency we need to mock and that’s it, Typescript compiler will identify that object as the same type automagically allowing us to use it seamless . ✨ · As you can notice, I’ve replaced the implementation with jest.fn() which allows us to mock that function using jest functionality.
🌐
Kulshekhar
kulshekhar.github.io › mock es6 class
Mock ES6 class | ts-jest
April 5, 2026 - ts-jest · Version: 29.4 · TypeScript is transpiling your ts file and your module is likely being imported using ES2015s import. const soundPlayer = require('./sound-player'). Therefore creating an instance of the class that was exported as a default will look like this: new soundPlayer.default(). However if you are mocking the class as suggested by the documentation.