Constructors

Properties

Methods

Spies, Mocks, and Fixtures

Constructors

Properties

#allMock: Record<string, any>
#didTeardown: boolean = false
#mocks: MockService[] = []
#refs: Map<TestBase, TapMock> = ...

Methods

  • Set some mocks that will be used for all subsequent @tapjs/mock!index.TapMock#mockImport and @tapjs/mock!index.TapMock#mockRequire calls made by this test.

    Mocks added with mockAll are overridden by any explicit mocks set in the t.mockRequire or t.mockImport call.

    Repeated calls to t.mockAll() will add mocks to the set. If the same name is used again, it will replace the previous value, not merge.

    If a key is set to undefined or null, then it will be removed from the mockAll set.

    Reset by calling t.mockAll(null)

    Call with no args to return the current mockAll object.

    Parameters

    • Optional mocks: null | Record<string, any>

    Returns Record<string, any>

Spies, Mocks, and Fixtures

  • Convenience method to create a mock from an existing object by overriding some (possibly deeply nested) methods or properties.

    Example:

    import * as fs from 'node:fs'
    const mockedThing = t.mockRequire('./module.js', t.createMock(
    { fs },
    { fs: { statSync: myMockedStatSync }}
    )

    This can also appear anywhere in the object hierarchy, which may be more convenient in some cases:

    import * as blah from '@long-name/blah-api'
    const mockedThing = t.mockRequire('./module.js', {
    fs: t.createMock(fs, { statSync: myMockedStatSync }),
    child_process: t.createMock(child_process, { spawn: mockSpawn }),
    '@long-name/blah-api': t.createMock(blah, {
    some: {
    nested: {
    prop: true
    }
    }
    })
    })

    To remove a property, set it as undefined in the override.

    Type Parameters

    • B extends any[] | {
          [k: PropertyKey]: any;
      }
    • O extends any[] | {
          [k: string]: any;
      }

    Parameters

    • bases: B
    • overrides: O

    Returns MockedObject<B, O>

  • 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' },
    })

    Type Parameters

    • T = any

    Parameters

    • module: string
    • mocks: Record<string, any> = {}

    Returns Promise<T>

  • 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
    • mocks: Record<string, any> = {}

    Returns T

  • Unwind the mocks and free up the memory at the end of the test.

    Called automatically if the @tapjs/after plugin is not disabled.

    Returns void

Generated using TypeDoc