For AI agents: the complete documentation index is available at https://rstest.rs/llms.txt, the full documentation bundle is available at https://rstest.rs/llms-full.txt, and this page is available as Markdown at https://rstest.rs/config/test/setup-files.md.
close
  • English
  • setupFiles

    • Type: string | string[]
    • Default: []

    A list of paths to modules that run some code to configure or set up the testing environment, they will be run before each test file.

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      setupFiles: ['./scripts/rstest.setup.ts'],
    });

    Registering hooks

    Setup files run in the test environment before the test file is loaded, so you can import Rstest APIs to register hooks or extend matchers. For example, restore spies after each test:

    scripts/rstest.setup.ts
    import { afterEach, rs } from '@rstest/core';
    
    afterEach(() => {
      rs.restoreAllMocks();
    });

    The setup file's top-level code runs once per test file; the afterEach callback runs after each test. This also applies when isolate is false.

    Unlike globalSetup, setup files do not use exported setup or teardown functions. Register beforeAll, beforeEach, afterAll, or afterEach to control initialization and cleanup. See the execution scope comparison to choose between them.