Skip to content

Hooks ​

AssinaturabeforeAll(callback: () => void): void
AssinaturaafterAll(callback: () => void): void
AssinaturabeforeEach(callback: () => void): void
AssinaturaafterEach(callback: () => void): void

beforeAll executa uma vez antes do primeiro teste da sua suíte e afterAll executa uma vez depois do último. beforeEach e afterEach executam em torno de cada teste da sua suíte e de todas as suítes aninhadas: beforeEach da suíte mais externa para dentro, e afterEach na ordem inversa. Hooks registrados fora de qualquer describe valem para o arquivo inteiro.

afterEach executa mesmo quando o teste falhou, então os recursos adquiridos na preparação são liberados. Uma suíte sem nenhum teste para executar, por causa de .skip ou .only, não executa seus hooks.

Um beforeAll que falha reprova todos os testes da sua suíte sem executá-los. Um beforeEach que falha reprova o seu teste, e um afterEach que falha é acrescentado ao relatório do teste depois do qual executou.

Hooks também podem ser async; cada um é aguardado antes do teste que prepara.

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);
    });
});