Hooks
Signature
beforeAll(callback: () => void): voidSignature
afterAll(callback: () => void): voidSignature
beforeEach(callback: () => void): voidSignature
afterEach(callback: () => void): voidbeforeAll runs once before the first test of its suite and afterAll runs once after the last. beforeEach and afterEach run around every test of their suite and of every nested suite: beforeEach from the outermost suite inward, and afterEach in the reverse order. Hooks registered outside any describe apply to the whole file.
afterEach runs even when the test failed, so resources acquired in setup are released. A suite with no test left to run, because of .skip or .only, does not run its hooks.
A failing beforeAll fails every test of its suite without running them. A failing beforeEach fails its test, and a failing afterEach is added to the report of the test it ran after.
Hooks can be async too; each one is awaited before the test it prepares.
tyrus
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach } from "tyrus:test";
describe("hooks", () => {
beforeAll(() => {
print("once, before the first test");
});
beforeEach(() => {
print("before every test");
});
afterEach(() => {
print("after every test");
});
afterAll(() => {
print("once, after the last test");
});
it("runs between the hooks", () => {
expect(1 + 1).toBe(2);
});
});