274 lines
19 KiB
Plaintext
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<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>
|
|
<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 { 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="api"><h2>Complete TypeScript API</h2><p>Generated from the exact installed package declarations.</p><pre data-language="typescript"><code>import { ProblemDetails, Context } from '@wrnexus/core';
|
|
export { createContext } from '@wrnexus/core';
|
|
export { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, mock, spyOn, test } from 'bun:test';
|
|
|
|
interface TestRequestOptions extends Omit<RequestInit, "body"> {
|
|
body?: BodyInit | Record<string, unknown> | URLSearchParams | FormData | null;
|
|
baseUrl?: string;
|
|
}
|
|
/** 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<T> {
|
|
response: Response;
|
|
body: T;
|
|
}
|
|
declare function readJsonResponse<T = unknown>(response: Response): Promise<JsonResponse<T>>;
|
|
declare function expectProblem(response: Response, status?: number): Promise<ProblemDetails>;
|
|
interface Deferred<T> {
|
|
promise: Promise<T>;
|
|
resolve(value: T | PromiseLike<T>): void;
|
|
reject(reason?: unknown): void;
|
|
}
|
|
declare function deferred<T>(): Deferred<T>;
|
|
interface WaitForOptions {
|
|
timeoutMs?: number;
|
|
intervalMs?: number;
|
|
signal?: AbortSignal;
|
|
}
|
|
/** Poll a condition without depending on fake timers or a browser runtime. */
|
|
declare function waitFor(condition: () => boolean | Promise<boolean>, options?: WaitForOptions): Promise<void>;
|
|
declare class MemoryCookieJar {
|
|
#private;
|
|
apply(response: Response): void;
|
|
header(): string;
|
|
request(path: string, options?: TestRequestOptions): Request;
|
|
clear(): void;
|
|
}
|
|
|
|
interface TransactionalDatabase {
|
|
tx<T>(callback: (transaction: TransactionalDatabase) => Promise<T>): Promise<T>;
|
|
}
|
|
/** Run test work in a real transaction and always force rollback. */
|
|
declare function withDatabaseRollback<T>(db: TransactionalDatabase, run: (transaction: TransactionalDatabase) => T | Promise<T>): Promise<T>;
|
|
declare function createFactory<T extends Record<string, unknown>>(build: (sequence: number) => T): {
|
|
build(overrides?: Partial<T>): T;
|
|
buildMany(count: number, overrides?: Partial<T>): T[];
|
|
reset(): void;
|
|
};
|
|
interface BrowserArtifactPage {
|
|
screenshot(options: {
|
|
path: string;
|
|
fullPage?: boolean;
|
|
}): Promise<unknown>;
|
|
context(): {
|
|
tracing?: {
|
|
stop(options: {
|
|
path: string;
|
|
}): Promise<unknown>;
|
|
};
|
|
};
|
|
}
|
|
declare function captureBrowserArtifacts(page: BrowserArtifactPage, testName: string, options?: {
|
|
root?: string;
|
|
screenshot?: boolean;
|
|
trace?: boolean;
|
|
}): Promise<{
|
|
screenshot?: string;
|
|
trace?: string;
|
|
}>;
|
|
|
|
/**
|
|
* @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 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 };
|
|
</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 { 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></article><article class="example-card"><h3>Calling an API route handler directly</h3><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></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>
|
|
}
|
|
}
|