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
rstest.config.ts
Global setup file formats
You can write global setup files in two formats:
Named functions (recommended)
Default function returning teardown
Note
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. This propagation does not apply to browser-mode tests; only the static test.env config is forwarded to browser workers.
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
Differences from setupFiles
Example: database setup
rstest.config.ts