Mock modules
Rstest supports mocking modules, which allows you to replace the implementation of modules in tests. Rstest provides utility functions in rs (rstest) for mocking modules. You can directly use the following methods to mock modules:
rs.mock
- Type:
<T = unknown>(moduleName: string | Promise<T>, factoryOrOptions?: (() => Partial<T>) | { spy: true } | { mock: true }) => void
Mocks and replaces the module specified in the first parameter.
rs.mock is hoisted to the top of the current module, so even if you execute import fn from 'some_module' before calling rs.mock('some_module'), some_module will be mocked from the beginning.
With factory function
If a factory function is provided as the second parameter, the module will be replaced with the return value of the factory function.
Basic example
With rs.fn() for call tracking
Use rs.fn() to create mock functions that can track calls and configure return values:
With rs.mockObject() for auto-mocking
Use rs.mockObject() to automatically mock all properties of an object:
Partial mock with importActual
Use import attributes with { rstest: 'importActual' } to load the original module, then combine with rs.mock to keep some original implementations:
With __mocks__ directory
If rs.mock is called without providing a factory function, it will attempt to resolve a module with the same name in the __mocks__ directory at the same level as the mocked module.
Resolution rules:
- Local modules: If there is a
__mocks__folder at the same level as the file being mocked containing a file with the same name, Rstest will use that file as the mock implementation. - npm dependencies: If there is a
__mocks__folder in the root directory containing a file with the same name as the mocked module, Rstest will use that file as the mock implementation. - Node.js built-in modules: If there is a
__mocks__folder in the root directory containing a file with the same name as the built-in module (e.g.,__mocks__/fs.mjs,__mocks__/path.ts), Rstest will use that file. Thenode:prefix will be ignored.
Example:
With { spy: true } option
If { spy: true } is provided as the second parameter, the module will be auto-mocked but the original implementations will be preserved. All exports will be wrapped in spy functions that track calls while still executing the original code.
This is useful when you want to assert that a function was called correctly without replacing its implementation.
- ESM modules: All named exports are wrapped in spy functions.
- CommonJS modules: In addition to wrapping exports, a
defaultexport is automatically added (pointing to the module itself) to preserveimport x from 'cjs-module'behavior.
With { mock: true } option
If { mock: true } is provided as the second parameter, the module will be auto-mocked with all function exports replaced by mock functions. Unlike { spy: true }, the original implementations are not preserved - mock functions return undefined by default.
This is useful when you want to completely replace a module's behavior and configure mock return values or implementations.
Type enhancement with Promise<T>
rs.mock supports passing a Promise<T> (via dynamic import) as the first parameter to get better type hints in IDEs. This only enhances type hints and has no impact on the module mocking capabilities.
rs.doMock
- Type:
<T = unknown>(moduleName: string | Promise<T>, factoryOrOptions?: (() => Partial<T>) | { spy: true } | { mock: true }) => void
Similar to rs.mock, but it is not hoisted to the top of the module. It is called when it's executed, which means that if a module has already been imported before calling rs.doMock, that module will not be mocked, while modules imported after calling rs.doMock will be mocked.
Supports the same options as rs.mock: factory function, __mocks__ directory, { spy: true }, and { mock: true }.
rs.hoisted
- Type:
<T = unknown>(fn: () => T) => T
rs.hoisted is a helper function that allows you to create values that can be accessed in hoisted functions like rs.mock factory functions. Like rs.mock, rs.hoisted is also hoisted to the top of the module, and it provides access to the rs utilities within the hoisted scope.
This is useful when you need to create mock functions or values that should be shared between the mock factory function and your test code.
In this example, rs.hoisted allows you to create a mock function using rs.fn() that can be used both in the rs.mock factory function and in your test assertions. Without rs.hoisted, you would not be able to access rs utilities in the scope where rs.mock factory functions are evaluated.
rs.importActual
- Type:
<T = Record<string, unknown>>(path: string) => Promise<T>
Loads the original implementation of a module even if it has already been mocked. Use rs.importActual when you want a partial mock so you can merge the real exports with your overrides.
Rstest also exposes a synchronous importActual option for static imports. Add the import attribute with { rstest: 'importActual' } to load the real module as the file evaluates:
rs.importMock
- Type:
<T = Record<string, unknown>>(path: string) => Promise<T>
Imports a module and all its properties as mock implementations.
rs.unmock
- Type:
(path: string) => void
Cancels the mock implementation of the specified module. After this, all calls to import will return the original module, even if it was previously mocked. Like rs.mock, this call is hoisted to the top of the file, so it will only cancel module mocks executed in setupFiles.
rs.doUnmock
- Type:
(path: string) => void
Same as rs.unmock, but it is not hoisted to the top of the file. The next import of the module will import the original module instead of the mock. This will not cancel modules that were imported before the mock.
rs.resetModules
- Type:
resetModules: () => RstestUtilities
Clears the cache of all modules. This allows modules to be re-executed when re-imported. This is useful for isolating the state of modules shared between different tests.
Does not reset mocked modules. To clear mocked modules, use rs.unmock or rs.doUnmock.
FAQ
Mocking re-exported modules
In some libraries, APIs are exposed via re-exports. For example, in React Router, certain APIs (such as useParams) are imported from react-router-dom but are actually re-exported from react-router.
When using Rspack, these re-exports may be further optimized: the exports can be resolved directly from the source module (e.g. react-router), skipping the intermediate module (react-router-dom).
In this case:
- Mocking
react-router-dommay not take effect - Mocking
react-routerdirectly will work as expected
If your mock does not work as expected, you can:
- Mock the module that is actually resolved at runtime:
- Disable the related bundler optimization in the test environment to prevent re-export flattening. Be careful with this: disabling it may lead to errors related to runtime circular dependencies.