Skip to main content
Version: 27.1

Test helpers


This function is now deprecated and will be removed in 28.0.0. The function has been integrated into jest-mock package as a part of Jest 27.4.0, see Please use the one from jest-mock instead.

ts-jest provides some test utilities to be used in your test, related to TypeScript.

mocked<T>(item: T, deep = false)

The mocked test helper provides typings on your mocked modules and even their deep methods, based on the typing of its source. It makes use of the latest TypeScript feature, so you even have argument types completion in the IDE (as opposed to jest.MockInstance).

Note: while it needs to be a function so that input type is changed, the helper itself does nothing else than returning the given input value.


// foo.ts
export const foo = {
a: {
b: {
c: {
hello: (name: string) => `Hello, ${name}`,
name: () => 'foo',
// foo.spec.ts
import { mocked } from 'ts-jest/utils'
import { foo } from './foo'

// here the whole foo var is mocked deeply
const mockedFoo = mocked(foo, true)

test('deep', () => {
// there will be no TS error here, and you'll have completion in modern IDEs
// same here

test('direct', () => {
// here only is mocked (or its methods if it's an object)