177 lines
13 KiB
Plaintext
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<string, unknown>): Promise<string>;</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): {
|
|
document: Document;
|
|
window: unknown;
|
|
querySelector: (sel: string) => Element | null;
|
|
querySelectorAll: (sel: string) => Element[];
|
|
};</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) => Response | Promise<Response>,
|
|
request: Request,
|
|
): Promise<Response>;</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<Harness>;
|
|
|
|
interface HarnessOptions {
|
|
/** Config/env profile. Default "test". */
|
|
profile?: string;
|
|
}
|
|
|
|
interface Harness {
|
|
/** Base URL of the ephemeral test server. */
|
|
url: string;
|
|
/** Fetch a path on the app (relative to `url`). */
|
|
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
/** The scanned router (pages/api/realtime/components). */
|
|
router: unknown;
|
|
/** Stop the server. */
|
|
close(): void;
|
|
}</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>"test"</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 { test, expect, renderComponent, mountHtml, createHarness } from "@wrnexus/test";
|
|
|
|
test("counter renders its label", async () => {
|
|
const html = await renderComponent(SRC, { start: 3, label: "Hits" });
|
|
expect(html).toContain("Hits");
|
|
});
|
|
|
|
test("reactive scope hydrates", () => {
|
|
const { querySelector } = mountHtml(serverHtml);
|
|
expect(querySelector("[data-text]")?.textContent).toBe("3");
|
|
});
|
|
|
|
test("home page responds", async () => {
|
|
const app = await createHarness("examples/basic-app");
|
|
const res = await app.fetch("/");
|
|
expect(res.status).toBe(200);
|
|
await app.close();
|
|
});</code></pre>
|
|
<p>Calling an API route handler directly:</p>
|
|
<pre data-language="ts"><code>import { test, expect, callRoute } from "@wrnexus/test";
|
|
import { GET } from "../app/api/health.ts";
|
|
|
|
test("health endpoint", async () => {
|
|
const res = await callRoute(GET, new Request("http://test/api/health"));
|
|
expect(res.status).toBe(200);
|
|
});</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 { Context } from '@wrnexus/core';
|
|
export { createContext } from '@wrnexus/core';
|
|
export { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, mock, spyOn, test } from 'bun:test';
|
|
|
|
/**
|
|
* @wrnexus/test — testing utilities for WRNexusJS apps. Runs on `bun test` (via
|
|
* `wrnexus test`). Import everything from one place:
|
|
*
|
|
* import { test, expect, renderComponent, mountHtml, createHarness } from "@wrnexus/test";
|
|
*
|
|
* test("counter renders its label", async () => {
|
|
* const html = await renderComponent(SRC, { start: 3, label: "Hits" });
|
|
* expect(html).toContain("Hits");
|
|
* });
|
|
*
|
|
* test("home page responds", async () => {
|
|
* const app = await createHarness("examples/basic-app");
|
|
* const res = await app.fetch("/");
|
|
* expect(res.status).toBe(200);
|
|
* await app.close();
|
|
* });
|
|
*/
|
|
|
|
/** Compile a `.wrn` component source + render it to HTML with the given props. */
|
|
declare function renderComponent(source: string, props?: Record<string, unknown>): Promise<string>;
|
|
/**
|
|
* 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): {
|
|
document: Document;
|
|
window: unknown;
|
|
querySelector: (sel: string) => Element | null;
|
|
querySelectorAll: (sel: string) => Element[];
|
|
};
|
|
/** Call an API route handler with a `Context` built from a Request. */
|
|
declare function callRoute(handler: (ctx: Context) => Response | Promise<Response>, request: Request): Promise<Response>;
|
|
interface Harness {
|
|
/** Base URL of the ephemeral test server. */
|
|
url: string;
|
|
/** Fetch a path on the app (relative to `url`). */
|
|
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
/** The scanned router (pages/api/realtime/components). */
|
|
router: unknown;
|
|
/** Stop the server. */
|
|
close(): void;
|
|
}
|
|
interface HarnessOptions {
|
|
/** Config/env profile. Default "test". */
|
|
profile?: string;
|
|
}
|
|
/**
|
|
* Boot the app on an ephemeral port for integration tests (pages, API routes,
|
|
* middleware, the full pipeline). Uses the "test" profile by default so it picks
|
|
* up your test database/env. Remember to `await app.close()`.
|
|
*/
|
|
declare function createHarness(projectRoot: string, options?: HarnessOptions): Promise<Harness>;
|
|
|
|
export { type Harness, type HarnessOptions, callRoute, createHarness, mountHtml, renderComponent };
|
|
</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<string, unknown>): Promise<string>;</code></pre></article><article class="example-card"><h3>Example 3</h3><pre data-language="ts"><code>function mountHtml(html: string): {
|
|
document: Document;
|
|
window: unknown;
|
|
querySelector: (sel: string) => Element | null;
|
|
querySelectorAll: (sel: string) => Element[];
|
|
};</code></pre></article><article class="example-card"><h3>Example 4</h3><pre data-language="ts"><code>function callRoute(
|
|
handler: (ctx: Context) => Response | Promise<Response>,
|
|
request: Request,
|
|
): Promise<Response>;</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>
|
|
}
|
|
}
|