Documentacion/API/Testing

Testing

createTestBoundary e installMatchers.

Testing

createTestBoundary()

createTestBoundary() retorna un wrapper Boundary para tests y funciones para inspeccionar lo capturado.

1import { createTestBoundary } from "react-rescuer/testing";
2
3const tb = createTestBoundary();
4const { Boundary, getLastError, getLastContext, reset } = tb;
5
6// render(
7// <Boundary>
8// <Bomb />
9// </Boundary>
10// )
11
12// expect(getLastError()).toBeInstanceOf(Error);
13// expect(getLastContext()?.fingerprint).toBeTruthy();
14
15reset();
COPIAR

Matchers

La libreria puede registrar matchers personalizados en expect:

1import { installMatchers } from "react-rescuer/testing";
2
3installMatchers();
COPIAR

Matchers incluidos:

  • toHaveCaughtError()
  • toHaveCaughtErrorMatching(pattern)

Uso:

1import { createTestBoundary, installMatchers } from "react-rescuer/testing";
2
3installMatchers();
4
5const tb = createTestBoundary();
6// render con tb.Boundary...
7
8// expect(tb).toHaveCaughtError();
9// expect(tb).toHaveCaughtErrorMatching(/boom/);
COPIAR