@nuxt/test-utils-nightly 3.21.0-20251029-131418-f2d3def → 3.21.0-20251031-123045-c452751

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.
@@ -7,9 +7,33 @@ import { RenderOptions as RenderOptions$1 } from '@testing-library/vue';
7
7
 
8
8
  type Awaitable<T> = T | Promise<T>;
9
9
  type OptionalFunction<T> = T | (() => Awaitable<T>);
10
+ /**
11
+ * `registerEndpoint` allows you create Nitro endpoint that returns mocked data. It can come in handy if you want to test a component that makes requests to API to display some data.
12
+ * @param url - endpoint name (e.g. `/test/`).
13
+ * @param options - factory function that returns the mocked data or an object containing the `handler`, `method`, and `once` properties.
14
+ * - `handler`: the event handler function
15
+ * - `method`: (optional) HTTP method to match (e.g., 'GET', 'POST')
16
+ * - `once`: (optional) if true, the handler will only be used for the first matching request and then automatically removed
17
+ * @example
18
+ * ```ts
19
+ * import { registerEndpoint } from '@nuxt/test-utils/runtime'
20
+ *
21
+ * registerEndpoint("/test/", () => ({
22
+ * test: "test-field"
23
+ * }))
24
+ *
25
+ * // With once option
26
+ * registerEndpoint("/api/user", {
27
+ * handler: () => ({ name: "Alice" }),
28
+ * once: true
29
+ * })
30
+ * ```
31
+ * @see https://nuxt.com/docs/getting-started/testing#registerendpoint
32
+ */
10
33
  declare function registerEndpoint(url: string, options: EventHandler | {
11
34
  handler: EventHandler;
12
- method: HTTPMethod;
35
+ method?: HTTPMethod;
36
+ once?: boolean;
13
37
  }): () => void;
14
38
  /**
15
39
  * `mockNuxtImport` allows you to mock Nuxt's auto import functionality.
@@ -11,16 +11,25 @@ function registerEndpoint(url, options) {
11
11
  if (!app) {
12
12
  throw new Error("registerEndpoint() can only be used in a `@nuxt/test-utils` runtime environment");
13
13
  }
14
- const config = typeof options === "function" ? { handler: options, method: void 0 } : options;
14
+ const config = typeof options === "function" ? { handler: options, method: void 0, once: false } : options;
15
15
  config.handler = defineEventHandler(config.handler);
16
16
  const hasBeenRegistered = window.__registry.has(url);
17
17
  endpointRegistry[url] ||= [];
18
18
  endpointRegistry[url].push(config);
19
19
  if (!hasBeenRegistered) {
20
20
  window.__registry.add(url);
21
- app.use("/_" + url, defineEventHandler((event) => {
21
+ app.use("/_" + url, defineEventHandler(async (event) => {
22
22
  const latestHandler = [...endpointRegistry[url] || []].reverse().find((config2) => config2.method ? event.method === config2.method : true);
23
- return latestHandler?.handler(event);
23
+ if (!latestHandler) return;
24
+ const result = await latestHandler.handler(event);
25
+ if (!latestHandler.once) return result;
26
+ const index = endpointRegistry[url]?.indexOf(latestHandler);
27
+ if (index === void 0 || index === -1) return result;
28
+ endpointRegistry[url]?.splice(index, 1);
29
+ if (endpointRegistry[url]?.length === 0) {
30
+ window.__registry.delete(url);
31
+ }
32
+ return result;
24
33
  }), {
25
34
  match(_, event) {
26
35
  return endpointRegistry[url]?.some((config2) => config2.method ? event?.method === config2.method : true) ?? false;
@@ -91,7 +100,7 @@ async function mountSuspended(component, options) {
91
100
  cleanupFunction();
92
101
  }
93
102
  const vueApp = tryUseNuxtApp()?.vueApp || globalThis.__unctx__.get("nuxt-app").tryUse().vueApp;
94
- const { render, setup, data, computed, methods } = component;
103
+ const { render, setup, data, computed, methods, ...componentRest } = component;
95
104
  let setupContext;
96
105
  let setupState;
97
106
  const setProps = reactive({});
@@ -139,6 +148,7 @@ async function mountSuspended(component, options) {
139
148
  (resolve) => {
140
149
  const vm = mount(
141
150
  {
151
+ __cssModules: componentRest.__cssModules,
142
152
  setup: (props2, ctx) => {
143
153
  setupContext = ctx;
144
154
  if (options?.scoped) {
@@ -322,7 +332,7 @@ async function renderSuspended(component, options) {
322
332
  } = options || {};
323
333
  const { render: renderFromTestingLibrary } = await import('@testing-library/vue');
324
334
  const vueApp = tryUseNuxtApp()?.vueApp || globalThis.__unctx__.get("nuxt-app").tryUse().vueApp;
325
- const { render, setup, data, computed, methods } = component;
335
+ const { render, setup, data, computed, methods, ...componentRest } = component;
326
336
  let setupContext;
327
337
  let setupState;
328
338
  const setProps = reactive({});
@@ -366,6 +376,7 @@ async function renderSuspended(component, options) {
366
376
  return new Promise((resolve) => {
367
377
  const utils = renderFromTestingLibrary(
368
378
  {
379
+ __cssModules: componentRest.__cssModules,
369
380
  setup: (props2, ctx) => {
370
381
  setupContext = ctx;
371
382
  const scope = effectScope();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/test-utils-nightly",
3
- "version": "3.21.0-20251029-131418-f2d3def",
3
+ "version": "3.21.0-20251031-123045-c452751",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/nuxt/test-utils.git"
@@ -183,17 +183,5 @@
183
183
  "engines": {
184
184
  "node": "^20.0.0 || ^22.0.0 || >=24.0.0"
185
185
  },
186
- "packageManager": "pnpm@10.19.0",
187
- "pnpm": {
188
- "onlyBuiltDependencies": [
189
- "@tailwindcss/oxide",
190
- "better-sqlite3"
191
- ],
192
- "ignoredBuiltDependencies": [
193
- "esbuild",
194
- "oxc-resolver",
195
- "unrs-resolver",
196
- "vue-demi"
197
- ]
198
- }
186
+ "packageManager": "pnpm@10.19.0"
199
187
  }