@nuxt/test-utils-nightly 3.20.2-20251204-114711-db7a66f → 3.20.2-20251204-130215-72cf241

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.
@@ -2,8 +2,7 @@ import { EventHandler, HTTPMethod } from 'h3';
2
2
  import { SetupContext, RenderFunction, ComputedOptions, MethodOptions, ComponentOptionsMixin, EmitsOptions, ComponentInjectOptions, ComponentOptionsWithoutProps, ComponentOptionsWithArrayProps, ComponentPropsOptions, ComponentOptionsWithObjectProps } from 'vue';
3
3
  import { ComponentMountingOptions, 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 { RenderOptions, RenderResult } from '@testing-library/vue';
7
6
 
8
7
  type Awaitable<T> = T | Promise<T>;
9
8
  type OptionalFunction<T> = T | (() => Awaitable<T>);
@@ -93,6 +92,9 @@ type MountSuspendedOptions<T> = ComponentMountingOptions<T> & {
93
92
  route?: RouteLocationRaw;
94
93
  scoped?: boolean;
95
94
  };
95
+ type MountSuspendedResult<T> = ReturnType<typeof mount<T>> & {
96
+ setupState: SetupState$1;
97
+ };
96
98
  type SetupState$1 = Record<string, any>;
97
99
  /**
98
100
  * `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:
@@ -120,16 +122,17 @@ type SetupState$1 = Record<string, any>;
120
122
  * @param component the component to be tested
121
123
  * @param options optional options to set up your component
122
124
  */
123
- declare function mountSuspended<T>(component: T, options?: MountSuspendedOptions<T>): Promise<ReturnType<typeof mount<T>> & {
124
- setupState: SetupState$1;
125
- }>;
125
+ declare function mountSuspended<T>(component: T, options?: MountSuspendedOptions<T>): Promise<MountSuspendedResult<T>>;
126
126
  declare global {
127
127
  var __cleanup: Array<() => void> | undefined;
128
128
  }
129
129
 
130
- type RenderOptions<C = unknown> = RenderOptions$1<C> & {
130
+ type RenderSuspendeOptions<T> = RenderOptions<T> & {
131
131
  route?: RouteLocationRaw;
132
132
  };
133
+ type RenderSuspendeResult = RenderResult & {
134
+ setupState: SetupState;
135
+ };
133
136
  type SetupState = Record<string, any>;
134
137
  /**
135
138
  * `renderSuspended` allows you to mount any vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins.
@@ -161,9 +164,7 @@ type SetupState = Record<string, any>;
161
164
  * @param component the component to be tested
162
165
  * @param options optional options to set up your component
163
166
  */
164
- declare function renderSuspended<T>(component: T, options?: RenderOptions<T>): Promise<_testing_library_vue.RenderResult & {
165
- setupState: SetupState;
166
- }>;
167
+ declare function renderSuspended<T>(component: T, options?: RenderSuspendeOptions<T>): Promise<RenderSuspendeResult>;
167
168
  declare global {
168
169
  interface Window {
169
170
  __cleanup?: Array<() => void>;
@@ -1,6 +1,6 @@
1
1
  import { defineEventHandler } from 'h3';
2
2
  import { mount } from '@vue/test-utils';
3
- import { reactive, h as h$1, Suspense, nextTick, effectScope, unref, isReadonly, getCurrentInstance, isRef, defineComponent as defineComponent$1 } from 'vue';
3
+ import { reactive, h as h$1, Suspense, nextTick, getCurrentInstance, effectScope, defineComponent as defineComponent$1 } from 'vue';
4
4
  import { defu } from 'defu';
5
5
  import { defineComponent, useRouter, h, tryUseNuxtApp } from '#imports';
6
6
  import NuxtRoot from '#build/root-component.mjs';
@@ -100,27 +100,11 @@ async function mountSuspended(component, options) {
100
100
  cleanupFunction();
101
101
  }
102
102
  const vueApp = tryUseNuxtApp()?.vueApp || globalThis.__unctx__.get("nuxt-app").tryUse().vueApp;
103
- const { render, setup, data, computed, methods, ...componentRest } = component;
103
+ const { render, setup, ...componentRest } = component;
104
+ let wrappedInstance = null;
104
105
  let setupContext;
105
106
  let setupState;
106
107
  const setProps = reactive({});
107
- let interceptedEmit = null;
108
- function getInterceptedEmitFunction(emit) {
109
- if (emit !== interceptedEmit) {
110
- interceptedEmit = interceptedEmit ?? ((event, ...args) => {
111
- emit(event, ...args);
112
- setupContext.emit(event, ...args);
113
- });
114
- }
115
- return interceptedEmit;
116
- }
117
- function interceptEmitOnCurrentInstance() {
118
- const currentInstance = getCurrentInstance();
119
- if (!currentInstance) {
120
- return;
121
- }
122
- currentInstance.emit = getInterceptedEmitFunction(currentInstance.emit);
123
- }
124
108
  function patchInstanceAppContext() {
125
109
  const app = getCurrentInstance()?.appContext.app;
126
110
  if (!app) return;
@@ -129,11 +113,14 @@ async function mountSuspended(component, options) {
129
113
  app[key] = value;
130
114
  }
131
115
  }
132
- let passedProps;
133
116
  let componentScope = null;
134
- const wrappedSetup = async (props2, setupContext2) => {
135
- interceptEmitOnCurrentInstance();
136
- passedProps = props2;
117
+ const wrappedSetup = async (props2, setupContext2, instanceContext) => {
118
+ const currentInstance = getCurrentInstance();
119
+ if (currentInstance) {
120
+ currentInstance.emit = (event, ...args) => {
121
+ setupContext2.emit(event, ...args);
122
+ };
123
+ }
137
124
  if (setup) {
138
125
  let result;
139
126
  if (options?.scoped) {
@@ -148,6 +135,9 @@ async function mountSuspended(component, options) {
148
135
  } else {
149
136
  result = await setup(props2, setupContext2);
150
137
  }
138
+ if (wrappedInstance?.exposed) {
139
+ instanceContext.expose(wrappedInstance.exposed);
140
+ }
151
141
  setupState = result && typeof result === "object" ? result : {};
152
142
  return result;
153
143
  }
@@ -157,8 +147,10 @@ async function mountSuspended(component, options) {
157
147
  const vm = mount(
158
148
  {
159
149
  __cssModules: componentRest.__cssModules,
150
+ inheritAttrs: false,
160
151
  setup: (props2, ctx) => {
161
152
  patchInstanceAppContext();
153
+ wrappedInstance = getCurrentInstance();
162
154
  setupContext = ctx;
163
155
  if (options?.scoped) {
164
156
  const scope = effectScope();
@@ -179,7 +171,7 @@ async function mountSuspended(component, options) {
179
171
  });
180
172
  }
181
173
  },
182
- render: (renderContext) => h$1(
174
+ render: () => h$1(
183
175
  Suspense,
184
176
  {
185
177
  onResolve: () => nextTick().then(() => {
@@ -197,56 +189,12 @@ async function mountSuspended(component, options) {
197
189
  const router = useRouter();
198
190
  await router.replace(route);
199
191
  const clonedComponent = {
200
- name: "MountSuspendedComponent",
192
+ components: {},
201
193
  ...component,
202
- render: render ? function(_ctx, ...args) {
203
- interceptEmitOnCurrentInstance();
204
- if (data && typeof data === "function") {
205
- const dataObject = data();
206
- for (const key in dataObject) {
207
- renderContext[key] = dataObject[key];
208
- }
209
- }
210
- for (const key in setupState || {}) {
211
- const warn = console.warn;
212
- console.warn = () => {
213
- };
214
- try {
215
- renderContext[key] = isReadonly(setupState[key]) ? unref(setupState[key]) : setupState[key];
216
- } catch {
217
- } finally {
218
- console.warn = warn;
219
- }
220
- if (key === "props") {
221
- renderContext[key] = cloneProps$1(renderContext[key]);
222
- }
223
- }
224
- const propsContext = "props" in renderContext ? renderContext.props : renderContext;
225
- for (const key in props || {}) {
226
- propsContext[key] = _ctx[key];
227
- }
228
- for (const key in passedProps || {}) {
229
- propsContext[key] = passedProps[key];
230
- }
231
- if (methods && typeof methods === "object") {
232
- for (const [key, value] of Object.entries(methods)) {
233
- renderContext[key] = value.bind(renderContext);
234
- }
235
- }
236
- if (computed && typeof computed === "object") {
237
- for (const [key, value] of Object.entries(computed)) {
238
- if ("get" in value) {
239
- renderContext[key] = value.get.call(renderContext);
240
- } else {
241
- renderContext[key] = value.call(renderContext);
242
- }
243
- }
244
- }
245
- return render.call(this, renderContext, ...args);
246
- } : void 0,
247
- setup: (props2) => wrappedSetup(props2, setupContext)
194
+ name: "MountSuspendedComponent",
195
+ setup: (props2, ctx) => wrappedSetup(props2, setupContext, ctx)
248
196
  };
249
- return () => h$1(clonedComponent, { ...props, ...setProps, ...attrs }, slots);
197
+ return () => h$1(clonedComponent, { ...props, ...setProps, ...attrs }, setupContext.slots);
250
198
  }
251
199
  })
252
200
  }
@@ -255,6 +203,7 @@ async function mountSuspended(component, options) {
255
203
  defu(
256
204
  _options,
257
205
  {
206
+ props,
258
207
  slots,
259
208
  attrs,
260
209
  global: {
@@ -282,58 +231,46 @@ async function mountSuspended(component, options) {
282
231
  }
283
232
  );
284
233
  }
285
- function cloneProps$1(props) {
286
- const newProps = reactive({});
287
- for (const key in props) {
288
- newProps[key] = props[key];
289
- }
290
- return newProps;
291
- }
292
234
  function wrappedMountedWrapper(wrapper) {
293
- const proxy = new Proxy(wrapper, {
294
- get: (target, prop, receiver) => {
295
- if (prop === "element") {
296
- const component = target.findComponent({ name: "MountSuspendedComponent" });
297
- return component[prop];
298
- } else if (prop === "vm") {
299
- const vm = Reflect.get(target, prop, receiver);
300
- return createVMProxy(vm, wrapper.setupState);
301
- } else {
302
- return Reflect.get(target, prop, receiver);
303
- }
235
+ const component = wrapper.findComponent({ name: "MountSuspendedComponent" });
236
+ const wrapperProps = [
237
+ "setProps",
238
+ "emitted",
239
+ "setupState",
240
+ "unmount"
241
+ ];
242
+ return new Proxy(wrapper, {
243
+ get: (_, prop, receiver) => {
244
+ if (prop === "getCurrentComponent") return getCurrentComponentPatchedProxy;
245
+ const target = wrapperProps.includes(prop) ? wrapper : Reflect.has(component, prop) ? component : wrapper;
246
+ const value = Reflect.get(target, prop, receiver);
247
+ return typeof value === "function" ? value.bind(target) : value;
304
248
  }
305
249
  });
306
- for (const key of ["props"]) {
307
- proxy[key] = new Proxy(wrapper[key], {
308
- apply: (target, thisArg, args) => {
309
- const component = thisArg.findComponent({ name: "MountSuspendedComponent" });
310
- return component[key](...args);
250
+ function getCurrentComponentPatchedProxy() {
251
+ const currentComponent = component.getCurrentComponent();
252
+ return new Proxy(currentComponent, {
253
+ get: (target, prop, receiver) => {
254
+ const value = Reflect.get(target, prop, receiver);
255
+ if (prop === "proxy" && value) {
256
+ return new Proxy(value, {
257
+ get(o, p, r) {
258
+ if (!Reflect.has(currentComponent.props, p)) {
259
+ const setupState = wrapper.setupState;
260
+ if (setupState && typeof setupState === "object") {
261
+ if (Reflect.has(setupState, p)) {
262
+ return Reflect.get(setupState, p, r);
263
+ }
264
+ }
265
+ }
266
+ return Reflect.get(o, p, r);
267
+ }
268
+ });
269
+ }
270
+ return value;
311
271
  }
312
272
  });
313
273
  }
314
- return proxy;
315
- }
316
- function createVMProxy(vm, setupState) {
317
- return new Proxy(vm, {
318
- get(target, key, receiver) {
319
- const value = Reflect.get(target, key, receiver);
320
- if (setupState && typeof setupState === "object" && key in setupState) {
321
- return unref(setupState[key]);
322
- }
323
- return value;
324
- },
325
- set(target, key, value, receiver) {
326
- if (setupState && typeof setupState === "object" && key in setupState) {
327
- const setupValue = setupState[key];
328
- if (setupValue && isRef(setupValue)) {
329
- setupValue.value = value;
330
- return true;
331
- }
332
- return Reflect.set(setupState, key, value, receiver);
333
- }
334
- return Reflect.set(target, key, value, receiver);
335
- }
336
- });
337
274
  }
338
275
 
339
276
  const WRAPPER_EL_ID = "test-wrapper";
@@ -347,27 +284,11 @@ async function renderSuspended(component, options) {
347
284
  } = options || {};
348
285
  const { render: renderFromTestingLibrary } = await import('@testing-library/vue');
349
286
  const vueApp = tryUseNuxtApp()?.vueApp || globalThis.__unctx__.get("nuxt-app").tryUse().vueApp;
350
- const { render, setup, data, computed, methods, ...componentRest } = component;
287
+ const { render, setup, ...componentRest } = component;
288
+ let wrappedInstance = null;
351
289
  let setupContext;
352
290
  let setupState;
353
291
  const setProps = reactive({});
354
- let interceptedEmit = null;
355
- function getInterceptedEmitFunction(emit) {
356
- if (emit !== interceptedEmit) {
357
- interceptedEmit = interceptedEmit ?? ((event, ...args) => {
358
- emit(event, ...args);
359
- setupContext.emit(event, ...args);
360
- });
361
- }
362
- return interceptedEmit;
363
- }
364
- function interceptEmitOnCurrentInstance() {
365
- const currentInstance = getCurrentInstance();
366
- if (!currentInstance) {
367
- return;
368
- }
369
- currentInstance.emit = getInterceptedEmitFunction(currentInstance.emit);
370
- }
371
292
  function patchInstanceAppContext() {
372
293
  const app = getCurrentInstance()?.appContext.app;
373
294
  if (!app) return;
@@ -380,13 +301,19 @@ async function renderSuspended(component, options) {
380
301
  fn();
381
302
  }
382
303
  document.querySelector(`#${WRAPPER_EL_ID}`)?.remove();
383
- let passedProps;
384
- const wrappedSetup = async (props2, setupContext2) => {
385
- interceptEmitOnCurrentInstance();
386
- passedProps = props2;
304
+ const wrappedSetup = async (props2, setupContext2, instanceContext) => {
305
+ const currentInstance = getCurrentInstance();
306
+ if (currentInstance) {
307
+ currentInstance.emit = (event, ...args) => {
308
+ setupContext2.emit(event, ...args);
309
+ };
310
+ }
387
311
  if (setup) {
388
312
  const result = await setup(props2, setupContext2);
389
313
  setupState = result && typeof result === "object" ? result : {};
314
+ if (wrappedInstance?.exposed) {
315
+ instanceContext.expose(wrappedInstance.exposed);
316
+ }
390
317
  return result;
391
318
  }
392
319
  };
@@ -400,8 +327,10 @@ async function renderSuspended(component, options) {
400
327
  const utils = renderFromTestingLibrary(
401
328
  {
402
329
  __cssModules: componentRest.__cssModules,
330
+ inheritAttrs: false,
403
331
  setup: (props2, ctx) => {
404
332
  patchInstanceAppContext();
333
+ wrappedInstance = getCurrentInstance();
405
334
  setupContext = ctx;
406
335
  const scope = effectScope();
407
336
  window.__cleanup ||= [];
@@ -413,7 +342,7 @@ async function renderSuspended(component, options) {
413
342
  expose: () => ({})
414
343
  }));
415
344
  },
416
- render: (renderContext) => (
345
+ render: () => (
417
346
  // See discussions in https://github.com/testing-library/vue-testing-library/issues/230
418
347
  // we add this additional root element because otherwise testing-library breaks
419
348
  // because there's no root element while Suspense is resolving
@@ -440,56 +369,13 @@ async function renderSuspended(component, options) {
440
369
  const router = useRouter();
441
370
  await router.replace(route);
442
371
  const clonedComponent = {
443
- name: "RenderSuspendedComponent",
372
+ components: {},
444
373
  ...component,
445
- render: render ? function(_ctx, ...args) {
446
- interceptEmitOnCurrentInstance();
447
- if (data && typeof data === "function") {
448
- const dataObject = data();
449
- for (const key in dataObject) {
450
- renderContext[key] = dataObject[key];
451
- }
452
- }
453
- for (const key in setupState || {}) {
454
- const warn = console.warn;
455
- console.warn = () => {
456
- };
457
- try {
458
- renderContext[key] = isReadonly(setupState[key]) ? unref(setupState[key]) : setupState[key];
459
- } catch {
460
- } finally {
461
- console.warn = warn;
462
- }
463
- if (key === "props") {
464
- renderContext[key] = cloneProps(renderContext[key]);
465
- }
466
- }
467
- const propsContext = "props" in renderContext ? renderContext.props : renderContext;
468
- for (const key in props || {}) {
469
- propsContext[key] = _ctx[key];
470
- }
471
- for (const key in passedProps || {}) {
472
- propsContext[key] = passedProps[key];
473
- }
474
- if (methods && typeof methods === "object") {
475
- for (const [key, value] of Object.entries(methods)) {
476
- renderContext[key] = value.bind(renderContext);
477
- }
478
- }
479
- if (computed && typeof computed === "object") {
480
- for (const [key, value] of Object.entries(computed)) {
481
- if ("get" in value) {
482
- renderContext[key] = value.get.call(renderContext);
483
- } else {
484
- renderContext[key] = value.call(renderContext);
485
- }
486
- }
487
- }
488
- return render.call(this, renderContext, ...args);
489
- } : void 0,
490
- setup: (props2) => wrappedSetup(props2, setupContext)
374
+ name: "RenderSuspendedComponent",
375
+ render,
376
+ setup: (props2, ctx) => wrappedSetup(props2, setupContext, ctx)
491
377
  };
492
- return () => h$1(clonedComponent, { ...props && typeof props === "object" ? props : {}, ...setProps, ...attrs }, slots);
378
+ return () => h$1(clonedComponent, { ...props && typeof props === "object" ? props : {}, ...setProps, ...attrs }, setupContext.slots);
493
379
  }
494
380
  })
495
381
  }
@@ -499,6 +385,7 @@ async function renderSuspended(component, options) {
499
385
  )
500
386
  },
501
387
  defu(_options, {
388
+ props,
502
389
  slots,
503
390
  attrs,
504
391
  global: {
@@ -519,12 +406,5 @@ async function renderSuspended(component, options) {
519
406
  );
520
407
  });
521
408
  }
522
- function cloneProps(props) {
523
- const newProps = reactive({});
524
- for (const key in props) {
525
- newProps[key] = props[key];
526
- }
527
- return newProps;
528
- }
529
409
 
530
410
  export { mockComponent, mockNuxtImport, mountSuspended, registerEndpoint, renderSuspended };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/test-utils-nightly",
3
- "version": "3.20.2-20251204-114711-db7a66f",
3
+ "version": "3.20.2-20251204-130215-72cf241",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/nuxt/test-utils.git"