Function mockImport

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

    Works with either ESM or CommonJS modules, but as with import() of CommonJS modules, the module.exports value will be set as the default property on the resolved object, making @tapjs/mock!index.TapMock#mockRequire somewhat more intuitive in those cases.

    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 = await t.mockImport<
    typeof import('../my-thing.js')
    >('../my-thing.js', {
    some: { tricky: 'mocks' },
    })

    Note: The terms "mock" and "import" are unfortunately very overloaded in the testing space. This is not "mock all imports of this module". It's "load this module, but with its imports mocked". The code of the target module is run normally, but its dependencies are injected with the supplied values, which is useful for triggering hard-to-reach error cases and other situations.

    It is also useful for just loading a fresh copy of a module in your tests, if for example your program behaves differently based on environment variables or other system settings. For example:

    t.test('windows behavior', async t => {
    t.intercept(process, 'platform', { value: 'win32' })
    const myThing = t.mockImport('../my-thing.js')
    t.equal(myThing.separator, '\\')
    })
    t.test('posix behavior', async t => {
    t.intercept(process, 'platform', { value: 'linux' })
    const myThing = t.mockImport('../my-thing.js')
    t.equal(myThing.separator, '/')
    })

    Type Parameters

    • T = any

    Parameters

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

    Returns Promise<T>