Test
Renderizza le app fuori schermo, premi i tasti e verifica i frame con @textjs/app/testing.
@textjs/app/testing (o @textjs/react/testing) esegue il rendering della tua app fuori schermo, preme i tasti e restituisce il frame come testo. I test vengono eseguiti senza browser o DevTools.
import { afterEach, expect, test } from "bun:test";import { cleanup, render } from "@textjs/app/testing";import { Counter } from "./counter";afterEach(cleanup);test("counts up", async () => { const app = await render(<Counter />, { cols: 40, rows: 6 }); await app.press("up up"); expect(app.lastFrame()).toContain("count = 2");});Funziona con qualsiasi runner che supporti test async, inclusi Bun, Vitest e Jest.
Rendering
| Metodo | Descrizione |
|---|---|
render(el, { cols, rows, theme }) | Montaggio fuori schermo (dimensioni predefinite 80×24) |
lastFrame() | Il frame come testo semplice |
lines() | Il frame come array di righe |
frames | Ogni frame distinto finora |
toAnsi() | ANSI Truecolor, per gli snapshot |
cell(x, y) | { char, fg, bg, attrs } |
locate(text) | { x, y } della prima corrispondenza |
rerender(el) / unmount() | Aggiornamento o smontaggio |
renderToString(el, options) esegue il rendering una volta e restituisce il testo.
Input
await app.press("down");await app.press("ctrl+k"); // modifiersawait app.press("g g"); // a sequenceawait app.press("G"); // Shift+Gawait app.type("hello\n"); // one key per character, \n is Enterawait app.paste("multi\nline");await app.wheel(3);await app.resize(120, 36);Ogni chiamata viene eseguita all’interno di act() e completa l’elaborazione dei frame risultanti, così l’asserzione successiva vede l’aggiornamento.
Tempo
await app.wait(300); // real time passes, then frames flushawait app.waitForText("Deployed"); // poll until the frame contains itawait app.waitFor(() => expect(saved).toBe(true), { timeout: 2000 });Animazioni, indicatori di caricamento e notifiche toast usano timer reali, quindi wait e waitFor sono il modo affidabile per osservarli.
Snapshot
expect(app.lastFrame()).toMatchSnapshot();expect(app.toAnsi()).toMatchSnapshot(); // colors tooGli spazi vuoti a destra e in fondo ai frame vengono rimossi, così gli snapshot non cambiano a causa degli spazi.