Files
WRNexusJSDoc/app/pages/packages/test.wrn
T

177 lines
13 KiB
Plaintext

page wrnexustest {
seo {
title = "@wrnexus/test"
description = "WRNexusJS-aware component, route, and browser testing utilities."
}
view {
<div class="docs-shell">
<header class="topbar">
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
<nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
<button data-wire-theme-toggle class="theme-button" aria-label="Toggle theme">Theme</button>
</header>
<main class="page package-page">
<aside class="sidebar"><a href="/packages">← All packages</a><span class="category">Tooling</span><h1>@wrnexus/test</h1><p>WRNexusJS-aware component, route, and browser testing utilities.</p><code>bun add @wrnexus/test@0.2.14</code><nav><a href="#guide">Guide</a><a href="#api">Complete API</a></nav></aside>
<article class="documentation"><section class="doc-intro"><span class="eyebrow">Tooling</span><h1>@wrnexus/test</h1><p>WRNexusJS-aware component, route, and browser testing utilities.</p><pre><code>bun add @wrnexus/test@0.2.14</code></pre></section><section id="guide" class="prose"><blockquote>Testing utilities for WRNexusJS apps — component rendering, reactive-DOM mounting, route handler calls, and a full in-process app harness, plus a one-import re-export of <code>bun:test</code>.</blockquote>
<p>Part of the <strong>WRNexusJS</strong> framework — an SSR-first, Bun-native full-stack web framework.</p>
<h3 id="overview">Overview</h3>
<p><code>@wrnexus/test</code> is the server-side test toolkit you reach for when writing tests for a WRNexusJS app. It runs under <code>bun test</code> (invoked via <code>wrnexus test</code>) and gives you a single import surface: the <code>bun:test</code> primitives (<code>test</code>, <code>expect</code>, <code>mock</code>, …) re-exported alongside WRNexusJS-aware helpers that compile <code>.wrn</code> components, hydrate server HTML in a DOM, invoke API route handlers, and boot the real app on an ephemeral port for integration tests.</p>
<h3 id="installation">Installation</h3>
<pre data-language="bash"><code>bun add @wrnexus/test</code></pre>
<blockquote>Private package — the machine must be authenticated to the <code>wrnexus</code> npm org</blockquote>
<blockquote>(a read token in <code>~/.npmrc</code>). Requires <strong>Bun</strong> (Node is not supported).</blockquote>
<h3 id="api">API</h3>
<h4 id="re-exported-test-primitives">Re-exported test primitives</h4>
<p>For one-import DX, the following are re-exported straight from <code>bun:test</code>:</p>
<p><code>test</code>, <code>expect</code>, <code>describe</code>, <code>it</code>, <code>beforeEach</code>, <code>afterEach</code>, <code>beforeAll</code>, <code>afterAll</code>, <code>mock</code>, <code>spyOn</code>.</p>
<p><code>createContext</code> is also re-exported from <code>@wrnexus/core</code>.</p>
<h4 id="rendercomponent-source-props"><code>renderComponent(source, props?)</code></h4>
<pre data-language="ts"><code>function renderComponent(source: string, props?: Record&lt;string, unknown&gt;): Promise&lt;string&gt;;</code></pre>
<p>Compiles a <code>.wrn</code> component <code>source</code> string (via <code>@wrnexus/compiler</code>) and renders it to an HTML string with the given <code>props</code>. Throws if the compiled module has no <code>render</code> export.</p>
<h4 id="mounthtml-html"><code>mountHtml(html)</code></h4>
<pre data-language="ts"><code>function mountHtml(html: string): &#123;
document: Document;
window: unknown;
querySelector: (sel: string) =&gt; Element | null;
querySelectorAll: (sel: string) =&gt; Element[];
&#125;;</code></pre>
<p>Mounts server-rendered <code>html</code> in a <code>happy-dom</code> window with the reactive runtime hydrated, so you can test <code>data-scope</code> / <code>data-text</code> / <code>data-for</code> / <code>data-show</code> behaviour. Returns the window plus <code>document</code> and query helpers; assert on those.</p>
<blockquote><code>happy-dom</code> is loaded lazily (via <code>require</code>), so importing this package never</blockquote>
<blockquote>requires it unless you actually call <code>mountHtml</code>.</blockquote>
<h4 id="callroute-handler-request"><code>callRoute(handler, request)</code></h4>
<pre data-language="ts"><code>function callRoute(
handler: (ctx: Context) =&gt; Response | Promise&lt;Response&gt;,
request: Request,
): Promise&lt;Response&gt;;</code></pre>
<p>Calls an API route <code>handler</code> with a <code>Context</code> built from a <code>Request</code> (using <code>createContext</code>). Returns the handler's <code>Response</code>.</p>
<h4 id="createharness-projectroot-options"><code>createHarness(projectRoot, options?)</code></h4>
<pre data-language="ts"><code>function createHarness(projectRoot: string, options?: HarnessOptions): Promise&lt;Harness&gt;;
interface HarnessOptions &#123;
/** Config/env profile. Default &quot;test&quot;. */
profile?: string;
&#125;
interface Harness &#123;
/** Base URL of the ephemeral test server. */
url: string;
/** Fetch a path on the app (relative to `url`). */
fetch(path: string, init?: RequestInit): Promise&lt;Response&gt;;
/** The scanned router (pages/api/realtime/components). */
router: unknown;
/** Stop the server. */
close(): void;
&#125;</code></pre>
<p>Boots the app at <code>projectRoot</code> on an ephemeral port (<code>port: 0</code>) for integration tests covering pages, API routes, middleware, and the full request pipeline. Loads env and app config for the given <code>profile</code> (default <code>&quot;test&quot;</code>) so it picks up your test database/env. The server runs in <code>development</code> mode with HMR disabled. Remember to <code>await app.close()</code> when done.</p>
<h3 id="usage">Usage</h3>
<pre data-language="ts"><code>import &#123; test, expect, renderComponent, mountHtml, createHarness &#125; from &quot;@wrnexus/test&quot;;
test(&quot;counter renders its label&quot;, async () =&gt; &#123;
const html = await renderComponent(SRC, &#123; start: 3, label: &quot;Hits&quot; &#125;);
expect(html).toContain(&quot;Hits&quot;);
&#125;);
test(&quot;reactive scope hydrates&quot;, () =&gt; &#123;
const &#123; querySelector &#125; = mountHtml(serverHtml);
expect(querySelector(&quot;[data-text]&quot;)?.textContent).toBe(&quot;3&quot;);
&#125;);
test(&quot;home page responds&quot;, async () =&gt; &#123;
const app = await createHarness(&quot;examples/basic-app&quot;);
const res = await app.fetch(&quot;/&quot;);
expect(res.status).toBe(200);
await app.close();
&#125;);</code></pre>
<p>Calling an API route handler directly:</p>
<pre data-language="ts"><code>import &#123; test, expect, callRoute &#125; from &quot;@wrnexus/test&quot;;
import &#123; GET &#125; from &quot;../app/api/health.ts&quot;;
test(&quot;health endpoint&quot;, async () =&gt; &#123;
const res = await callRoute(GET, new Request(&quot;http://test/api/health&quot;));
expect(res.status).toBe(200);
&#125;);</code></pre>
<h3 id="requirements-notes">Requirements / Notes</h3>
<ul>
<li><strong>Bun-only.</strong> Runs under <code>bun test</code> (via <code>wrnexus test</code>); uses Bun's module</li>
<p>loading and the <code>bun:test</code> runtime.</p>
<li><code>mountHtml</code> requires <strong><code>happy-dom</code></strong> to be available in the workspace (loaded</li>
<p>lazily; it's a dev dependency, not a runtime dependency of this package).</p>
<li>Works with the rest of the WRNexusJS toolchain:</li>
<p>[<code>@wrnexus/compiler</code>](../compiler) (compiles <code>.wrn</code> sources), [<code>@wrnexus/core</code>](../core) (<code>Context</code> / <code>createContext</code>), [<code>@wrnexus/csr</code>](../csr) (reactive runtime for <code>mountHtml</code>), [<code>@wrnexus/dev-server</code>](../dev-server) (<code>startServer</code> behind <code>createHarness</code>), and [<code>@wrnexus/styles</code>](../styles) (config/env/profile loading for the harness).</p>
</ul></section><section id="api" class="prose api"><h2>Complete TypeScript API</h2><p>This declaration is generated from the exact published package and lists its exported functions, classes, interfaces, and types.</p><pre data-language="typescript"><code>import &#123; Context &#125; from '@wrnexus/core';
export &#123; createContext &#125; from '@wrnexus/core';
export &#123; afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, mock, spyOn, test &#125; from 'bun:test';
/**
* @wrnexus/test — testing utilities for WRNexusJS apps. Runs on `bun test` (via
* `wrnexus test`). Import everything from one place:
*
* import &#123; test, expect, renderComponent, mountHtml, createHarness &#125; from &quot;@wrnexus/test&quot;;
*
* test(&quot;counter renders its label&quot;, async () =&gt; &#123;
* const html = await renderComponent(SRC, &#123; start: 3, label: &quot;Hits&quot; &#125;);
* expect(html).toContain(&quot;Hits&quot;);
* &#125;);
*
* test(&quot;home page responds&quot;, async () =&gt; &#123;
* const app = await createHarness(&quot;examples/basic-app&quot;);
* const res = await app.fetch(&quot;/&quot;);
* expect(res.status).toBe(200);
* await app.close();
* &#125;);
*/
/** Compile a `.wrn` component source + render it to HTML with the given props. */
declare function renderComponent(source: string, props?: Record&lt;string, unknown&gt;): Promise&lt;string&gt;;
/**
* Mount server-rendered HTML in a happy-dom window with the reactive runtime
* hydrated, so you can test `data-scope`/`data-text`/`data-for`/`data-show`
* behaviour. Returns the window; assert on `win.document`.
*/
declare function mountHtml(html: string): &#123;
document: Document;
window: unknown;
querySelector: (sel: string) =&gt; Element | null;
querySelectorAll: (sel: string) =&gt; Element[];
&#125;;
/** Call an API route handler with a `Context` built from a Request. */
declare function callRoute(handler: (ctx: Context) =&gt; Response | Promise&lt;Response&gt;, request: Request): Promise&lt;Response&gt;;
interface Harness &#123;
/** Base URL of the ephemeral test server. */
url: string;
/** Fetch a path on the app (relative to `url`). */
fetch(path: string, init?: RequestInit): Promise&lt;Response&gt;;
/** The scanned router (pages/api/realtime/components). */
router: unknown;
/** Stop the server. */
close(): void;
&#125;
interface HarnessOptions &#123;
/** Config/env profile. Default &quot;test&quot;. */
profile?: string;
&#125;
/**
* Boot the app on an ephemeral port for integration tests (pages, API routes,
* middleware, the full pipeline). Uses the &quot;test&quot; profile by default so it picks
* up your test database/env. Remember to `await app.close()`.
*/
declare function createHarness(projectRoot: string, options?: HarnessOptions): Promise&lt;Harness&gt;;
export &#123; type Harness, type HarnessOptions, callRoute, createHarness, mountHtml, renderComponent &#125;;
</code></pre></section><section id="examples" class="prose examples"><h2>Examples</h2><p>Copy-ready examples taken from this package's published documentation.</p><div class="example-grid"><article class="example-card"><h3>Example 1</h3><pre data-language="bash"><code>bun add @wrnexus/test</code></pre></article><article class="example-card"><h3>Example 2</h3><pre data-language="ts"><code>function renderComponent(source: string, props?: Record&lt;string, unknown&gt;): Promise&lt;string&gt;;</code></pre></article><article class="example-card"><h3>Example 3</h3><pre data-language="ts"><code>function mountHtml(html: string): &#123;
document: Document;
window: unknown;
querySelector: (sel: string) =&gt; Element | null;
querySelectorAll: (sel: string) =&gt; Element[];
&#125;;</code></pre></article><article class="example-card"><h3>Example 4</h3><pre data-language="ts"><code>function callRoute(
handler: (ctx: Context) =&gt; Response | Promise&lt;Response&gt;,
request: Request,
): Promise&lt;Response&gt;;</code></pre></article></div></section></article>
<aside class="on-this-page"><h2>On this page</h2><nav><a class="toc-level-2" href="#guide">Guide</a><a class="toc-level-3" href="#overview">Overview</a><a class="toc-level-3" href="#installation">Installation</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#re-exported-test-primitives">Re-exported test primitives</a><a class="toc-level-4" href="#rendercomponent-source-props">renderComponent(source, props?)</a><a class="toc-level-4" href="#mounthtml-html">mountHtml(html)</a><a class="toc-level-4" href="#callroute-handler-request">callRoute(handler, request)</a><a class="toc-level-4" href="#createharness-projectroot-options">createHarness(projectRoot, options?)</a><a class="toc-level-3" href="#usage">Usage</a><a class="toc-level-3" href="#requirements-notes">Requirements / Notes</a><a class="toc-level-2" href="#api">Complete API</a><a class="toc-level-2" href="#examples">Examples</a></nav></aside>
</main>
<footer>WRNexusJS 0.2.14 · SSR-first · Bun-native · Documentation generated from published package APIs.</footer>
</div>
}
}