Hooks

Hooks allow you to run setup and teardown logic before or after your tests or test suites.

beforeAll

  • Type: (fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => void

Runs once before all tests in the current suite.

import { beforeAll } from '@rstest/core';

beforeAll(async (ctx) => {
  // Setup logic before all tests
  // ctx.filepath gives the current test file path
});

afterAll

  • Type: (fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => void

Runs once after all tests in the current suite.

import { afterAll } from '@rstest/core';

afterAll(async (ctx) => {
  // Cleanup logic after all tests
});

beforeEach

  • Type: (fn: () => void | Promise<void>, timeout?: number) => void

Runs before each test in the current suite.

import { beforeEach } from '@rstest/core';

beforeEach(async () => {
  // Setup logic before each test
});

afterEach

  • Type: (fn: () => void | Promise<void>, timeout?: number) => void

Runs after each test in the current suite.

import { afterEach } from '@rstest/core';

afterEach(async () => {
  // Cleanup logic after each test
});

onTestFinished

  • Type: (fn: () => void | Promise<void>, timeout?: number) => void

Called after the test has finished running whatever the test result is. This can be used to perform cleanup actions. This hook will be called after afterEach.

import { onTestFinished, test } from '@rstest/core';

test('test server', () => {
  const server = startServer();
  // Register a cleanup function to close the server after the test
  onTestFinished(() => server.close());

  server.listen(3000, () => {
    console.log('Server is running on port 3000');
  });
});

It should be noted that when you use the onTestFinished hook in concurrent tests, you should get the hook from the test context. This is because Rstest cannot accurately track the specific test to which the global onTestFinished hook belongs in concurrent tests.

describe.concurrent('concurrent suite', () => {
  test('test 1', async ({ onTestFinished }) => {
    /* ... */
  });
  test('test 2', async ({ onTestFinished }) => {
    /* ... */
  });
});