Private
#allPrivate
#didPrivate
#mocksPrivate
#tPrivate
Static
#refsSet 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' },
})
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' },
})
Generated using TypeDoc
Implementation class providing the @tapjs/mock!index.TapMock#mockRequire, @tapjs/mock!index.TapMock#mockImport, and @tapjs/mock!index.TapMock#createMock methods.