Skip to content

Commit 1c768f4

Browse files
authored
feat: Update vitest docs (#1589)
* feat: 4.43 * chore: * chore: tidy * chore: update vitest docs (add mocking and spying) * chore:
1 parent 9aa21fa commit 1c768f4

File tree

16 files changed

+1752
-98
lines changed

16 files changed

+1752
-98
lines changed

docs/testing/01-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ Stencil supports the following for testing components:
2121

2222
The integrated Stencil Test Runner is deprecated as of Stencil v4.43 and will be removed in Stencil v5. We recommend migrating to the new testing tools:
2323

24-
- **For `--spec` style tests**: Migrate to [@stencil/vitest](./vitest/01-overview.md). It has a similar API to the current Jest integration but gives you far greater control. You can test against different bundles/outputs, test in a real browser (for accessibility tests, visual regressions, etc.), or pick your node-dom of choice (jsdom, happy-dom, or mock-doc). See the [migration guide](./vitest/06-migration.md) for detailed instructions.
24+
- **For `--spec` style tests**: Migrate to [@stencil/vitest](./vitest/01-overview.md). It has a similar API to the current Jest integration but gives you far greater control. You can test against different bundles/outputs, test in a real browser (for accessibility tests, visual regressions, etc.), or pick your node-dom of choice (jsdom, happy-dom, or mock-doc). See the [migration guide](./vitest/07-migration.md) for detailed instructions.
2525

2626
- **For `--e2e` style tests**: Many library authors actually want "component" tests—isolated testing of components that run in a browser. For this, use [@stencil/vitest](./vitest/01-overview.md) with browser mode. For true end-to-end tests (routing, full applications, proper onload initialization), use [@stencil/playwright](./playwright/01-overview.md).

docs/testing/stencil-testrunner/01-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ sidebar_label: Overview
99

1010
**The Stencil Test Runner is deprecated as of Stencil v4.43 and will be removed in Stencil v5.**
1111

12-
We recommend migrating to [@stencil/vitest](../vitest/01-overview.md) for all new and existing projects. See the [migration guide](../vitest/06-migration.md) for detailed instructions on updating your tests.
12+
We recommend migrating to [@stencil/vitest](../vitest/01-overview.md) for all new and existing projects. See the [migration guide](../vitest/07-migration.md) for detailed instructions on updating your tests.
1313

1414
**Why is this being removed?**
1515

docs/testing/vitest/01-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ Add the following scripts to your `package.json`:
132132

133133
## Migration from Stencil Test Runner
134134

135-
If you're migrating from the Stencil Test Runner, see the [Migration Guide](./06-migration.md) for detailed instructions on updating your tests.
135+
If you're migrating from the Stencil Test Runner, see the [Migration Guide](./07-migration.md) for detailed instructions on updating your tests.

docs/testing/vitest/02-configuration.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ For spec tests, you can choose between different DOM implementations using `envi
100100
environmentOptions: {
101101
stencil: {
102102
// Options: 'mock-doc' (default), 'jsdom', 'happy-dom'
103+
// *note: jsdom and happy-dom require additional dependencies*
103104
domEnvironment: 'jsdom'
104105
},
105106
},
@@ -165,8 +166,8 @@ export {};
165166
Depending on your Stencil configuration, you may need to adjust how components are loaded:
166167

167168
- **Default (lazy-load)**: Works with the standard dev build
168-
- **Production build**: Use `--prod` flag or set `buildDist: true` in your stencil.config to generate production bundles
169-
- **Custom elements**: If using the `dist-custom-elements` output target, adjust the import path accordingly
169+
- **Production build**: Use `--prod` flag to generate production bundles
170+
- **Custom elements**: If using the `dist-custom-elements` output target, adjust the setup file accordingly. If you wish to test this output in `--watch` mode, set `buildDist: true` in your stencil.config
170171

171172
## TypeScript Configuration
172173

docs/testing/vitest/03-writing-tests.md

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,25 @@ slug: /testing-vitest-writing-tests
1111

1212
## Rendering Components
1313

14-
### Basic Rendering
15-
1614
Use the `render` function to render a component for testing:
1715

1816
```tsx
1917
import { render, h, describe, it, expect } from '@stencil/vitest';
2018

2119
describe('my-component', () => {
2220
it('renders', async () => {
23-
const { root } = await render(<my-component name="World" />);
21+
const { root, waitForChanges } = await render(<my-component name="World" />);
2422
expect(root.textContent).toContain('World');
23+
24+
// Update props and wait for re-render
25+
root.name = 'Stencil';
26+
await waitForChanges();
27+
expect(root.textContent).toContain('Stencil');
2528
});
2629
});
2730
```
2831

29-
### Render Return Values
30-
31-
The `render` function returns an object with utilities for interacting with the rendered component:
32-
33-
```tsx
34-
const { root, waitForChanges, setProps, unmount, spyOnEvent } = await render(<my-component />);
35-
```
36-
37-
| Property | Description |
38-
| -------- | ----------- |
39-
| `root` | The rendered component element |
40-
| `waitForChanges` | Wait for component re-renders after state/prop changes |
41-
| `setProps` | Update component props and wait for changes |
42-
| `unmount` | Remove the component from the DOM |
43-
| `spyOnEvent` | Create a spy for custom events |
44-
45-
### Updating Props
46-
47-
There are two ways to update component props:
48-
49-
```tsx
50-
const { root, waitForChanges, setProps } = await render(<my-component name="World" />);
51-
52-
// Option 1: Direct property assignment
53-
root.name = 'Stencil';
54-
await waitForChanges();
55-
56-
// Option 2: Using setProps
57-
await setProps({ name: 'Stencil' });
58-
```
59-
60-
### Unmounting
61-
62-
Clean up by unmounting the component:
63-
64-
```tsx
65-
const { unmount } = await render(<my-component />);
66-
// ... run tests
67-
unmount();
68-
```
32+
For the complete render function API including all return values and options, see the [API Reference](./04-api.md#render-function).
6933

7034
## Event Testing
7135

docs/testing/vitest/04-api.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Complete reference for the `@stencil/vitest` testing utilities.
1111

1212
## Render Function
1313

14-
### `render(VNode)`
14+
### `render(VNode, options?)`
1515

1616
Renders a Stencil component for testing.
1717

@@ -21,7 +21,42 @@ import { render, h } from '@stencil/vitest';
2121
const result = await render(<my-component name="World" />);
2222
```
2323

24-
**Returns:**
24+
### Options
25+
26+
| Option | Type | Default | Description |
27+
| ------ | ---- | ------- | ----------- |
28+
| `waitForReady` | `boolean` | `true` | Wait for component hydration before returning |
29+
| `spyOn` | `SpyOnOptions` | - | Configure spies for methods, props, and lifecycle hooks |
30+
31+
#### `waitForReady`
32+
33+
By default, `render()` waits for components to be fully hydrated before returning. It detects when Stencil applies the hydrated flag (class or attribute) to your component, respecting your `stencil.config` settings.
34+
35+
```tsx
36+
// Default behavior - waits for hydration
37+
const { root } = await render(<my-component />);
38+
39+
// Skip hydration wait (useful for pre-ready states)
40+
const { root } = await render(<my-component />, { waitForReady: false });
41+
```
42+
43+
#### `spyOn`
44+
45+
Configure spies for component methods, props, and lifecycle hooks. See the [Mocking & Spying](./05-mocking.md) guide for comprehensive documentation.
46+
47+
```tsx
48+
const { root, spies } = await render(<my-component />, {
49+
spyOn: {
50+
methods: ['handleClick'],
51+
props: ['variant'],
52+
lifecycle: ['componentDidLoad'],
53+
},
54+
});
55+
56+
expect(spies?.methods.handleClick).toHaveBeenCalled();
57+
```
58+
59+
### Return Values
2560

2661
| Property | Type | Description |
2762
| -------- | ---- | ----------- |
@@ -30,6 +65,35 @@ const result = await render(<my-component name="World" />);
3065
| `setProps` | `(props: object) => Promise<void>` | Update props and wait for changes |
3166
| `unmount` | `() => void` | Remove the component from the DOM |
3267
| `spyOnEvent` | `(eventName: string) => EventSpy` | Create a spy for custom events |
68+
| `spies` | `ComponentSpies \| undefined` | Spy objects when using `spyOn` option |
69+
70+
### Example
71+
72+
```tsx
73+
import { render, h } from '@stencil/vitest';
74+
75+
const { root, waitForChanges, setProps, unmount, spyOnEvent } = await render(
76+
<my-component name="World" />
77+
);
78+
79+
// Access the element
80+
expect(root.textContent).toContain('World');
81+
82+
// Update props directly
83+
root.name = 'Stencil';
84+
await waitForChanges();
85+
86+
// Or update props via setProps
87+
await setProps({ name: 'Vitest' });
88+
89+
// Spy on events
90+
const eventSpy = spyOnEvent('myEvent');
91+
root.click();
92+
expect(eventSpy).toHaveReceivedEvent();
93+
94+
// Unmount component
95+
unmount();
96+
```
3397

3498
## Custom Matchers
3599

0 commit comments

Comments
 (0)