[READ-ONLY] Mirror of https://github.com/danielroe/nuxt-vitest. An vitest environment with support for testing code that needs a Nuxt runtime environment
nuxt nuxt-module testing unit-testing vitest
0

Configure Feed

Select the types of activity you want to include in your feed.

style: lint

Daniel Roe (Nov 20, 2023, 11:13 PM UTC) df32e212 4e9db752

+69 -64
+1 -1
packages/nuxt-vitest/package.json
··· 64 64 "nuxt": "3.7.4", 65 65 "vitest": "0.33.0" 66 66 } 67 - } 67 + }
+68 -63
packages/vitest-environment-nuxt/src/runtime/mount.ts
··· 71 71 } 72 72 } 73 73 74 - return new Promise<ReturnType<typeof mount<T>> & { setupState: any }>(resolve => { 75 - const vm = mount( 76 - { 77 - setup: (props: Record<string, any>, ctx: SetupContext) => { 78 - setupContext = ctx 79 - return NuxtRoot.setup(props, { 80 - ...ctx, 81 - expose: () => {}, 82 - }) 83 - }, 84 - render: (renderContext: any) => 85 - h( 86 - Suspense, 87 - { 88 - onResolve: () => 89 - nextTick().then(() => { 90 - ;(vm as any).setupState = setupState 91 - resolve(vm as any) 92 - }), 93 - }, 94 - { 95 - default: () => 96 - h({ 97 - async setup() { 98 - const router = useRouter() 99 - await router.replace(route) 100 - 101 - // Proxy top-level setup/render context so test wrapper resolves child component 102 - const clonedComponent = { 103 - ...component, 104 - render: render 105 - ? (_ctx: any, ...args: any[]) => { 106 - // add all _ctx properties to renderContext 107 - // the renderContext must remain intact, otherwise the emits don't work 108 - for (const key in _ctx) { 109 - renderContext[key] = _ctx[key] 110 - } 111 - return render.apply(_ctx, [renderContext, ...args]) 112 - } 113 - : undefined, 114 - setup: setup 115 - ? (props: Record<string, any>) => 116 - wrappedSetup(props, setupContext) 117 - : undefined, 118 - } 119 - 120 - return () => 121 - h(clonedComponent, { ...props, ...attrs }, slots) 122 - }, 123 - }), 124 - } 125 - ), 126 - }, 127 - defu(_options, { 128 - slots, 129 - global: { 130 - config: { 131 - globalProperties: vueApp.config.globalProperties, 74 + return new Promise<ReturnType<typeof mount<T>> & { setupState: any }>( 75 + resolve => { 76 + const vm = mount( 77 + { 78 + setup: (props: Record<string, any>, ctx: SetupContext) => { 79 + setupContext = ctx 80 + return NuxtRoot.setup(props, { 81 + ...ctx, 82 + expose: () => {}, 83 + }) 132 84 }, 133 - provide: vueApp._context.provides, 134 - components: { RouterLink }, 85 + render: (renderContext: any) => 86 + h( 87 + Suspense, 88 + { 89 + onResolve: () => 90 + nextTick().then(() => { 91 + ;(vm as any).setupState = setupState 92 + resolve(vm as any) 93 + }), 94 + }, 95 + { 96 + default: () => 97 + h({ 98 + async setup() { 99 + const router = useRouter() 100 + await router.replace(route) 101 + 102 + // Proxy top-level setup/render context so test wrapper resolves child component 103 + const clonedComponent = { 104 + ...component, 105 + render: render 106 + ? (_ctx: any, ...args: any[]) => { 107 + // add all _ctx properties to renderContext 108 + // the renderContext must remain intact, otherwise the emits don't work 109 + for (const key in _ctx) { 110 + renderContext[key] = _ctx[key] 111 + } 112 + return render.apply(_ctx, [ 113 + renderContext, 114 + ...args, 115 + ]) 116 + } 117 + : undefined, 118 + setup: setup 119 + ? (props: Record<string, any>) => 120 + wrappedSetup(props, setupContext) 121 + : undefined, 122 + } 123 + 124 + return () => 125 + h(clonedComponent, { ...props, ...attrs }, slots) 126 + }, 127 + }), 128 + } 129 + ), 135 130 }, 136 - } satisfies ComponentMountingOptions<T>) as ComponentMountingOptions<T> 137 - ) 138 - }) 131 + defu(_options, { 132 + slots, 133 + global: { 134 + config: { 135 + globalProperties: vueApp.config.globalProperties, 136 + }, 137 + provide: vueApp._context.provides, 138 + components: { RouterLink }, 139 + }, 140 + } satisfies ComponentMountingOptions<T>) as ComponentMountingOptions<T> 141 + ) 142 + } 143 + ) 139 144 }