Function createMock

  • 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>