Guides
Testing
Test Gridland components with @gridland/testing
Installation
bun add -D @gridland/testingnpm install --save-dev @gridland/testingyarn add -D @gridland/testingpnpm add -D @gridland/testingCreating a Test Screen
The createScreen function renders your component to a virtual buffer that you
can inspect:
import { createScreen } from "@gridland/testing"
const screen = await createScreen(
<box border padding={1}>
<text>Hello World</text>
</box>,
{ cols: 40, rows: 10 }
)Querying Content
Use screen.getByText() to find text in the rendered output:
const match = screen.getByText("Hello World")
expect(match).toBeTruthy()
expect(match.row).toBeGreaterThan(0)Simulating Input
Send keystrokes to test interactive components:
import { createScreen, keys } from "@gridland/testing"
const screen = await createScreen(<MyInputComponent />, {
cols: 40,
rows: 10,
})
screen.sendKeys(keys.type("hello"))
screen.sendKeys(keys.enter)Available Key Helpers
| Helper | Description |
|---|---|
keys.type(str) | Type a string character by character |
keys.enter | Press Enter |
keys.escape | Press Escape |
keys.tab | Press Tab |
keys.backspace | Press Backspace |
keys.up / keys.down | Arrow keys |
keys.left / keys.right | Arrow keys |
Waiting for Updates
Use waitFor to wait for async state changes:
import { createScreen, waitFor } from "@gridland/testing"
const screen = await createScreen(<AsyncComponent />)
await waitFor(() => {
expect(screen.getByText("Loaded")).toBeTruthy()
})Snapshot Testing
Capture the rendered buffer as a string for snapshot comparisons:
const output = screen.toString()
expect(output).toMatchSnapshot()Test Runner Compatibility
@gridland/testing works with any test runner that supports ESM:
- Bun - Recommended, used in the Gridland test suite
- Vitest - Works with proper module resolution
- Jest - Works with ESM transforms configured
Bun is the recommended test runner for Gridland projects. It has native ESM support and is used for the Gridland test suite itself.
Tips
- Set explicit
colsandrowsfor deterministic layout - Run tests multiple times with randomized order to catch state leaks
- Use
screen.debug()to print the buffer to the console during development