Easy typed test mocks with Jest and TypeScript

Here’s a quick and simple way to create type-safe test mocks with Jest in TypeScript:

const fooMock: jest.Mocked<IFoo> = {
	fooMethod: jest.fn()
}

const testSubject = new ClientClass(fooMock);

This has a few useful properties:

You can also use TypeScript’s handy Pick utility type to get more specific type-safe mocks:

const fooMock: Pick<jest.Mocked<IFoo>, "fooMethod"> = {
	fooMethod: jest.fn()
}

Tech mentioned