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.
beforeAll also supports returning a function that runs after all tests for cleanup (equivalent to afterAll):
afterAll
- Type:
(fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => void
Runs once after all tests in the current suite.
ctx.meta is the mutable metadata object for the current suite. If the suite was declared with describe(name, { meta }, fn), ctx.meta starts with that metadata after suite inheritance has been applied. File-level hooks write metadata to TestFileResult.meta; hooks inside a describe block write metadata to that suite result passed to custom reporters.
beforeEach
- Type:
(fn: (ctx: TestContext) => void | Promise<void>, timeout?: number) => void
See TestContext for the ctx fields (the same applies to the per-test hooks below).
Runs before each test in the current suite.
beforeEach also supports returning a function that runs after each test for cleanup (equivalent to afterEach):
afterEach
- Type:
(fn: (ctx: TestContext) => void | Promise<void>, timeout?: number) => void
Runs after each test in the current suite.
onTestFinished
- Type:
(fn: (ctx: TestContext) => 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.
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.
onTestFailed
- Type:
(fn: (ctx: TestContext) => void | Promise<void>, timeout?: number) => void
Called after the test has failed.
It should be noted that when you use the onTestFailed 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 onTestFailed hook belongs in concurrent tests.