@hamur/test-utils
Component tests for one UI library, on top of hamur/test: one entry per library, which names hamur, the adapter and the library as peers and has no dependency. Install it as a dev dependency next to the adapter your app uses; html has no client components, so it has no entry.
bun add -d @hamur/test-utils
@hamur/test-utils/react, @hamur/test-utils/vue, @hamur/test-utils/preact, @hamur/test-utils/svelte, @hamur/test-utils/solid
Each entry exports the same names, typed by its library. Import the entry of your app's framework; another one throws @hamur/test-utils: the app's framework is "vue"; import mountSuspended from "@hamur/test-utils/vue". Every error of the package starts @hamur/test-utils: .
mountSuspended, MountOptions, Mounted
function mountSuspended<C = unknown>(component: string, options?: MountOptions<C>): Promise<Mounted<C>>
interface MountOptions<C> {
props?: PropsOf<C> // JSON values only
route?: string // "/"
backend?: "chrome" | "webkit" | "cdp" // as createPage
}
interface Mounted<C> {
readonly page: TestPage
text(selector?: string): Promise<string> // the mount root by default
html(selector?: string): Promise<string> // the inner HTML of the mount root by default
click(selector: string): Promise<void>
setProps(props: Partial<PropsOf<C>>): Promise<void>
unmount(): Promise<void>
}
Mounts one component in a real browser, on the client, with the adapter's own mount: the app's plugins, app and runtime config, head and state are there, and useRoute() reads route. The first call starts a server in the test process that builds a client bundle of the component through the app's client build (once per component in the process) and serves it with an empty #<app.rootId>; every other path it answers from the app, so useFetch("/api/x") reaches the app's server routes. The test page opens with createPage and mountSuspended resolves once the component mounted and its async data settled.
import { mountSuspended } from "@hamur/test-utils/vue"
import { hasBrowser } from "hamur/test"
import { expect, test } from "bun:test"
test.skipIf(!hasBrowser())("the counter counts", async () => {
const counter = await mountSuspended("app/components/Counter.vue", { props: { start: 0 } })
await counter.click("button")
expect(await counter.text()).toBe("1")
await counter.setProps({ start: 5 })
expect(await counter.text()).toBe("5")
await counter.unmount()
})
componentis a module path relative to the app root: a component value is a function of the test process, which the browser cannot get.propstravel to the browser as JSON.setPropsapplies new ones the library's way (React and Preact render again, Vue and Svelte set a reactive props object, Solid a store);clickandsetPropsresolve two animation frames and 50 ms after the change.unmount()removes the component and closes the page. One process serves one app, as for the test preload.- Name
Cto typeprops:mountSuspended<typeof import("#app/components/Counter.vue").default>(…).
Errors:
@hamur/test-utils: mountSuspended needs a module path such as "app/components/Counter.vue"; a component value cannot be sent to the browser@hamur/test-utils: mountSuspended props must be JSON values; props.onClick is a function@hamur/test-utils: the app's framework is "vue"; import mountSuspended from "@hamur/test-utils/vue"@hamur/test-utils: mountSuspended("app/components/Counter.vue") did not mount: …, with the page's console problemshamur/test: mountSuspended "app/components/X.vue" does not resolve from <rootDir>hamur/test: mountSuspended route must be a path starting with "/" (got "x")hamur/test: mountSuspended needs bun test
renderSuspended
function renderSuspended<C>(
component: C | string,
options?: Omit<RenderComponentOptions, "props"> & { props?: PropsOf<C> },
): Promise<RenderedComponent>
renderComponent of hamur/test with props typed by the library: it renders on the server, in the test process, and needs no browser.
PropsOf
type PropsOf<C> = …;
The props of C as the library types them: React and Preact ComponentProps<C>, Vue the $props of a component, Svelte and Solid ComponentProps<C>. Without C, any object.
mockComponent
function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void>
Replaces one component file of the app for the rest of the test file, wherever it is imported, in renderPage, renderComponent, renderSuspended and mountSuspended. path is the file's path from the app root ("app/components/base/Button.vue"). replacement is a module path ("test/mocks/BaseButton.vue") or a factory that returns the component or { default: component }. After the file the real component is back.
import { mockComponent } from "@hamur/test-utils/vue"
import { renderPage } from "hamur/test"
import { expect, test } from "bun:test"
await mockComponent("app/components/base/Button.vue", "test/mocks/BaseButton.vue")
test("the page shows the mock", async () => {
expect((await renderPage("/")).html).toContain("mock:")
})
- A module path works everywhere. A factory is a function of the test process, so it works only where the test process renders:
renderPage,renderComponentandrenderSuspended.mountSuspendedbuilds the component for a browser and takes the module path. - The mock file is loaded with the app's build, so a
.vueor.sveltefile works; it may import the original component. - It returns a promise: call it with
awaitat the top of the file or inbeforeAll. It needsbun test.
Errors:
@hamur/test-utils: mockComponent "app/components/base/Buton.vue" does not resolve from <rootDir>@hamur/test-utils: mockComponent "app/components/base/Button.vue" replacement "test/mocks/Missing.vue" does not resolve from <rootDir>@hamur/test-utils: mountSuspended cannot send the factory of mockComponent("app/components/base/Button.vue") to the browser; pass a module path: mockComponent("app/components/base/Button.vue", "test/mocks/Button.vue")@hamur/test-utils: mockComponent needs bun test