@noma.to/qwik-testing-library 1.0.2 → 1.0.4
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/README.md +128 -20
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ src="https://raw.githubusercontent.com/ianlet/qwik-testing-library/main/high-vol
|
|
|
37
37
|
|
|
38
38
|
[qtl-docs-repo]: https://github.com/ianlet/qwik-testing-library/blob/main/README.md
|
|
39
39
|
|
|
40
|
-
[build-badge]: https://img.shields.io/github/actions/workflow/status/ianlet/qwik-testing-library/
|
|
40
|
+
[build-badge]: https://img.shields.io/github/actions/workflow/status/ianlet/qwik-testing-library/ci.yml?style=flat-square
|
|
41
41
|
|
|
42
42
|
[build]: https://github.com/ianlet/qwik-testing-library/actions
|
|
43
43
|
|
|
@@ -91,12 +91,13 @@ src="https://raw.githubusercontent.com/ianlet/qwik-testing-library/main/high-vol
|
|
|
91
91
|
- [Installation](#installation)
|
|
92
92
|
- [Setup](#setup)
|
|
93
93
|
- [Examples](#examples)
|
|
94
|
-
|
|
94
|
+
- [Qwikstart](#qwikstart)
|
|
95
|
+
- [Qwik City - `server$` calls](#qwik-city---server-calls)
|
|
95
96
|
- [Gotchas](#gotchas)
|
|
96
97
|
- [Issues](#issues)
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
- [🐛 Bugs](#-bugs)
|
|
99
|
+
- [💡 Feature Requests](#-feature-requests)
|
|
100
|
+
- [❓ Questions](#-questions)
|
|
100
101
|
- [Contributors](#contributors)
|
|
101
102
|
- [Acknowledgements](#acknowledgements)
|
|
102
103
|
|
|
@@ -131,8 +132,19 @@ npm install --save-dev @noma.to/qwik-testing-library
|
|
|
131
132
|
|
|
132
133
|
This library supports `qwik` versions `1.7.2` and above.
|
|
133
134
|
|
|
134
|
-
You may also be interested in installing `@testing-library/jest-dom` so you can
|
|
135
|
-
use [the custom jest matchers][jest-dom].
|
|
135
|
+
You may also be interested in installing `@testing-library/jest-dom` and `@testing-library/user-event` so you can
|
|
136
|
+
use [the custom jest matchers][jest-dom] and [the user event library][user-event] to test interactions with the DOM.
|
|
137
|
+
|
|
138
|
+
```shell
|
|
139
|
+
npm install --save-dev @testing-library/jest-dom @testing-library/user-event
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Finally, we need a DOM environment to run the tests in.
|
|
143
|
+
This library was tested (for now) only with `jsdom` so we recommend using it:
|
|
144
|
+
|
|
145
|
+
```shell
|
|
146
|
+
npm install --save-dev jsdom
|
|
147
|
+
```
|
|
136
148
|
|
|
137
149
|
[npm]: https://www.npmjs.com/
|
|
138
150
|
|
|
@@ -140,22 +152,67 @@ use [the custom jest matchers][jest-dom].
|
|
|
140
152
|
|
|
141
153
|
[jest-dom]: https://github.com/testing-library/jest-dom
|
|
142
154
|
|
|
155
|
+
[user-event]: https://github.com/testing-library/user-event
|
|
156
|
+
|
|
143
157
|
## Setup
|
|
144
158
|
|
|
145
159
|
We recommend using `@noma.to/qwik-testing-library` with [Vitest][] as your test
|
|
146
|
-
runner.
|
|
160
|
+
runner.
|
|
147
161
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
162
|
+
If you haven't done so already, add vitest to your project using Qwik CLI:
|
|
163
|
+
|
|
164
|
+
```shell
|
|
165
|
+
npm qwik add vitest
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
After that, we need to configure Vitest so it can run your tests.
|
|
169
|
+
For this, create a _separate_ `vitest.config.ts` so you don't have to modify your project's `vite.config.ts`:
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
// vitest.config.ts
|
|
173
|
+
|
|
174
|
+
import {defineConfig, mergeConfig} from "vitest/config";
|
|
175
|
+
import viteConfig from "./vite.config";
|
|
176
|
+
|
|
177
|
+
export default defineConfig((configEnv) =>
|
|
178
|
+
mergeConfig(
|
|
179
|
+
viteConfig(configEnv),
|
|
180
|
+
defineConfig({
|
|
181
|
+
// qwik-testing-library needs to consider your project as a Qwik lib
|
|
182
|
+
// if it's already a Qwik lib, you can remove this section
|
|
183
|
+
build: {
|
|
184
|
+
target: "es2020",
|
|
185
|
+
lib: {
|
|
186
|
+
entry: "./src/index.ts",
|
|
187
|
+
formats: ["es", "cjs"],
|
|
188
|
+
fileName: (format, entryName) =>
|
|
189
|
+
`${entryName}.qwik.${format === "es" ? "mjs" : "cjs"}`,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
// configure your test environment
|
|
193
|
+
test: {
|
|
194
|
+
environment: "jsdom",
|
|
195
|
+
setupFiles: ["./vitest.setup.ts"],
|
|
196
|
+
globals: true,
|
|
197
|
+
},
|
|
198
|
+
}),
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
For now, `qwik-testing-library` needs to consider your project as a lib ([PR welcomed][prs] to simplify this).
|
|
204
|
+
Hence, the `build.lib` section in the config above.
|
|
205
|
+
|
|
206
|
+
As the build will try to use `./src/index.ts` as the entry point, we need to create it:
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
// ./src/index.ts
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* DO NOT DELETE THIS FILE
|
|
213
|
+
*
|
|
214
|
+
* This entrypoint is needed by @noma.to/qwik-testing-library to run your tests
|
|
215
|
+
*/
|
|
159
216
|
```
|
|
160
217
|
|
|
161
218
|
Then, create the `vitest.setup.ts` file:
|
|
@@ -238,12 +295,63 @@ describe("<Counter />", () => {
|
|
|
238
295
|
await userEvent.click(incrementBtn);
|
|
239
296
|
await userEvent.click(incrementBtn);
|
|
240
297
|
|
|
241
|
-
// assert that the counter is now
|
|
298
|
+
// assert that the counter is now 2
|
|
242
299
|
await waitFor(() => expect(screen.getByText(/count:2/)).toBeInTheDocument());
|
|
243
300
|
});
|
|
244
301
|
})
|
|
245
302
|
```
|
|
246
303
|
|
|
304
|
+
### Qwik City - `server$` calls
|
|
305
|
+
|
|
306
|
+
If one of your Qwik components uses `server$` calls, your tests might fail with a rather cryptic message (e.g. `QWIK
|
|
307
|
+
ERROR __vite_ssr_import_0__.myServerFunctionQrl is not a function` or
|
|
308
|
+
`QWIK ERROR Failed to parse URL from ?qfunc=DNpotUma33o`).
|
|
309
|
+
|
|
310
|
+
We're happy to discuss it on [Discord][discord], but we consider this failure to be a good thing:
|
|
311
|
+
your components should be tested in isolation, so you will be forced to mock your server functions.
|
|
312
|
+
|
|
313
|
+
Here is an example of how to test a component that uses `server$` calls:
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
// ~/server/blog-posts.ts
|
|
317
|
+
|
|
318
|
+
import {server$} from "@builder.io/qwik-city";
|
|
319
|
+
import {BlogPost} from "~/lib/blog-post";
|
|
320
|
+
|
|
321
|
+
export const getLatestPosts$ = server$(function (): Promise<BlogPost> {
|
|
322
|
+
// get the latest posts
|
|
323
|
+
return Promise.resolve([]);
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
```tsx
|
|
328
|
+
// ~/components/latest-post-list.spec.tsx
|
|
329
|
+
|
|
330
|
+
import {render, screen, waitFor} from "@noma.to/qwik-testing-library";
|
|
331
|
+
import {LatestPostList} from "./latest-post-list";
|
|
332
|
+
|
|
333
|
+
vi.mock('~/server/blog-posts', () => ({
|
|
334
|
+
// the mocked function should end with `Qrl` instead of `$`
|
|
335
|
+
getLatestPostsQrl: () => {
|
|
336
|
+
return Promise.resolve([{id: 'post-1', title: 'Post 1'}, {id: 'post-2', title: 'Post 2'}]);
|
|
337
|
+
},
|
|
338
|
+
}));
|
|
339
|
+
|
|
340
|
+
describe('<LatestPostList />', () => {
|
|
341
|
+
it('should render the latest posts', async () => {
|
|
342
|
+
await render(<LatestPostList/>);
|
|
343
|
+
|
|
344
|
+
await waitFor(() => expect(screen.queryAllByRole('listitem')).toHaveLength(2));
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
Notice how the mocked function is ending with `Qrl` instead of `$`, despite being named as `getLatestPosts$`.
|
|
350
|
+
This is caused by the Qwik optimizer renaming it to `Qrl`.
|
|
351
|
+
So, we need to mock the `Qrl` function instead of the original `$` one.
|
|
352
|
+
|
|
353
|
+
If your `server$` function doesn't end with `$`, the Qwik optimizer might not rename it to `Qrl`.
|
|
354
|
+
|
|
247
355
|
## Gotchas
|
|
248
356
|
|
|
249
357
|
* Watch mode (at least in Webstorm) doesn't seem to work well: components are not being updated with your latest changes
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noma.to/qwik-testing-library",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Simple and complete Qwik testing utilities that encourage good testing practices.",
|
|
5
5
|
"repository": "https://github.com/ianlet/qwik-testing-library",
|
|
6
6
|
"homepage": "https://github.com/ianlet/qwik-testing-library",
|
|
7
|
+
"license": "MIT",
|
|
7
8
|
"main": "./lib/index.qwik.mjs",
|
|
8
9
|
"qwik": "./lib/index.qwik.mjs",
|
|
9
10
|
"types": "./lib-types/index.d.ts",
|
|
@@ -36,15 +37,15 @@
|
|
|
36
37
|
"@builder.io/qwik": "1.9.0",
|
|
37
38
|
"@testing-library/dom": "^10.4.0",
|
|
38
39
|
"@types/eslint": "8.56.10",
|
|
39
|
-
"@types/node": "
|
|
40
|
-
"@typescript-eslint/eslint-plugin": "7.
|
|
41
|
-
"@typescript-eslint/parser": "7.
|
|
42
|
-
"eslint": "8.57.
|
|
40
|
+
"@types/node": "22.7.4",
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "8.7.0",
|
|
42
|
+
"@typescript-eslint/parser": "8.7.0",
|
|
43
|
+
"eslint": "8.57.1",
|
|
43
44
|
"eslint-plugin-qwik": "1.9.0",
|
|
44
45
|
"prettier": "3.3.3",
|
|
45
|
-
"typescript": "5.
|
|
46
|
+
"typescript": "5.6.2",
|
|
46
47
|
"undici": "*",
|
|
47
|
-
"vite": "5.
|
|
48
|
-
"vite-tsconfig-paths": "^
|
|
48
|
+
"vite": "5.4.8",
|
|
49
|
+
"vite-tsconfig-paths": "^5.0.1"
|
|
49
50
|
}
|
|
50
51
|
}
|