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

274 lines
19 KiB
Plaintext

page wrnexustest {
seo {
title = "@wrnexus/test"
description = "WRNexusJS-aware component, route, and browser testing utilities."
}
view {
<div class="docs-shell">
<a href="#main" class="skip-link">Skip to content</a>
<header class="topbar">
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
<nav aria-label="Primary"><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="https://component.wrnexusjs.dev/">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
<div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.8.4</a><button data-wire-theme-toggle class="theme-button" aria-label="Toggle color theme" title="Toggle color theme">◐</button></div>
</header>
<div class="mobile-doc-nav"><details><summary>Browse documentation</summary><nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="https://component.wrnexusjs.dev/">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a><a href="/tutorial">Tutorial</a><a href="/guides/project-structure">Guides</a><a href="/examples">Examples</a><a href="/search">Search</a></nav></details></div>
<main class="portal-main docs-layout">
<article id="main" class="documentation prose standalone package-document"><nav class="breadcrumbs" aria-label="Breadcrumb"><a href="/">Home</a><span>/</span><a href="/packages">Packages</a><span>/</span><span aria-current="page">@wrnexus/test</span></nav><section class="doc-intro"><span class="eyebrow">Tooling · Package reference</span><h1>@wrnexus/test</h1><p>WRNexusJS-aware component, route, and browser testing utilities.</p><div class="doc-meta"><span>v0.8.4</span><span>Private registry</span><span>Tooling</span></div><section id="access" class="access-callout"><h2>Install the package</h2><p>After WorkRoot approves private registry access, install the release-aligned package:</p><pre><code>bun add @wrnexus/test@0.8.4</code><button type="button" class="copy-button" aria-label="Copy installation command">Copy</button></pre><p><a href="/access">Request preview access</a>. Never put registry tokens in source control.</p></section></section><section id="guide"><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>
<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>
<p>The CLI supports focused suites by file or directory convention:</p>
<pre data-language="bash"><code>wrnexus test unit # *.unit.test.ts or test/unit/**
wrnexus test component # *.component.test.ts or test/component/**
wrnexus test api # *.api.test.ts or test/api/**
wrnexus test accessibility # *.a11y.test.ts / *.accessibility.test.ts
wrnexus test performance # *.performance.test.ts / *.benchmark.test.ts
wrnexus test browser # Playwright project when configured
wrnexus test visual # Playwright tests tagged @visual</code></pre>
<p>Pass the application directory after the level, for example <code>wrnexus test component examples/basic-app</code>. A focused command fails clearly when no matching suite exists instead of silently running unrelated tests.</p>
<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="api"><h2>Complete TypeScript API</h2><p>Generated from the exact installed package declarations.</p><pre data-language="typescript"><code>import &#123; ProblemDetails, 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';
interface TestRequestOptions extends Omit&lt;RequestInit, &quot;body&quot;&gt; &#123;
body?: BodyInit | Record&lt;string, unknown&gt; | URLSearchParams | FormData | null;
baseUrl?: string;
&#125;
/** Build a web-standard Request with convenient JSON/FormData handling. */
declare function testRequest(path?: string, options?: TestRequestOptions): Request;
/** Create a complete Context suitable for middleware and route unit tests. */
declare function testContext(path?: string, options?: TestRequestOptions): Context;
interface JsonResponse&lt;T&gt; &#123;
response: Response;
body: T;
&#125;
declare function readJsonResponse&lt;T = unknown&gt;(response: Response): Promise&lt;JsonResponse&lt;T&gt;&gt;;
declare function expectProblem(response: Response, status?: number): Promise&lt;ProblemDetails&gt;;
interface Deferred&lt;T&gt; &#123;
promise: Promise&lt;T&gt;;
resolve(value: T | PromiseLike&lt;T&gt;): void;
reject(reason?: unknown): void;
&#125;
declare function deferred&lt;T&gt;(): Deferred&lt;T&gt;;
interface WaitForOptions &#123;
timeoutMs?: number;
intervalMs?: number;
signal?: AbortSignal;
&#125;
/** Poll a condition without depending on fake timers or a browser runtime. */
declare function waitFor(condition: () =&gt; boolean | Promise&lt;boolean&gt;, options?: WaitForOptions): Promise&lt;void&gt;;
declare class MemoryCookieJar &#123;
#private;
apply(response: Response): void;
header(): string;
request(path: string, options?: TestRequestOptions): Request;
clear(): void;
&#125;
interface TransactionalDatabase &#123;
tx&lt;T&gt;(callback: (transaction: TransactionalDatabase) =&gt; Promise&lt;T&gt;): Promise&lt;T&gt;;
&#125;
/** Run test work in a real transaction and always force rollback. */
declare function withDatabaseRollback&lt;T&gt;(db: TransactionalDatabase, run: (transaction: TransactionalDatabase) =&gt; T | Promise&lt;T&gt;): Promise&lt;T&gt;;
declare function createFactory&lt;T extends Record&lt;string, unknown&gt;&gt;(build: (sequence: number) =&gt; T): &#123;
build(overrides?: Partial&lt;T&gt;): T;
buildMany(count: number, overrides?: Partial&lt;T&gt;): T[];
reset(): void;
&#125;;
interface BrowserArtifactPage &#123;
screenshot(options: &#123;
path: string;
fullPage?: boolean;
&#125;): Promise&lt;unknown&gt;;
context(): &#123;
tracing?: &#123;
stop(options: &#123;
path: string;
&#125;): Promise&lt;unknown&gt;;
&#125;;
&#125;;
&#125;
declare function captureBrowserArtifacts(page: BrowserArtifactPage, testName: string, options?: &#123;
root?: string;
screenshot?: boolean;
trace?: boolean;
&#125;): Promise&lt;&#123;
screenshot?: string;
trace?: string;
&#125;&gt;;
/**
* @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 BrowserArtifactPage, type Deferred, type Harness, type HarnessOptions, type JsonResponse, MemoryCookieJar, type TestRequestOptions, type TransactionalDatabase, type WaitForOptions, callRoute, captureBrowserArtifacts, createFactory, createHarness, deferred, expectProblem, mountHtml, readJsonResponse, renderComponent, testContext, testRequest, waitFor, withDatabaseRollback &#125;;
</code></pre></section><section id="examples" class="examples"><h2>Examples</h2><p>Copy-ready examples from the installed package documentation.</p><div class="example-grid"><article class="example-card"><h3>The CLI supports focused suites by file or directory convention</h3><pre data-language="bash"><code>wrnexus test unit # *.unit.test.ts or test/unit/**
wrnexus test component # *.component.test.ts or test/component/**
wrnexus test api # *.api.test.ts or test/api/**
wrnexus test accessibility # *.a11y.test.ts / *.accessibility.test.ts
wrnexus test performance # *.performance.test.ts / *.benchmark.test.ts
wrnexus test browser # Playwright project when configured
wrnexus test visual # Playwright tests tagged @visual</code></pre></article><article class="example-card"><h3>suite exists instead of silently running unrelated tests.</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></article><article class="example-card"><h3>Calling an API route handler directly</h3><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></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="#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><div class="footer-brand"><span class="footer-mark" aria-hidden="true">W</span><p><strong>WRNexusJS 0.8.4</strong><span>Complete API documentation generated from installed package declarations.</span></p></div><nav aria-label="Footer"><a href="/packages">All packages</a><a href="/getting-started">Get started</a><a href="/security">Security</a><a href="/support">Support</a><a href="/llms.txt">AI guide</a></nav><p class="footer-meta">Private Developer Preview · Bun-native</p></footer>
</div>
}
}