टेस्टिंग
@textjs/app/testing से ऐप ऑफ़स्क्रीन रेंडर करें, कुंजियां दबाएं और फ़्रेम पर असर्शन चलाएं।
@textjs/app/testing (या @textjs/react/testing) आपके ऐप को ऑफ़स्क्रीन रेंडर करता है, कुंजियां दबाता है और फ़्रेम को टेक्स्ट के रूप में लौटाता है। टेस्ट ब्राउज़र या 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");});यह async टेस्ट का समर्थन करने वाले किसी भी रनर के साथ काम करता है, जिनमें Bun, Vitest और Jest शामिल हैं।
रेंडरिंग
| मेथड | विवरण |
|---|---|
render(el, { cols, rows, theme }) | ऑफ़स्क्रीन माउंट करना (डिफ़ॉल्ट 80×24) |
lastFrame() | प्लेन टेक्स्ट के रूप में फ़्रेम |
lines() | पंक्तियों के ऐरे के रूप में फ़्रेम |
frames | अब तक का हर अलग फ़्रेम |
toAnsi() | स्नैपशॉट के लिए Truecolor 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फ़्रेम की दाईं ओर और नीचे से खाली जगह हटाई जाती है, इसलिए व्हाइटस्पेस से स्नैपशॉट में बार-बार बदलाव नहीं होते।