Function mockRequire

  • Load the supplied module synchronously using require(), replacing any of the referenced modules with the mocks provided.

    Only works with CommonJS modules.

    For type info, cast using as typeof import(...) or use the type parameter, as TypeScript lacks a way to infer imports dynamically.

    For example:

    const myThing = t.mockRequire<
    typeof import('../my-thing.js')
    >('../my-thing.js', {
    some: { tricky: 'mocks' },
    })

    Type Parameters

    • T = any

    Parameters

    • module: string
    • Optionalmocks: Record<string, any>

    Returns T