@noma.to/qwik-testing-library 1.5.0 → 1.5.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/README.md CHANGED
@@ -169,56 +169,19 @@ npm install --save-dev vitest
169
169
  ```
170
170
 
171
171
  After that, we need to configure Vitest so it can run your tests.
172
- For this, create a _separate_ `vitest.config.ts` so you don't have to modify your project's `vite.config.ts`:
172
+ Add the `test` section to your `vite.config.ts`:
173
173
 
174
174
  ```ts
175
- // vitest.config.ts
176
-
177
- import {defineConfig, mergeConfig} from "vitest/config";
178
- import viteConfig from "./vite.config";
179
-
180
- export default defineConfig((configEnv) =>
181
- mergeConfig(
182
- viteConfig(configEnv),
183
- defineConfig({
184
- // qwik-testing-library needs to consider your project as a Qwik lib
185
- // if it's already a Qwik lib, you can remove this section
186
- build: {
187
- target: "es2020",
188
- lib: {
189
- entry: "./src/index.ts",
190
- formats: ["es", "cjs"],
191
- fileName: (format, entryName) =>
192
- `${entryName}.qwik.${format === "es" ? "mjs" : "cjs"}`,
193
- },
194
- },
195
- // configure your test environment
196
- test: {
197
- environment: "jsdom", // or "happy-dom"
198
- setupFiles: [
199
- "@noma.to/qwik-testing-library/setup",
200
- "@testing-library/jest-dom/vitest", // optional, for DOM matchers
201
- ],
202
- globals: true,
203
- },
204
- }),
205
- ),
206
- );
207
- ```
208
-
209
- For now, `qwik-testing-library` needs to consider your project as a lib ([PR welcomed][prs] to simplify this).
210
- Hence, the `build.lib` section in the config above.
211
-
212
- As the build will try to use `./src/index.ts` as the entry point, we need to create it:
213
-
214
- ```ts
215
- // ./src/index.ts
216
-
217
- /**
218
- * DO NOT DELETE THIS FILE
219
- *
220
- * This entrypoint is needed by @noma.to/qwik-testing-library to run your tests
221
- */
175
+ // vite.config.ts
176
+
177
+ test: {
178
+ environment: "jsdom", // or "happy-dom"
179
+ setupFiles: [
180
+ "@noma.to/qwik-testing-library/setup",
181
+ "@testing-library/jest-dom/vitest", // optional, for DOM matchers
182
+ ],
183
+ globals: true,
184
+ },
222
185
  ```
223
186
 
224
187
  The `@noma.to/qwik-testing-library/setup` module configures Qwik globals (`qTest`, `qRuntimeQrl`, `qDev`,
@@ -327,7 +290,7 @@ Here's an example on how to use the `mock$` function:
327
290
  // import qwik-testing methods
328
291
  import {render, screen, waitFor} from "@noma.to/qwik-testing-library";
329
292
  // import qwik-mock methods
330
- import {mock$, clearAllMock} from "@noma.to/qwik-mock";
293
+ import {clearAllMocks, mock$} from "@noma.to/qwik-mock";
331
294
  // import the userEvent methods to interact with the DOM
332
295
  import {userEvent} from "@testing-library/user-event";
333
296
 
@@ -337,9 +300,7 @@ import {Counter} from "./counter";
337
300
  // describe the test suite
338
301
  describe("<Counter />", () => {
339
302
  // initialize a mock
340
- // note: the empty callback is required but currently unused
341
- const onChangeMock = mock$(() => {
342
- });
303
+ const onChangeMock = mock$();
343
304
 
344
305
  // setup beforeEach block to run before each test
345
306
  beforeEach(() => {
@@ -362,15 +323,25 @@ describe("<Counter />", () => {
362
323
  await user.click(decrementBtn);
363
324
 
364
325
  // assert that the onChange$ callback was called with the right value
365
- // note: QRLs are async in Qwik, so we need to resolve them to verify interactions
366
326
  await waitFor(() =>
367
- expect(onChangeMock.resolve()).resolves.toHaveBeenCalledWith(-1),
327
+ expect(onChangeMock).toHaveBeenCalledWith(-1),
368
328
  );
369
329
  });
370
330
  });
371
331
  })
372
332
  ```
373
333
 
334
+ You can also provide a default implementation to `mock$`:
335
+
336
+ ```tsx
337
+ const onSubmitMock = mock$(() => "success");
338
+
339
+ await render(<SubmitForm onSubmit$={onSubmitMock} />);
340
+ await user.click(screen.getByRole("button", { name: "Submit" }));
341
+
342
+ expect(await screen.findByText("success")).toBeInTheDocument();
343
+ ```
344
+
374
345
  ### Qwik City - `server$` calls
375
346
 
