آزمون

با ⁦@textjs/app/testing⁩ برنامه‌ها را خارج از صفحه رندر کنید، کلیدها را فشار دهید و فریم‌ها را با assertion بررسی کنید.

ماژول @textjs/app/testing (یا @textjs/react/testing) برنامهٔ شما را خارج از صفحه رندر می‌کند، کلیدها را فشار می‌دهد و فریم را به‌صورت متن برمی‌گرداند. تست‌ها بدون مرورگر یا DevTools اجرا می‌شوند.

counter.test.tsxtsx
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");});

این ماژول با هر اجراکننده‌ای که از تست‌های async پشتیبانی کند، از جمله Bun، Vitest و Jest، کار می‌کند.

رندر

متدتوضیحات
render(el, { cols, rows, theme })نصب خارج از صفحه (پیش‌فرض ⁦80×24⁩)
lastFrame()فریم به‌صورت متن ساده
lines()فریم به‌صورت آرایه‌ای از سطرها
framesهمهٔ فریم‌های متمایز تا این لحظه
toAnsi()ANSI با رنگ واقعی، برای اسنپ‌شات‌ها
cell(x, y){ char, fg, bg, attrs }
locate(text)مختصات { x, y } اولین تطابق
rerender(el) / unmount()به‌روزرسانی یا برچیدن

تابع renderToString(el, options) یک بار رندر می‌کند و متن را برمی‌گرداند.

ورودی

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);

هر فراخوانی درون act() اجرا می‌شود و فریم‌های حاصل را تخلیه می‌کند، بنابراین بررسی بعدی به‌روزرسانی را می‌بیند.

زمان

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 });

انیمیشن‌ها، نشانگرهای چرخان و اعلان‌های گذرا از زمان‌سنج‌های واقعی استفاده می‌کنند، بنابراین توابع wait و waitFor راه مطمئن مشاهدهٔ آن‌ها هستند.

اسنپ‌شات‌ها

expect(app.lastFrame()).toMatchSnapshot();expect(app.toAnsi()).toMatchSnapshot(); // colors too

فضای خالی سمت راست و پایین فریم‌ها حذف می‌شود تا اسنپ‌شات‌ها به‌دلیل فضای خالی مدام تغییر نکنند.

ویرایش این صفحه در GitHub