@nuxt/test-utils 3.20.1 → 3.22.0

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.
@@ -1,19 +1,42 @@
1
1
  import { EventHandler, HTTPMethod } from 'h3';
2
2
  import { SetupContext, RenderFunction, ComputedOptions, MethodOptions, ComponentOptionsMixin, EmitsOptions, ComponentInjectOptions, ComponentOptionsWithoutProps, ComponentOptionsWithArrayProps, ComponentPropsOptions, ComponentOptionsWithObjectProps } from 'vue';
3
- import { ComponentMountingOptions, mount } from '@vue/test-utils';
3
+ import { mount } from '@vue/test-utils';
4
4
  import { RouteLocationRaw } from 'vue-router';
5
- import * as _testing_library_vue from '@testing-library/vue';
6
- import { RenderOptions as RenderOptions$1 } from '@testing-library/vue';
5
+ import { render } from '@testing-library/vue';
7
6
 
8
7
  type Awaitable<T> = T | Promise<T>;
9
8
  type OptionalFunction<T> = T | (() => Awaitable<T>);
9
+ /**
10
+ * `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.
11
+ * @param url - endpoint name (e.g. `/test/`).
12
+ * @param options - factory function that returns the mocked data or an object containing the `handler`, `method`, and `once` properties.
13
+ * - `handler`: the event handler function
14
+ * - `method`: (optional) HTTP method to match (e.g., 'GET', 'POST')
15
+ * - `once`: (optional) if true, the handler will only be used for the first matching request and then automatically removed
16
+ * @example
17
+ * ```ts
18
+ * import { registerEndpoint } from '@nuxt/test-utils/runtime'
19
+ *
20
+ * registerEndpoint("/test/", () => ({
21
+ * test: "test-field"
22
+ * }))
23
+ *
24
+ * // With once option
25
+ * registerEndpoint("/api/user", {
26
+ * handler: () => ({ name: "Alice" }),
27
+ * once: true
28
+ * })
29
+ * ```
30
+ * @see https://nuxt.com/docs/getting-started/testing#registerendpoint
31
+ */
10
32
  declare function registerEndpoint(url: string, options: EventHandler | {
11
33
  handler: EventHandler;
12
- method: HTTPMethod;
34
+ method?: HTTPMethod;
35
+ once?: boolean;
13
36
  }): () => void;
14
37
  /**
15
38
  * `mockNuxtImport` allows you to mock Nuxt's auto import functionality.
16
- * @param _name - name of an import to mock.
39
+ * @param _target - name of an import to mock or mocked target.
17
40
  * @param _factory - factory function that returns mocked import.
18
41
  * @example
19
42
  * ```ts
@@ -24,10 +47,17 @@ declare function registerEndpoint(url: string, options: EventHandler | {
24
47
  * return { value: 'mocked storage' }
25
48
  * }
26
49
  * })
50
+ *
51
+ * // With mocked target
52
+ * mockNuxtImport(useStorage, () => {
53
+ * return () => {
54
+ * return { value: 'mocked storage' }
55
+ * }
56
+ * })
27
57
  * ```
28
58
  * @see https://nuxt.com/docs/getting-started/testing#mocknuxtimport
29
59
  */
30
- declare function mockNuxtImport<T = unknown>(_name: string, _factory: () => T | Promise<T>): void;
60
+ declare function mockNuxtImport<T = unknown>(_target: string | T, _factory: () => T | Promise<T>): void;
31
61
  /**
32
62
  * `mockComponent` allows you to mock Nuxt's component.
33
63
  * @param path - component name in PascalCase, or the relative path of the component.
@@ -65,11 +95,26 @@ declare function mockComponent<Props = {}, RawBindings = {}, D = {}, C extends C
65
95
  declare function mockComponent<PropNames extends string, RawBindings, D, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = {}, EE extends string = string, I extends ComponentInjectOptions = {}, II extends string = string>(path: string, options: OptionalFunction<ComponentOptionsWithArrayProps<PropNames, RawBindings, D, C, M, Mixin, Extends, E, EE, I, II>>): void;
66
96
  declare function mockComponent<PropsOptions extends Readonly<ComponentPropsOptions>, RawBindings, D, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = {}, EE extends string = string, I extends ComponentInjectOptions = {}, II extends string = string>(path: string, options: OptionalFunction<ComponentOptionsWithObjectProps<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE, I, II>>): void;
67
97
 
68
- type MountSuspendedOptions<T> = ComponentMountingOptions<T> & {
69
- route?: RouteLocationRaw;
98
+ type SetupState = Record<string, any>;
99
+ type WrapperFnComponent<Fn> = Fn extends (c: infer C, o: infer _) => infer _ ? C : never;
100
+ type WrapperFnOption<Fn> = Fn extends (c: WrapperFnComponent<Fn>, o: infer O) => infer _ ? O : never;
101
+ type WrapperFnResult<Fn> = Fn extends (c: WrapperFnComponent<Fn>, o: WrapperFnOption<Fn>) => infer R ? R : never;
102
+ type WrapperSuspendedOptions<Fn> = WrapperFnOption<Fn> & {
103
+ route?: RouteLocationRaw | false;
70
104
  scoped?: boolean;
71
105
  };
72
- type SetupState$1 = Record<string, any>;
106
+ type WrapperSuspendedResult<Fn> = WrapperFnResult<Fn> & {
107
+ setupState: SetupState;
108
+ };
109
+ declare global {
110
+ interface Window {
111
+ __cleanup?: Array<() => void>;
112
+ }
113
+ }
114
+
115
+ type WrapperFn$1<C> = typeof mount<C>;
116
+ type WrapperOptions$1<C> = WrapperSuspendedOptions<WrapperFn$1<C>>;
117
+ type WrapperResult$1<C> = WrapperSuspendedResult<WrapperFn$1<C>>;
73
118
  /**
74
119
  * `mountSuspended` allows you to mount any vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins. For example:
75
120
  *
@@ -96,17 +141,11 @@ type SetupState$1 = Record<string, any>;
96
141
  * @param component the component to be tested
97
142
  * @param options optional options to set up your component
98
143
  */
99
- declare function mountSuspended<T>(component: T, options?: MountSuspendedOptions<T>): Promise<ReturnType<typeof mount<T>> & {
100
- setupState: SetupState$1;
101
- }>;
102
- declare global {
103
- var __cleanup: Array<() => void> | undefined;
104
- }
144
+ declare function mountSuspended<T>(component: T, options?: WrapperOptions$1<T>): Promise<WrapperResult$1<T>>;
105
145
 
106
- type RenderOptions<C = unknown> = RenderOptions$1<C> & {
107
- route?: RouteLocationRaw;
108
- };
109
- type SetupState = Record<string, any>;
146
+ type WrapperFn<C> = typeof render<C>;
147
+ type WrapperOptions<C> = WrapperSuspendedOptions<WrapperFn<C>>;
148
+ type WrapperResult<C> = WrapperSuspendedResult<WrapperFn<C>>;
110
149
  /**
111
150
  * `renderSuspended` allows you to mount any vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins.
112
151
  *
@@ -137,13 +176,6 @@ type SetupState = Record<string, any>;
137
176
  * @param component the component to be tested
138
177
  * @param options optional options to set up your component
139
178
  */
140
- declare function renderSuspended<T>(component: T, options?: RenderOptions<T>): Promise<_testing_library_vue.RenderResult & {
141
- setupState: SetupState;
142
- }>;
143
- declare global {
144
- interface Window {
145
- __cleanup?: Array<() => void>;
146
- }
147
- }
179
+ declare function renderSuspended<T>(component: T, options?: WrapperOptions<T>): Promise<WrapperResult<T>>;
148
180
 
149
181
  export { mockComponent, mockNuxtImport, mountSuspended, registerEndpoint, renderSuspended };