Test helpers

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


The mocked test helper provides typings on your mocked modules and even their deep methods, based on the typing of its source. It make use of the latest TypeScript features 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 foo.name is mocked (or its methods if it's an object)