376
347
  If one of your Qwik components uses `server$` calls, your tests might fail with a rather cryptic message (e.g. `QWIK
@@ -55,21 +55,17 @@ async function render(ui, options = {}) {
55
55
  baseElement = document.body;
56
56
  }
57
57
  if (!container) {
58
- container = baseElement.insertBefore(document.createElement("host"), baseElement.firstChild);
58
+ container = baseElement.insertBefore(
59
+ document.createElement("host"),
60
+ baseElement.firstChild
61
+ );
59
62
  }
60
- const wrappedUi = !Wrapper ? ui : /* @__PURE__ */ jsxRuntime.jsx(Wrapper, {
61
- children: ui
62
- });
63
+ const wrappedUi = !Wrapper ? ui : /* @__PURE__ */ jsxRuntime.jsx(Wrapper, { children: ui });
63
64
  const doc = baseElement.ownerDocument;
64
65
  const win = doc.defaultView;
65
66
  new Function("document", "window", server.getQwikLoaderScript())(doc, win);
66
- const { cleanup: cleanup2 } = await qwik.render(container, wrappedUi, {
67
- serverData
68
- });
69
- mountedContainers.add({
70
- container,
71
- componentCleanup: cleanup2
72
- });
67
+ const { cleanup: cleanup2 } = await qwik.render(container, wrappedUi, { serverData });
68
+ mountedContainers.add({ container, componentCleanup: cleanup2 });
73
69
  return {
74
70
  container,
75
71
  baseElement,
@@ -82,10 +78,9 @@ async function render(ui, options = {}) {
82
78
  return template.content;
83
79
  }
84
80
  },
85
- debug: (el = baseElement, maxLength, options2) => Array.isArray(el) ? el.forEach((e) => console.log(dom.prettyDOM(e, maxLength, options2))) : console.log(dom.prettyDOM(el, maxLength, {
86
- ...options2,
87
- filterNode: () => true
88
- })),
81
+ debug: (el = baseElement, maxLength, options2) => Array.isArray(el) ? el.forEach((e) => console.log(dom.prettyDOM(e, maxLength, options2))) : console.log(
82
+ dom.prettyDOM(el, maxLength, { ...options2, filterNode: () => true })
83
+ ),
89
84
  unmount: cleanup2,
90
85
  ...dom.getQueriesForElement(container, queries)
91
86
  };
@@ -32,21 +32,17 @@ async function render(ui, options = {}) {
32
32
  baseElement = document.body;
33
33
  }
34
34
  if (!container) {
35
- container = baseElement.insertBefore(document.createElement("host"), baseElement.firstChild);
35
+ container = baseElement.insertBefore(
36
+ document.createElement("host"),
37
+ baseElement.firstChild
38
+ );
36
39
  }
37
- const wrappedUi = !Wrapper ? ui : /* @__PURE__ */ jsx(Wrapper, {
38
- children: ui
39
- });
40
+ const wrappedUi = !Wrapper ? ui : /* @__PURE__ */ jsx(Wrapper, { children: ui });
40
41
  const doc = baseElement.ownerDocument;
41
42
  const win = doc.defaultView;
42
43
  new Function("document", "window", getQwikLoaderScript())(doc, win);
43
- const { cleanup: cleanup2 } = await qwik.render(container, wrappedUi, {
44
- serverData
45
- });
46
- mountedContainers.add({
47
- container,
48
- componentCleanup: cleanup2
49
- });
44
+ const { cleanup: cleanup2 } = await qwik.render(container, wrappedUi, { serverData });
45
+ mountedContainers.add({ container, componentCleanup: cleanup2 });
50
46
  return {
51
47
  container,
52
48
  baseElement,
@@ -59,10 +55,9 @@ async function render(ui, options = {}) {
59
55
  return template.content;
60
56
  }
61
57
  },
62
- debug: (el = baseElement, maxLength, options2) => Array.isArray(el) ? el.forEach((e) => console.log(prettyDOM(e, maxLength, options2))) : console.log(prettyDOM(el, maxLength, {
63
- ...options2,
64
- filterNode: () => true
65
- })),
58
+ debug: (el = baseElement, maxLength, options2) => Array.isArray(el) ? el.forEach((e) => console.log(prettyDOM(e, maxLength, options2))) : console.log(
59
+ prettyDOM(el, maxLength, { ...options2, filterNode: () => true })
60
+ ),
66
61
  unmount: cleanup2,
67
62
  ...getQueriesForElement(container, queries)
68
63
  };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@noma.to/qwik-testing-library",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
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
7
  "license": "MIT",
8
8
  "main": "./lib/index.qwik.mjs",
9
- "qwik": "./lib/index.qwik.mjs",
10
9
  "types": "./lib-types/index.d.ts",
10
+ "qwik": "./lib/index.qwik.mjs",
11
11
  "exports": {
12
12
  ".": {
13
13
  "types": "./lib-types/index.d.ts",
@@ -40,16 +40,16 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/eslint": "8.56.10",
43
- "@types/node": "25.1.0",
44
- "@typescript-eslint/eslint-plugin": "8.54.0",
45
- "@typescript-eslint/parser": "8.54.0",
43
+ "@types/node": "25.3.3",
44
+ "@typescript-eslint/eslint-plugin": "8.56.1",
45
+ "@typescript-eslint/parser": "8.56.1",
46
46
  "eslint": "8.57.1",
47
47
  "eslint-plugin-qwik": "1.19.0",
48
48
  "prettier": "3.8.1",
49
49
  "typescript": "5.9.3",
50
50
  "undici": "*",
51
51
  "vite": "7.3.1",
52
- "vite-tsconfig-paths": "^6.0.5",
52
+ "vite-tsconfig-paths": "^6.1.1",
53
53
  "vitest": "^4.0.18"
54
54
  },
55
55
  "peerDependencies": {