Utilities
A set of useful utility functions.
rs.stubEnv
-
Alias:
rstest.stubEnv -
Type:
(name: string, value: string | undefined) => RstestUtilities & 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. -
Example:
usingsyntax
rs.stubEnv() returns a Disposable that works with the using syntax to restore the current env stub automatically when the block exits.
The _env binding is intentionally unused, as indicated by its leading underscore. A binding is required by the using syntax, so it cannot be omitted.
rs.unstubAllEnvs
-
Alias:
rstest.unstubAllEnvs -
Type:
() => RstestUtilities
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 the
unstubEnvsconfig is enabled.
Example:
When stubbing environment variables in multiple tests, call rs.unstubAllEnvs() in an afterEach hook to restore them after every test:
rs.stubGlobal
-
Alias:
rstest.stubGlobal -
Type:
(name: string | number | symbol, value: unknown) => RstestUtilities & 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. -
Example:
usingsyntax
rs.stubGlobal() returns a Disposable that works with the using syntax to restore the current global stub automatically when the block exits.
The _global binding is intentionally unused, as indicated by its leading underscore. A binding is required by the using syntax, so it cannot be omitted.
rs.unstubAllGlobals
-
Alias:
rstest.unstubAllGlobals -
Type:
() => RstestUtilities
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 the
unstubGlobalsconfig is enabled.
Example:
When stubbing global variables in multiple tests, call rs.unstubAllGlobals() in an afterEach hook to restore them after every test:
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:
The return value also includes a copy of the resolved expect.poll configuration. Modifying this copy does not change the runtime configuration.
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: