Utilities
A set of useful utility functions.
rs.stubEnv
-
Alias:
rstest.stubEnv -
Type:
(name: string, value: string | undefined) => Rstest & Disposable
Temporarily sets an environment variable in process.env and import.meta.env to the specified value. Useful for testing code that depends on environment variables.
- If
valueisundefined, the variable will be removed fromprocess.envandimport.meta.env. - You can call this multiple times to stub multiple variables.
- Use
rs.unstubAllEnvs()to restore all environment variables changed by this method.
Disposable / using syntax
Rstest supports the using syntax to restore the scoped env stub automatically when the block exits.
Example:
rs.unstubAllEnvs
-
Alias:
rstest.unstubAllEnvs -
Type:
() => Rstest
Restores all environment variables that were changed using rs.stubEnv to their original values.
- Call this after your test to clean up any environment changes.
- Automatically called before each test if
unstubEnvsconfig is enabled.
Example:
rs.stubGlobal
-
Alias:
rstest.stubGlobal -
Type:
(name: string | number | symbol, value: unknown) => Rstest & Disposable
Temporarily sets a global variable to the specified value. Useful for mocking global objects or functions.
- You can call this multiple times to stub multiple globals.
- Use
rs.unstubAllGlobals()to restore all globals changed by this method.
Disposable / using syntax
Rstest supports the using syntax to restore the scoped global stub automatically when the block exits.
Example:
rs.unstubAllGlobals
-
Alias:
rstest.unstubAllGlobals -
Type:
() => Rstest
Restores all global variables that were changed using rs.stubGlobal to their original values.
- Call this after your test to clean up any global changes.
- Automatically called before each test if
unstubGlobalsconfig is enabled.
Example:
rs.setConfig
-
Alias:
rstest.setConfig -
Type:
Dynamically updates the runtime configuration for the current test file. Useful for temporarily overriding test settings such as timeouts, concurrency, or mock behavior.
Example:
rs.resetConfig
-
Alias:
rstest.resetConfig -
Type:
() => void
Resets the runtime configuration that was changed using rs.setConfig back to the default values.
rs.getConfig
-
Alias:
rstest.getConfig -
Type:
() => RuntimeConfig
Retrieves the current runtime configuration for the test file. Useful for inspecting or logging the current settings.
Example:
rs.waitFor
-
Alias:
rstest.waitFor -
Type:
Retries callback until it succeeds (does not throw) or timeout is reached.
- If
optionsis a number, it is treated astimeout. - If timeout is reached, it throws the last error from the callback.
Example:
rs.waitUntil
-
Alias:
rstest.waitUntil -
Type:
Calls callback repeatedly only while it returns undefined (no value) or any falsy value.
It resolves when the callback returns a truthy value.
- If
optionsis a number, it is treated astimeout. - If the callback throws, execution is interrupted immediately and the error is thrown.
- If timeout is reached, it throws a timeout error.
Example: