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.
Optional
mocks: null | Record<string, any>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.
Deprecated alias for @tapjs/mock!index.TapMock#mockRequire
Prints a warning to stderr the first time it used, otherwise identical.
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, '/')
})
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' },
})
Implementation class providing the @tapjs/mock!index.TapMock#mockRequire, @tapjs/mock!index.TapMock#mockImport, and @tapjs/mock!index.TapMock#createMock methods.