Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci_compatibility-deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
node-version: lts/*

- name: ➕ Actions - Setup Deno
uses: denoland/setup-deno@v1
uses: denoland/setup-deno@v2
with:
deno-version: ${{ matrix.deno-version }}

Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Lojhan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
148 changes: 111 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,90 +1,164 @@
<div align="center">
<img height="180" alt="Poku's Logo" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/poku.svg">

# poku-react-testing

React testing helpers and a Poku plugin for DOM-backed test execution.
Enjoying **Poku**? [Give him a star to show your support](https://github.com/wellwelwel/poku) 🌟

---

📘 [**Documentation**](https://github.com/Lojhan/poku-react-testing#readme)

</div>

---

🧪 [**poku-react-testing**](https://github.com/Lojhan/poku-react-testing) is a **Poku** plugin for React component testing with DOM adapters.

> [!TIP]
>
> Render React components in isolated test files — automatic TSX loader injection, DOM environment setup, and optional render metrics included.

---

## Features
## Quickstart

- Lightweight `render`, `renderHook`, `cleanup`, `screen`, and `fireEvent` helpers.
- Poku plugin that injects TSX loader and DOM setup automatically for `.tsx` and `.jsx` tests.
- DOM adapters:
- `happy-dom` (default)
- `jsdom` (optional)
- custom setup module
- Optional render metrics with configurable reporting.
### Install

## Install
<table>
<tr>
<td width="225">

```bash
npm install --save-dev poku-react-testing poku react react-dom
# Node.js
npm i -D poku-react-testing
```

If you want to run tests with `jsdom`:
</td>
<td width="225">

```bash
npm install --save-dev jsdom
# Bun
bun add -d poku-react-testing
```

## Usage
</td>
<td width="225">

### 1) Configure Poku
```bash
# Deno (optional)
deno add npm:poku-react-testing
```

```ts
</td>
</tr>
</table>

Install a DOM adapter (at least one is required):

<table>
<tr>
<td width="225">

```bash
# happy-dom (recommended)
npm i -D happy-dom \
@happy-dom/global-registrator
```

</td>
<td width="225">

```bash
# jsdom
npm i -D jsdom
```

</td>
</tr>
</table>

### Enable the Plugin

```js
// poku.config.js
import { defineConfig } from 'poku';
import { reactTestingPlugin } from 'poku-react-testing';
import { reactTestingPlugin } from 'poku-react-testing/plugin';

export default defineConfig({
plugins: [
reactTestingPlugin({
dom: 'happy-dom',
domUrl: 'http://localhost:3000/',
metrics: false,
}),
],
});
```

### 2) Write tests
### Write Tests

```tsx
// tests/my-component.test.tsx
import { afterEach, assert, test } from 'poku';
import { cleanup, render, screen } from 'poku-react-testing';

afterEach(cleanup);

test('renders component', () => {
test('renders a heading', () => {
render(<h1>Hello</h1>);
assert.strictEqual(screen.getByRole('heading').textContent, 'Hello');
});
```

## Metrics
---

## Compatibility

Metrics are disabled by default. Enable metrics explicitly:
### Runtime × DOM Adapter

| | Node.js ≥ 20 | Bun ≥ 1 | Deno ≥ 2 |
|---|:---:|:---:|:---:|
| **happy-dom** | ✅ | ✅ | ✅ |
| **jsdom** | ✅ | ✅ | ⚠️ |
| **custom setup** | ✅ | ✅ | ✅ |

> [!NOTE]
>
> `jsdom` under Deno may be unstable depending on Deno's npm compatibility layer for the current `jsdom` version. Use `happy-dom` for Deno environments.

---

## Options

```ts
reactTestingPlugin({
/**
* DOM adapter to use. Defaults to 'happy-dom'.
* - 'happy-dom' — fast, recommended for most tests
* - 'jsdom' — broader browser API coverage
* - { setupModule } — path to a custom DOM setup module
*/
dom: 'happy-dom',

/** Base URL assigned to the DOM environment. */
domUrl: 'http://localhost:3000/',

/**
* Render metrics. Disabled by default.
* Pass `true` for defaults, or an object for fine-grained control.
*/
metrics: {
enabled: true,
topN: 10,
minDurationMs: 1,
topN: 5,
minDurationMs: 0,
reporter(summary) {
console.log(summary.topSlowest);
},
},
});
```

`metrics: true` is shorthand for enabling metrics with default options.

## Build and Validate

```bash
npm run check
npm run build
npm pack --dry-run
```
---

## Release
## License

- Push a tag like `v0.1.0` to trigger publish workflow.
- Set repository secret `NPM_TOKEN` with publish permissions.
[MIT](./LICENSE)
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"test:happy": "cross-env POKU_REACT_TEST_DOM=happy-dom node --import=tsx ./node_modules/poku/lib/bin/index.js --showLogs",
"test:jsdom": "cross-env POKU_REACT_TEST_DOM=jsdom node --import=tsx ./node_modules/poku/lib/bin/index.js --showLogs",
"test:bun": "cross-env POKU_REACT_TEST_DOM=happy-dom bun ./node_modules/poku/lib/bin/index.js --showLogs && cross-env POKU_REACT_TEST_DOM=jsdom bun ./node_modules/poku/lib/bin/index.js --showLogs",
"test:deno": "cross-env POKU_REACT_TEST_DOM=happy-dom deno run -A npm:poku --showLogs && cross-env POKU_REACT_TEST_DOM=jsdom deno run -A npm:poku --showLogs",
"test:deno": "cross-env POKU_REACT_TEST_DOM=happy-dom deno run -A npm:poku --showLogs",
"clean": "rimraf dist",
"build": "tsup src/index.ts src/plugin.ts src/react-testing.ts src/dom-setup-happy.ts src/dom-setup-jsdom.ts --format esm --dts --target node20 --sourcemap --clean --tsconfig tsconfig.tsup.json",
"typecheck": "tsc -p tsconfig.build.json --noEmit",
Expand All @@ -52,7 +52,7 @@
"jsdom"
],
"author": "",
"license": "ISC",
"license": "MIT",
"publishConfig": {
"access": "public",
"provenance": true
Expand Down
13 changes: 13 additions & 0 deletions src/dom-setup-happy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GlobalRegistrator } from '@happy-dom/global-registrator';

declare const Deno: unknown;

type ReactActGlobal = typeof globalThis & {
IS_REACT_ACT_ENVIRONMENT?: boolean;
};
Expand All @@ -10,9 +12,20 @@ const defaultUrl = 'http://localhost:3000/';
const configuredUrl = process.env.POKU_REACT_DOM_URL || defaultUrl;

if (!globalThis.window || !globalThis.document) {
const isDenoRuntime = typeof Deno !== 'undefined';
const nativeEvent = isDenoRuntime ? globalThis.Event : undefined;
const nativeDispatchEvent = isDenoRuntime
? globalThis.dispatchEvent?.bind(globalThis)
: undefined;

GlobalRegistrator.register({
url: configuredUrl,
});

if (isDenoRuntime) {
if (nativeEvent) (globalThis as unknown as Record<string, unknown>).Event = nativeEvent;
if (nativeDispatchEvent) globalThis.dispatchEvent = nativeDispatchEvent;
}
}

if (typeof reactGlobal.IS_REACT_ACT_ENVIRONMENT === 'undefined') {
Expand Down
Loading
Loading