Skip to content

Instantly share code, notes, and snippets.

@Itsindigo
Last active February 22, 2022 16:40
Show Gist options
  • Save Itsindigo/0e2191720b0b106b39ffd02853c5cdd7 to your computer and use it in GitHub Desktop.
Save Itsindigo/0e2191720b0b106b39ffd02853c5cdd7 to your computer and use it in GitHub Desktop.
How to partially mock a module with jest
import { functionWithDependencyThatNeedsMocking } from './service';
jest.mock('./service', () => {
...jest.requireActual('./service');
dependencyThatNeedsMocking: jest.fn()
})
it('should mock the call', () => {
const mySpy = jest.spyOn('./service', 'dependencyThatNeedsMocking')
functionWithDependencyThatNeedsMocking()
expect(mySpy).toHaveCallCount(1)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment