Jest

Framework de testing JavaScript - zero config, snapshots, mocking y cobertura de codigo listos para usar

TL;DR

Qué: Un framework de testing JavaScript encantador con cero configuración.

Por qué: Rápido, testing con snapshots, excelente mocking, cobertura integrada, ampliamente adoptado.

Quick Start

Instalación:

npm install --save-dev jest

Añadir a package.json:

{
  "scripts": {
    "test": "jest"
  }
}

Crear archivo de test (sum.test.js):

const sum = (a, b) => a + b;

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

Ejecutar tests:

npm test

Cheatsheet

MatcherDescripción
toBe(value)Igualdad exacta
toEqual(obj)Igualdad profunda
toBeTruthy()Valor truthy
toBeNull()Comprobación null
toContain(item)Array contiene
toThrow()Lanza error
toHaveBeenCalled()Mock fue llamado

Gotchas

Async testing

// Async/await
test('fetches data', async () => {
  const data = await fetchData();
  expect(data).toBe('data');
});

// Promises
test('resolves to data', () => {
  return expect(fetchData()).resolves.toBe('data');
});

Mocking

// Función mock
const mockFn = jest.fn();
mockFn.mockReturnValue(42);
mockFn.mockResolvedValue('async result');

// Mock de módulo
jest.mock('./api', () => ({
  fetchUser: jest.fn().mockResolvedValue({ name: 'John' })
}));

Snapshot testing

test('renders correctly', () => {
  const tree = renderer.create(<Button>Click</Button>).toJSON();
  expect(tree).toMatchSnapshot();
});

Setup and teardown

beforeAll(() => { /* Ejecutar una vez antes de todos los tests */ });
afterAll(() => { /* Ejecutar una vez después de todos los tests */ });
beforeEach(() => { /* Ejecutar antes de cada test */ });
afterEach(() => { /* Ejecutar después de cada test */ });

Next Steps