globalSetup
- Type:
string | string[] - Default:
undefined
The globalSetup option in Rstest allows you to run setup and teardown code that executes once before all tests and after all tests complete. This is useful for:
- Starting and stopping databases
- Initializing test services
- Cleaning up resources after test runs
Global setup file formats
You can write global setup files in two formats:
Named functions (recommended)
Default function returning teardown
Global setup runs in a separate context: module-scope variables defined in the setup file are not accessible from tests. However, for node test workers, process.env mutations made in globalSetup are snapshotted after setup completes and propagated to each worker — setting environment variables here is a supported way to share values with node tests. For browser-mode tests, the environment variable changes made in the browser project's own globalSetup are propagated too: they are injected into the browser runtime env store (readable via process.env / import.meta.env), on top of the built-in NODE_ENV / RSTEST values, with explicit test.env config still taking precedence. Other host environment variables are not forwarded to browser tests, and in mixed projects the env changes from Node-side globalSetup are not merged into browser tests. globalSetup entries added or modified by browser provider hooks (modifyRstestConfig) are not picked up. Browser-mode watch runs do not execute globalSetup yet.
Multiple global setup files
When using multiple global setup files:
- Setup functions execute sequentially in the order provided
- Teardown functions execute in reverse order (LIFO - Last In, First Out)
- If any setup fails, the entire test run fails