الاختبار

اعرض التطبيقات خارج الشاشة، واضغط المفاتيح، وتحقق من الإطارات باستخدام ⁦@textjs/app/testing⁩.

تعرض الحزمة @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