|
| 1 | +# 🐆 NitroUI Testing Library |
| 2 | + |
| 3 | +NitroUI Testing Library is a [Testing Library](https://testing-library.com) implementation for the [NitroUI](https://www.npmjs.com/package/nitro-ui-renderer) framework. It renders components with [Test Renderer](https://github.com/mdjastrzebski/test-renderer) and simulates the NitroUI runtime around them, so tests run in Node or Bun without a device or simulator. |
| 4 | + |
| 5 | +The API follows [React Native Testing Library](https://github.com/callstack/react-native-testing-library), so it will feel familiar if you've used RNTL or React Testing Library. NitroUI and this library are both 0.x, so only the latest NitroUI release is supported, currently `nitro-ui-renderer` 0.3.0. |
| 6 | + |
| 7 | +## 📦 Installation |
| 8 | + |
| 9 | +```sh |
| 10 | +bun add --dev nitro-ui-testing-library |
| 11 | +``` |
| 12 | + |
| 13 | +Peer dependencies: `nitro-ui-renderer` (>= 0.3.0) and `react` (^19.3.0, the version NitroUI uses). |
| 14 | + |
| 15 | +## 🏃 Test runners |
| 16 | + |
| 17 | +Vitest, Bun's test runner and Jest are all supported. Add the setup entry for your runner. It mocks NitroUI's native modules, cleans up after each test and adds the custom matchers. |
| 18 | + |
| 19 | +**Vitest** (`vitest.config.ts`): |
| 20 | + |
| 21 | +```ts |
| 22 | +export default defineConfig({ |
| 23 | + test: { |
| 24 | + setupFiles: ['nitro-ui-testing-library/vitest-setup'], |
| 25 | + server: { deps: { inline: [/nitro-ui-/] } }, |
| 26 | + }, |
| 27 | +}) |
| 28 | +``` |
| 29 | + |
| 30 | +**Bun** (`bunfig.toml`): |
| 31 | + |
| 32 | +```toml |
| 33 | +[test] |
| 34 | +preload = ["nitro-ui-testing-library/bun-setup"] |
| 35 | +``` |
| 36 | + |
| 37 | +**Jest** (`jest.config.cjs`): |
| 38 | + |
| 39 | +```js |
| 40 | +module.exports = { |
| 41 | + testEnvironment: 'node', |
| 42 | + setupFiles: ['nitro-ui-testing-library/jest-setup'], |
| 43 | + // NitroUI packages ship TypeScript sources, so compile them too. |
| 44 | + transformIgnorePatterns: [ |
| 45 | + '/node_modules/(?!(\\.(bun|pnpm)/[^/]+/node_modules/)?nitro-ui-[^/]+/)', |
| 46 | + ], |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +See [`examples/basic`](examples/basic) for a complete NitroUI app tested on all three runners. |
| 51 | + |
| 52 | +## ✨ Example |
| 53 | + |
| 54 | +```tsx |
| 55 | +import { render, screen, userEvent } from 'nitro-ui-testing-library' |
| 56 | + |
| 57 | +test('counts taps', async () => { |
| 58 | + await render(<Counter />) |
| 59 | + |
| 60 | + await userEvent.tap(screen.getByText('Increment')) |
| 61 | + await userEvent.tap(screen.getByText('Increment')) |
| 62 | + |
| 63 | + expect(screen.getByText('Count: 2')).toBeMounted() |
| 64 | +}) |
| 65 | +``` |
| 66 | + |
| 67 | +## 🧰 API |
| 68 | + |
| 69 | +- **`render(element, { wrapper?, device? })`**: renders a component (async, so `await` it). Returns the queries plus `rerender`, `unmount`, `toJSON`, `container` and `root`. |
| 70 | +- **`screen`**: the queries for the most recent render. |
| 71 | +- **Queries**: `ByText`, `ByPlaceholderText` and `ByDisplayValue`, each in `getBy`, `getAllBy`, `queryBy`, `queryAllBy`, `findBy` and `findAllBy` variants. Text matches take a string or regex, with `exact` and `normalizer` options. |
| 72 | +- **`within(element)`**: queries scoped to part of the tree. |
| 73 | +- **`userEvent`**: simulates real interactions: `tap(element)`, and `type(element, text)` with `{Backspace}` and `{Enter}` support. Elements blocked by `isUserInteractionEnabled={false}` don't respond. |
| 74 | +- **`device`**: simulates the environment around the app: `setKeyboardInset`, `setSafeAreaInsets`, `setColorScheme` and `setSize`. It resets after each test. |
| 75 | +- **`waitFor(callback, options?)`**: retries until the callback stops throwing. |
| 76 | +- **`act`** and **`cleanup`**: rarely needed directly. Cleanup runs automatically after each test. |
| 77 | +- **Matchers**: `toBeMounted`, `toBeEmptyElement`, `toContainElement`, `toHaveTextContent`, `toHaveDisplayValue`, `toHaveProp`, `toHaveStyle` and `toHaveLayout`. |
| 78 | + |
| 79 | +## 🤝 Contributing |
| 80 | + |
| 81 | +```sh |
| 82 | +bun install |
| 83 | +bun run test # Jest, Vitest and bun test |
| 84 | +``` |
| 85 | + |
| 86 | +See [`AGENTS.md`](AGENTS.md) for more on the repo's setup. |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +MIT |
0 commit comments