01
Primeros pasos
Test básico
test/expect para hacer aserciones.
jest
test("adds numbers", () => {
expect(1 + 2).toBe(3);
});Describe
describe agrupa los tests relacionados.
jest
describe("Math", () => {
test("add", () => expect(1+1).toBe(2));
test("sub", () => expect(2-1).toBe(1));
});Matchers
Los matchers más comunes.
jest
expect(value).toBe(other);
expect(arr).toContain(item);
expect(obj).toEqual({a: 1});
expect(fn).toThrow();
expect(promise).resolves.toBe(42);02
Características
Mock
jest.fn() crea funciones mock.
jest
const mockFn = jest.fn();
mockFn.mockReturnValue(42);
mockFn();
expect(mockFn).toHaveBeenCalled();Async
Los tests asíncronos usan async/await.
jest
test("async", async () => {
const data = await fetchData();
expect(data).toBe("ok");
});Before/After
Hooks de configuración y limpieza.
jest
beforeEach(() => setup());
afterEach(() => cleanup());
afterAll(() => db.close());Snapshot
Snapshot testing para componentes de UI.
jest
expect(component).toMatchSnapshot();Was this helpful?