|
| 1 | +# @testing-library/svelte-core |
| 2 | + |
| 3 | +Do you want to build your own Svelte testing library? You may want to use our |
| 4 | +rendering core, which abstracts away differences in Svelte versions to provide a |
| 5 | +simple API to render Svelte components into the document and clean them up |
| 6 | +afterwards |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Example Usage](#example-usage) |
| 11 | +- [API](#api) |
| 12 | + - [`render`](#render) |
| 13 | + - [`setup`](#setup) |
| 14 | + - [`mount`](#mount) |
| 15 | + - [`cleanup`](#cleanup) |
| 16 | + - [`addCleanupTask`](#addcleanuptask) |
| 17 | + - [`removeCleanupTask`](#removecleanuptask) |
| 18 | + - [Utility types](#utility-types) |
| 19 | + |
| 20 | +## Example Usage |
| 21 | + |
| 22 | +```ts |
| 23 | +import { beforeEach } from 'vitest' |
| 24 | +import * as SvelteCore from '@testing-library/svelte-core' |
| 25 | + |
| 26 | +import { bindQueries, type Screen } from './bring-your-own-queries.js' |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + SvelteCore.cleanup() |
| 30 | +}) |
| 31 | + |
| 32 | +export interface RenderResult< |
| 33 | + C extends SvelteCore.Component, |
| 34 | +> extends SvelteCore.RenderResult<C> { |
| 35 | + screen: Screen |
| 36 | +} |
| 37 | + |
| 38 | +export const render = <C extends SvelteCore.Component>( |
| 39 | + Component: SvelteCore.ComponentImport<C>, |
| 40 | + options: SvelteCore.ComponentOptions<C> |
| 41 | +): RenderResult<C> => { |
| 42 | + const renderResult = SvelteCore.render(Component, options) |
| 43 | + const screen = bindQueries(baseElement) |
| 44 | + |
| 45 | + return { screen, ...renderResult } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## API |
| 50 | + |
| 51 | +### `render` |
| 52 | + |
| 53 | +Set up the document and mount a component into that document. |
| 54 | + |
| 55 | +```ts |
| 56 | +const { baseElement, container, component, unmount, rerender } = render( |
| 57 | + Component, |
| 58 | + componentOptions, |
| 59 | + setupOptions |
| 60 | +) |
| 61 | +``` |
| 62 | + |
| 63 | +| Argument | Type | Description | |
| 64 | +| ------------------ | ------------------------------------------------------- | --------------------------------------------- | |
| 65 | +| `Component` | [Svelte component][svelte-component-docs] | An imported Svelte component | |
| 66 | +| `componentOptions` | `Props` or partial [`mount` options][svelte-mount-docs] | Options for how the component will be mounted | |
| 67 | +| `setupOptions` | `{ baseElement?: HTMLElement }` | Optionally override `baseElement` | |
| 68 | + |
| 69 | +| Result | Type | Description | Default | |
| 70 | +| ------------- | ------------------------------------------ | ---------------------------------------- | ----------------------------------- | |
| 71 | +| `baseElement` | `HTMLElement` | The base element | `document.body` | |
| 72 | +| `container` | `HTMLElement` | The component's immediate parent element | `<div>` appended to `document.body` | |
| 73 | +| `component` | [component exports][svelte-mount-docs] | The component's exports from `mount` | N/A | |
| 74 | +| `rerender` | `(props: Partial<Props>) => Promise<void>` | Update the component's props | N/A | |
| 75 | +| `unmount` | `() => void` | Unmount the component from the document | N/A | |
| 76 | + |
| 77 | +> \[!TIP] |
| 78 | +> Calling `render` is equivalent to calling `setup` followed by `mount` |
| 79 | +> |
| 80 | +> ```ts |
| 81 | +> const { baseElement, container, mountOptions } = setup( |
| 82 | +> componentOptions, |
| 83 | +> setupOptions |
| 84 | +> ) |
| 85 | +> const { component, rerender, unmount } = mount(Component, mountOptions) |
| 86 | +> ``` |
| 87 | +
|
| 88 | +[svelte-component-docs]: https://svelte.dev/docs/svelte-components |
| 89 | +[svelte-mount-docs]: https://svelte.dev/docs/svelte/imperative-component-api#mount |
| 90 | +
|
| 91 | +### `setup` |
| 92 | +
|
| 93 | +Validate options and prepare document elements for rendering. |
| 94 | +
|
| 95 | +```ts |
| 96 | +const { baseElement, target, mountOptions } = setup(options, renderOptions) |
| 97 | +``` |
| 98 | +
|
| 99 | +| Argument | Type | Description | |
| 100 | +| ------------------ | ------------------------------------------------------- | --------------------------------------------- | |
| 101 | +| `componentOptions` | `Props` or partial [`mount` options][svelte-mount-docs] | Options for how the component will be mounted | |
| 102 | +| `setupOptions` | `{ baseElement?: HTMLElement }` | Optionally override `baseElement` | |
| 103 | + |
| 104 | +| Result | Type | Description | Default | |
| 105 | +| -------------- | ------------------------------------ | ---------------------------------------- | ----------------------------------- | |
| 106 | +| `baseElement` | `HTMLElement` | The base element | `document.body` | |
| 107 | +| `container` | `HTMLElement` | The component's immediate parent element | `<div>` appended to `document.body` | |
| 108 | +| `mountOptions` | [`mount` options][svelte-mount-docs] | Validated options to pass to `mount` | `{ target, props: {} }` | |
| 109 | + |
| 110 | +### `mount` |
| 111 | + |
| 112 | +Mount a Svelte component into the document. |
| 113 | + |
| 114 | +```ts |
| 115 | +const { component, unmount, rerender } = mount(Component, options) |
| 116 | +``` |
| 117 | + |
| 118 | +| Argument | Type | Description | |
| 119 | +| -------------- | ----------------------------------------- | -------------------------------------------- | |
| 120 | +| `Component` | [Svelte component][svelte-component-docs] | An imported Svelte component | |
| 121 | +| `mountOptions` | [component options][svelte-mount-docs] | Options to pass to Svelte's `mount` function | |
| 122 | + |
| 123 | +| Result | Type | Description | |
| 124 | +| ----------- | ------------------------------------------ | --------------------------------------- | |
| 125 | +| `component` | [component exports][svelte-mount-docs] | The component's exports from `mount` | |
| 126 | +| `unmount` | `() => void` | Unmount the component from the document | |
| 127 | +| `rerender` | `(props: Partial<Props>) => Promise<void>` | Update the component's props | |
| 128 | + |
| 129 | +### `cleanup` |
| 130 | + |
| 131 | +Cleanup rendered components and added elements. Call this when your tests are |
| 132 | +over. |
| 133 | + |
| 134 | +```ts |
| 135 | +cleanup() |
| 136 | +``` |
| 137 | + |
| 138 | +### `addCleanupTask` |
| 139 | + |
| 140 | +Add a custom cleanup task to be called with `cleanup()` |
| 141 | + |
| 142 | +```ts |
| 143 | +addCleanupTask(() => { |
| 144 | + // ...reset something |
| 145 | +}) |
| 146 | +``` |
| 147 | + |
| 148 | +### `removeCleanupTask` |
| 149 | + |
| 150 | +Remove a cleanup task from `cleanup()`. Useful if a cleanup task can only be run |
| 151 | +once and may be run outside of `cleanup` |
| 152 | + |
| 153 | +```ts |
| 154 | +const customCleanup = () => { |
| 155 | + // ...reset something |
| 156 | +} |
| 157 | + |
| 158 | +addCleanupTask(customCleanup) |
| 159 | + |
| 160 | +const manuallyCleanupEarly = () => { |
| 161 | + customCleanup() |
| 162 | + removeCleanupTask(customCleanup) |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +### Utility types |
| 167 | + |
| 168 | +This module exports various utility types from |
| 169 | +`@testing-library/svelte-core/types`. They adapt to whatever Svelte version is |
| 170 | +installed, and can be used to get type signatures for imported components, |
| 171 | +props, exports, etc. |
| 172 | + |
| 173 | +See [`./types.d.ts`](./types.d.ts) for the full list of available types. |
0 commit comments