﻿---
title: Global setup hook
description: Use a global setup hook to run code once before any tests start (even with multiple workers). This is most useful for parallel suites, where you want...
url: https://www.elastic.co/elastic/docs-builder/docs/3109/extend/kibana/scout/global-setup-hook
products:
  - Kibana
---

# Global setup hook
Use a global setup hook to run code **once** before any tests start (even with multiple workers). This is most useful for [parallel suites](https://www.elastic.co/elastic/docs-builder/docs/3109/extend/kibana/scout/parallelism), where you want shared data/setup to exist before workers begin. It is also supported by non-parallel test suites.

### When to use

Global setup is most valuable when:
- **Running parallel suites**: shared data must exist before workers start
- **Heavy one-time ingestion**: data that takes significant time to load
- **Shared, immutable data**: data that all suites read but none modify

**Keep in `beforeAll`/`afterAll` instead** when:
- **Cleanup is required**: `kbnClient.importExport.load()` creates saved objects that should be removed with `.unload()` after tests. Since there's no global teardown, these operations belong in hooks.
- **Data isolation matters**: suites that create/modify data should manage their own setup and cleanup.

<tip>
  `esArchiver.loadIfNeeded()` is idempotent: only the first call ingests data; subsequent calls do a fast index-exists check and skip. For **sequential runs**, keeping it in `beforeAll` is fine (no benefit from global setup). For **parallel runs**, move it to global setup so ES isn't handling ingestion while workers are running (ingestion can affect Kibana performance).
</tip>

<note>
  Scout doesn’t currently have a global teardown hook. Most environments are ephemeral and are shut down after the run.
</note>


## Enable it

<stepper>
  <step title="Turn it on in your config">
    Set `runGlobalSetup: true` in your Playwright config:
    ```ts
    import { createPlaywrightConfig } from '@kbn/scout';

    export default createPlaywrightConfig({
      testDir: './parallel_tests',
      workers: 2,
      runGlobalSetup: true,
    });
    ```
  </step>

  <step title="Create `global.setup.ts`">
    Add `global.setup.ts` inside the `testDir` folder. Scout will discover and run it automatically.
    ```text
    test/scout/ui/
    └── parallel_tests/
        ├── global.setup.ts
        └── some_suite.spec.ts
    ```
  </step>

  <step title="Write setup code">
    Example: load an ES archive once:
    ```ts
    import { globalSetupHook } from '@kbn/scout';

    globalSetupHook('Load test data', async ({ esArchiver, log }) => {
      log.info('[setup] loading ES archive (only if needed)...');
      await esArchiver.loadIfNeeded('x-pack/platform/test/fixtures/es_archives/ml/farequote');
    });
    ```

    <warning>
      The global setup hook only has access to **worker-scoped** fixtures. It cannot use test-scoped fixtures like `page`, `browserAuth`, or `pageObjects`.
    </warning>
  </step>

  <step title="Run tests">
    Run tests as usual via [Run Scout tests](https://www.elastic.co/elastic/docs-builder/docs/3109/extend/kibana/scout/run-tests). The global setup hook will execute first—check console logs to verify it ran successfully.
  </step>
</stepper>