01
Primeiros passos
Teste básico
test/expect para asserções.
jest
test("adds numbers", () => {
expect(1 + 2).toBe(3);
});Describe
describe agrupa testes relacionados.
jest
describe("Math", () => {
test("add", () => expect(1+1).toBe(2));
test("sub", () => expect(2-1).toBe(1));
});Matchers
Matchers mais comuns.
jest
expect(value).toBe(other);
expect(arr).toContain(item);
expect(obj).toEqual({a: 1});
expect(fn).toThrow();
expect(promise).resolves.toBe(42);02
Recursos
Mock
jest.fn() cria funções mock.
jest
const mockFn = jest.fn();
mockFn.mockReturnValue(42);
mockFn();
expect(mockFn).toHaveBeenCalled();Async
Testes assíncronos usam async/await.
jest
test("async", async () => {
const data = await fetchData();
expect(data).toBe("ok");
});Before/After
Hooks de setup e teardown.
jest
beforeEach(() => setup());
afterEach(() => cleanup());
afterAll(() => db.close());Snapshot
Testes de snapshot para componentes de UI.
jest
expect(component).toMatchSnapshot();Was this helpful?