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.
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:
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, themodule.exports
value will be set as thedefault
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:
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: