@nuxt/test-utils-nightly 3.21.0-20251029-131418-f2d3def → 3.21.0-20251030-161832-4f04851
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
|
|
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
|
-
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/test-utils-nightly",
|
|
3
|
-
"version": "3.21.0-
|
|
3
|
+
"version": "3.21.0-20251030-161832-4f04851",
|
|
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
|
}
|