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
Differences from setupFiles
Choose the scope that matches the resource you need:
setupFiles does not recognize exported setup or teardown functions. Its top-level code runs before each file and can register hooks for later cleanup.
Global setup file formats
You can write global setup files in two formats:
Named functions (recommended)
Either export can be omitted; a file that exports neither and has no default function fails the run.
Default function returning teardown
Sharing values with tests
Global setup runs in a separate setup worker. Tests cannot access its module-scope variables, such as a database connection. Share connection details through environment variables, then create the connection in the test environment.
Environment variable changes made in global setup are passed to Node.js test workers without changing the host process's process.env.
Browser projects
For Browser Mode, environment sharing follows these rules:
- Changes from the browser project's own
globalSetupare available throughprocess.envandimport.meta.env, alongside the built-inNODE_ENVandRSTESTvalues. - Explicit
envconfiguration takes precedence over values from global setup. - Other host environment variables are not forwarded automatically. In mixed Node.js and browser projects, Node.js projects' global setup changes are not merged into browser tests.
Browser provider hooks (modifyRstestConfig) run during test discovery, before the setup stage. Any globalSetup entries they add or change are therefore included.
Watch mode
Once a project's global setup succeeds, watch reruns reuse it. The teardown runs when the watch session closes. Put initialization that must repeat for each test file in setupFiles.
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