Hooks allow you to run setup and teardown logic before or after your tests or test suites.
(fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => voidRuns once before all tests in the current suite.
beforeAll also supports returning a function that runs after all tests for cleanup (equivalent to afterAll):
(fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => voidRuns once after all tests in the current suite.
(fn: () => void | Promise<void>, timeout?: number) => voidRuns before each test in the current suite.
beforeEach also supports returning a function that runs after each test for cleanup (equivalent to afterEach):
(fn: (ctx: { task: { result: Readonly<TestResult> } }) => void | Promise<void>, timeout?: number) => voidRuns after each test in the current suite.
(fn: (ctx: { task: { result: Readonly<TestResult> } }) => void | Promise<void>, timeout?: number) => voidCalled 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.
(fn: (ctx: { task: { result: Readonly<TestResult> } }) => void | Promise<void>, timeout?: number) => voidCalled 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.