Utilities
A set of useful utility functions.
rstest.stubEnv
-
Alias:
rs.stubEnv -
Type:
(name: string, value: string | undefined) => Rstest
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
rstest.unstubAllEnvs()to restore all environment variables changed by this method.
Example:
rstest.unstubAllEnvs
-
Alias:
rs.unstubAllEnvs -
Type:
() => Rstest
Restores all environment variables that were changed using rstest.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:
rstest.stubGlobal
-
Alias:
rs.stubGlobal -
Type:
(name: string | number | symbol, value: unknown) => Rstest
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
rstest.unstubAllGlobals()to restore all globals changed by this method.
Example:
rstest.unstubAllGlobals
-
Alias:
rs.unstubAllGlobals -
Type:
() => Rstest
Restores all global variables that were changed using rstest.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:
rstest.setConfig
-
Alias:
rs.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:
rstest.resetConfig
-
Alias:
rs.resetConfig -
Type:
() => void
Resets the runtime configuration that was changed using rstest.setConfig back to the default values.
rstest.getConfig
-
Alias:
rs.getConfig -
Type:
() => RuntimeConfig
Retrieves the current runtime configuration for the test file. Useful for inspecting or logging the current settings.
Example: