vitest-browser-qwik 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +130 -0
- package/dist/dist-BMuVnpzs.js +5151 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +6910 -0
- package/dist/magic-string.es-DsiZ_q8M.js +1014 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 Kunai Consulting
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Vitest Browser Qwik
|
|
2
|
+
|
|
3
|
+
A modern testing setup demonstrating browser-based testing for Qwik components using Vitest. This project showcases how to effectively test Qwik components with both Client-Side Rendering (CSR) and Server-Side Rendering (SSR), making it perfect for testing complex UI behaviors and SSR scenarios.
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add vitest-browser-qwik
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Core Features
|
|
12
|
+
|
|
13
|
+
- **`render`** - Client-Side Rendering for interactive component testing
|
|
14
|
+
- **`renderSSR`** - Server-Side Rendering for testing SSR output and hydration
|
|
15
|
+
- **`renderHook`** - Hook testing utilities (currently only CSR supported)
|
|
16
|
+
- All functions are async for predictable testing behavior
|
|
17
|
+
|
|
18
|
+
### Client-Side Rendering Example
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import { render } from 'vitest-browser-qwik'
|
|
22
|
+
import { expect, test } from 'vitest'
|
|
23
|
+
|
|
24
|
+
test('renders counter with CSR', async () => {
|
|
25
|
+
const screen = await render(<Counter initialCount={1} />);
|
|
26
|
+
await expect.element(screen.getByText('Count is 1')).toBeVisible();
|
|
27
|
+
await screen.getByRole('button', { name: 'Increment' }).click();
|
|
28
|
+
await expect.element(screen.getByText('Count is 2')).toBeVisible();
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Server-Side Rendering Example
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import { renderSSR } from 'vitest-browser-qwik'
|
|
36
|
+
import { expect, test } from 'vitest'
|
|
37
|
+
|
|
38
|
+
test('renders counter with SSR', async () => {
|
|
39
|
+
const screen = await renderSSR(<Counter initialCount={5} />);
|
|
40
|
+
|
|
41
|
+
// Test the server-rendered HTML
|
|
42
|
+
expect(screen.container.innerHTML).toContain('Count is 5');
|
|
43
|
+
expect(screen.container.innerHTML).toContain('button');
|
|
44
|
+
|
|
45
|
+
// Can also use DOM queries on SSR content
|
|
46
|
+
await expect.element(screen.getByText('Count is 5')).toBeVisible();
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Hook Testing Example
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { renderHook } from 'vitest-browser-qwik'
|
|
54
|
+
import { useSignal } from '@builder.io/qwik'
|
|
55
|
+
|
|
56
|
+
test('tests custom hook', async () => {
|
|
57
|
+
const { result } = await renderHook(() => {
|
|
58
|
+
const count = useSignal(0);
|
|
59
|
+
return { count, increment: () => count.value++ };
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(result.count.value).toBe(0);
|
|
63
|
+
result.increment();
|
|
64
|
+
expect(result.count.value).toBe(1);
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## SSR vs CSR
|
|
69
|
+
|
|
70
|
+
Both `render` and `renderSSR` provide the same testing interface, but work differently under the hood:
|
|
71
|
+
|
|
72
|
+
- **`render` (CSR)**: Renders components in the browser context with full interactivity
|
|
73
|
+
- **`renderSSR` (SSR)**: Executes components in a Node.js context to generate server-side HTML, then provides that HTML for testing
|
|
74
|
+
|
|
75
|
+
The SSR approach is unique because it executes your components in a different context than your test files, simulating real server-side rendering behavior.
|
|
76
|
+
|
|
77
|
+
### Page Object Integration
|
|
78
|
+
|
|
79
|
+
You can also use the `page` object, as this library injects `.render` and `.renderServerHTML` methods:
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
import { expect, test } from 'vitest'
|
|
83
|
+
|
|
84
|
+
test('renders counter with page object', async () => {
|
|
85
|
+
const screen = await page.render(<Counter initialCount={1} />);
|
|
86
|
+
await expect.element(screen.getByText('Count is 1')).toBeVisible();
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Render Options
|
|
91
|
+
|
|
92
|
+
The `render` function accepts an options object as its second parameter:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
interface RenderOptions {
|
|
96
|
+
// Optional HTMLElement where the component will be rendered
|
|
97
|
+
container?: HTMLElement;
|
|
98
|
+
// Optional HTMLElement that serves as the base element (defaults to document.body)
|
|
99
|
+
baseElement?: HTMLElement;
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Example with options:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
import { render } from 'vitest-browser-qwik'
|
|
107
|
+
|
|
108
|
+
test('renders with custom container', async () => {
|
|
109
|
+
const customContainer = document.createElement('div');
|
|
110
|
+
const screen = await render(<MyComponent />, {
|
|
111
|
+
container: customContainer
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**Note**: `renderSSR` currently does not support custom render options due to its execution in a separate Node.js context.
|
|
117
|
+
|
|
118
|
+
## Important Notes
|
|
119
|
+
|
|
120
|
+
- **Always await**: All functions from `vitest-browser-qwik` are async and should be awaited for predictable behavior
|
|
121
|
+
- **SSR Context**: `renderSSR` executes components in a Node.js context separate from your test files, providing true server-side rendering simulation
|
|
122
|
+
- **Same Interface**: Both CSR and SSR provide the same testing interface, making it easy to test both rendering modes
|
|
123
|
+
|
|
124
|
+
## Contributing
|
|
125
|
+
|
|
126
|
+
Feel free to open issues and pull requests. All contributions are welcome!
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|