Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
"hooks": {
"before:init": "bun run validate",
"after:bump": "bun run build",
"after:release": "echo Successfully released ${name} v${version} to ${repo.repository}."
"after:release": "echo Successfully released v${version} to ${repo.repository}."
},
"git": {
"commitMessage": "chore: release v${version}",
"tagName": "v${version}"
},
"npm": {
"publish": true
"publish": false
},
"github": {
"release": true,
"releaseName": "v${version}"
},
"plugins": {
"./scripts/release-packages.js": {},
"@release-it/conventional-changelog": {
"preset": {
"name": "conventionalcommits",
Expand Down
58 changes: 44 additions & 14 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ Native). It renders components with
[test-renderer](https://github.com/mdjastrzebski/test-renderer) and supports Jest,
Vitest and Bun's test runner.

## Layout

The repo is a Bun workspace monorepo. More packages are coming.

- `packages/*`: the published packages, all released together with one
version. Each has its own `package.json`, build, typecheck and tests.
Folder names drop the `nitro-ui-` prefix of the npm name.
- `packages/testing-library`: the library. Paths in the sections
on tests and the library below (`src/`, `test/`, runner configs) are
relative to it.
- `examples/*`: private apps that use the built packages like an app would.
- The root is private. It holds the shared tooling (oxfmt, release-it, CI,
`scripts/`) and the release version, and its scripts run each task across
the packages with `bun run --filter`.

## Status

This repository is being ported, step by step, from the proof of concept at
Expand All @@ -21,8 +36,8 @@ reviewed steps, rather than copying it wholesale.

- **NitroUI:** only the latest `nitro-ui-renderer` release (currently 0.3.0).
NitroUI and this library are both 0.x and change quickly, so raise the
minimum supported version (the `peerDependencies` range and the exact
devDependency) whenever we move to a newer NitroUI, and re-create
minimum supported version (the package's `peerDependencies` range and
exact devDependency) whenever we move to a newer NitroUI, and re-create
`examples/basic` from the new template. Don't keep compatibility
code for older NitroUI versions.
- **React:** pin the devDependency to the exact version `nitro-ui-renderer`
Expand All @@ -37,24 +52,38 @@ reviewed steps, rather than copying it wholesale.
This repo uses [Bun](https://bun.com) as its package manager, like the rest of
the NitroUI ecosystem.

- Install dependencies: `bun install`. `bunfig.toml` sets the isolated linker,
so code can only import dependencies declared in `package.json`. Don't switch
it to hoisted to work around a missing dependency: declare the dependency.
Run these from the root, where they cover every package. A package's own
scripts (`build`, `typecheck`, `test`, `test:jest`, `test:vitest`,
`test:bun`) work the same from its directory.

- Install dependencies: `bun install`. The root `bunfig.toml` sets the
isolated linker, so code can only import dependencies declared in its own
package's `package.json`. Don't switch it to hoisted to work around a
missing dependency: declare the dependency. The root only declares the
shared tooling; build and test tools belong to each package.
- Typecheck: `bun run typecheck`
- Build: `bun run build` runs [tsdown](https://tsdown.dev) (`tsdown.config.ts`),
which writes ESM and CommonJS (for Jest) with type declarations to `dist/`.
The package exports point at `dist/`, so build before running the example
app (`bun run test:examples` does).
- Build: `bun run build` runs each package's build. The library uses
[tsdown](https://tsdown.dev) (`tsdown.config.ts`), which writes ESM and
CommonJS (for Jest) with type declarations to `dist/`. The package exports
point at `dist/`, so build before running the example app
(`bun run test:examples` does).
- Format with [oxfmt](https://oxc.rs/docs/guide/usage/formatter): `bun run format`
(check only: `bun run format:check`). Run it before committing.
- Run tests: `bun run test` runs Jest, then Vitest, then `bun test`. To run one
runner, use `bun run test:jest`, `bun run test:vitest` or `bun run test:bun`.
Plain `bun test` runs only Bun's runner.
Plain `bun test` works only in a package's directory, where its
`bunfig.toml` sets the preload.
- Run every check (format, typecheck, tests, example app): `bun run validate`.
- Release with `bun run release` ([release-it](https://github.com/release-it/release-it),
`.release-it.json`): it runs `validate`, bumps the version, builds, writes
the changelog from Conventional Commits, tags, publishes to npm and creates
the GitHub release.
the changelog from Conventional Commits, publishes to npm, tags and creates
the GitHub release. Every public package in `packages/` is released with
the same version, kept in the root `package.json`:
`scripts/release-packages.js`, a local release-it plugin, copies it to each
package, updates `bun.lock` and publishes them with `bun publish` (which
replaces `workspace:` ranges with real versions). New packages need no
release setup, but must start at the current version. Try it with
`bun run release --dry-run`.
- CI runs on GitHub Actions. The test jobs live in the reusable
`.github/workflows/test.yml`, one job each for typecheck, `bun test`, and
Jest and Vitest on Node 24.
Expand All @@ -65,7 +94,8 @@ the NitroUI ecosystem.
so a new release is caught even when nothing is pushed.
- To try a specific NitroUI version locally, run
`NITRO_UI_VERSION=x.y.z ./scripts/use-latest-nitro-ui.sh`, then revert
`package.json` and `bun.lock`.
the `package.json` files and `bun.lock`. Add new packages that depend on
NitroUI to the script.

## Example app

Expand All @@ -80,7 +110,7 @@ NitroUI.
- Re-create it from the template whenever the NitroUI template changes,
usually when raising the NitroUI version. The steps and the deliberate
changes from the template are in `examples/basic/README.md`.
- Its NitroUI packages and React must match the root's exact versions: the
- Its NitroUI packages and React must match the library's exact versions: the
library and the app share one `nitro-ui-renderer` and one React only when
the versions are the same. `scripts/use-latest-nitro-ui.sh` updates both.
- The template's `App` isn't tested, because `nitro-ui-navigation` isn't
Expand Down
118 changes: 7 additions & 111 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,125 +1,21 @@
<h1>NitroUI Testing Library<br><big>🐆</big></h1>

NitroUI Testing Library is a [Testing Library](https://testing-library.com) implementation for the [NitroUI](https://nitro-ui.com/) 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.
[Testing Library](https://testing-library.com) for the [NitroUI](https://nitro-ui.com/) framework. Tests render components with [Test Renderer](https://github.com/mdjastrzebski/test-renderer) and run in Node or Bun without a device or simulator, on Jest, Vitest or Bun test.

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.
## 📦 Packages

## 📦 Installation

```sh
bun add --dev nitro-ui-testing-library
```

Peer dependencies: `nitro-ui-renderer` (>= 0.3.0) and `react` (^19.3.0, the version NitroUI uses).

## 🏃 Test runners

Vitest, Bun test 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.

**Vitest** (`vitest.config.ts`):

```ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
// Lets tests use `test` and `expect` without importing them.
globals: true,
setupFiles: ['nitro-ui-testing-library/vitest-setup'],
// NitroUI packages ship TypeScript sources, so compile them too.
server: { deps: { inline: [/nitro-ui-/] } },
},
})
```

**Bun test** (`bunfig.toml`):

```toml
[test]
preload = ["nitro-ui-testing-library/bun-setup"]
```

**Jest** (`jest.config.cjs`):

```js
module.exports = {
testEnvironment: 'node',
setupFiles: ['nitro-ui-testing-library/jest-setup'],
// NitroUI packages ship TypeScript sources, so compile them too.
transformIgnorePatterns: [
'/node_modules/(?!(\\.(bun|pnpm)/[^/]+/node_modules/)?nitro-ui-[^/]+/)',
],
}
```

Jest compiles TypeScript and JSX with Babel:

```sh
bun add --dev jest babel-jest @babel/core @babel/preset-env @babel/preset-typescript @babel/preset-react @babel/plugin-syntax-decorators
```

```js
// babel.config.cjs
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
['@babel/preset-react', { runtime: 'automatic' }],
],
// NitroUI's native module specs use decorators.
plugins: [['@babel/plugin-syntax-decorators', { version: '2023-11' }]],
}
```

**TypeScript**: the NitroUI template's `tsconfig.json` loads no global types, so add your runner's test types. On Vitest and Bun, also add the setup entry, which adds the matcher types (on Jest, importing the library adds them):

```jsonc
{
"extends": "nitro-ui-cli/tsconfig.json",
"compilerOptions": {
// Jest: ["jest"] (with @types/jest)
// Vitest: ["vitest/globals", "nitro-ui-testing-library/vitest-setup"]
// Bun: ["bun", "bun-types/test-globals", "nitro-ui-testing-library/bun-setup"] (with @types/bun)
"types": ["vitest/globals", "nitro-ui-testing-library/vitest-setup"],
},
"include": ["src"],
}
```
| Package | Description |
| ------------------------------------------------------ | ------------------------------------ |
| [`nitro-ui-testing-library`](packages/testing-library) | Render, query and interact with apps |

See [`examples/basic`](examples/basic) for a complete NitroUI app tested on all three runners.

## ✨ Example

```tsx
import { render, screen, userEvent } from 'nitro-ui-testing-library'

test('counts taps', async () => {
await render(<Counter />)

await userEvent.tap(screen.getByText('Increment'))
await userEvent.tap(screen.getByText('Increment'))

expect(screen.getByText('Count: 2')).toBeMounted()
})
```

## 🧰 API

- **`render(element, { wrapper?, device? })`**: renders a component (async, so `await` it). Returns the queries plus `rerender`, `unmount`, `toJSON`, `container` and `root`.
- **`screen`**: the queries for the most recent render.
- **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.
- **`within(element)`**: queries scoped to part of the tree.
- **`userEvent`**: simulates real interactions: `tap(element)`, and `type(element, text)` with `{Backspace}` and `{Enter}` support. Elements blocked by `isUserInteractionEnabled={false}` don't respond.
- **`device`**: simulates the environment around the app: `setKeyboardInset`, `setSafeAreaInsets`, `setColorScheme` and `setSize`. It resets after each test.
- **`waitFor(callback, options?)`**: retries until the callback stops throwing.
- **`act`** and **`cleanup`**: rarely needed directly. Cleanup runs automatically after each test.
- **Matchers**: `toBeMounted`, `toBeEmptyElement`, `toContainElement`, `toHaveTextContent`, `toHaveDisplayValue`, `toHaveProp`, `toHaveStyle` and `toHaveLayout`.

## 🤝 Contributing

```sh
bun install
bun run test # Jest, Vitest and bun test
bun run test # Jest, Vitest and bun test, in every package
bun run validate # format, typecheck, tests and the example app
```

See [`AGENTS.md`](AGENTS.md) for more on the repo's setup.
Expand Down
Loading