Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

feat: support injects, global properties (and routing!) #154

Merged
merged 6 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nuxt-vitest/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function getVitestConfigFromNuxt(
transform(code, id) {
if (id.match(/nuxt3?\/.*\/entry\./)) {
return code.replace(
/(?<!app = )entry\(\)\.catch/,
/(?<!vueAppPromise = )entry\(\)\.catch/,
'Promise.resolve().catch'
)
}
Expand Down
1 change: 1 addition & 0 deletions packages/vitest-environment-nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"peerDependencies": {
"vitest": "^0.24.5 || ^0.26.0 || ^0.27.0 || ^0.28.0 || ^0.29.0 || ^0.30.0",
"vue-router": "^4.0.0",
"vue": "^3.2.45"
}
}
1 change: 1 addition & 0 deletions packages/vitest-environment-nuxt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default <Environment>{
class IntersectionObserver {
observe() {}
unobserve() {}
disconnect() {}
}

const h3App = createApp()
Expand Down
25 changes: 23 additions & 2 deletions packages/vitest-environment-nuxt/src/runtime/mount.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { mount, VueWrapper } from '@vue/test-utils'
import { h, DefineComponent, Suspense, nextTick } from 'vue'
import type { RouteLocationRaw } from 'vue-router'

import { RouterLink } from './components/RouterLink'

// @ts-expect-error virtual file
import NuxtRoot from '#build/root-component.mjs'
import { useRouter } from '#imports'

interface MountSuspendedOptions {
route?: RouteLocationRaw
}

export async function mountSuspended<
T extends DefineComponent<any, any, any, any>
>(component: T) {
>(component: T, options?: MountSuspendedOptions) {
// @ts-expect-error untyped global __unctx__
const vueApp = globalThis.__unctx__.get('nuxt-app').tryUse().vueApp
return new Promise<VueWrapper<InstanceType<T>>>(resolve => {
const vm = mount(
{
Expand All @@ -17,11 +25,24 @@ export async function mountSuspended<
h(
Suspense,
{ onResolve: () => nextTick().then(() => resolve(vm as any)) },
{ default: () => h(component) }
{
default: () =>
h({
async setup() {
const router = useRouter()
await router.replace(options?.route || '/')
return () => h(component)
},
}),
}
),
},
{
global: {
config: {
globalProperties: vueApp.config.globalProperties,
},
provide: vueApp._context.provides,
components: {
RouterLink,
},
Expand Down
6 changes: 4 additions & 2 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<template>
<LazySomeComponent />
<GlobalComponent />
<!-- TODO: <NuxtPage /> -->
<NuxtLink to="/test">Test link</NuxtLink>
<NuxtPage />
<NuxtLink to="/test">
Test link
</NuxtLink>
</template>

<script setup>
Expand Down
6 changes: 6 additions & 0 deletions playground/pages/[...slug].vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<template>
<div>{{ $route.fullPath }}</div>
</template>

<script setup>
definePageMeta({
slug: 'foo',
})
</script>
30 changes: 21 additions & 9 deletions playground/tests/nuxt/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ describe('client-side nuxt features', () => {
expect(app.$router).toBeDefined()
})

it.todo('defaults to index page', async () => {
it('defaults to index page', async () => {
expect(useRoute().matched[0].meta).toMatchInlineSnapshot(`
{}
`)
expect(useRoute().fullPath).toMatchInlineSnapshot('"/blank"')
{
"slug": "foo",
}
`)
// TODO: should it be possible to push to other routes?
})

it('allows pushing to other pages', async () => {
await useRouter().push('/something')
expect(useRoute().fullPath).toMatchInlineSnapshot('"/something"')
})
})

describe('test utils', () => {
Expand All @@ -39,8 +45,8 @@ describe('test utils', () => {
expect(component.html()).toMatchInlineSnapshot(`
"<div>This is an auto-imported component</div>
<div> I am a global component </div>
<!-- TODO: <NuxtPage /> -->
<a href=\\"/test\\">Test link</a>"
<div>Index page</div>
<a href=\\"/test\\"> Test link </a>"
`)
})

Expand All @@ -54,7 +60,13 @@ describe('test utils', () => {
)
})

// TODO: make working with router - currently router components are not
// registered and route symbol is not injected
it.todo('handles nuxt routing')
it('handles nuxt routing', async () => {
const component = await mountSuspended(App, { route: '/test' })
expect(component.html()).toMatchInlineSnapshot(`
"<div>This is an auto-imported component</div>
<div> I am a global component </div>
<div>/</div>
<a href=\\"/test\\"> Test link </a>"
`)
})
})
4 changes: 2 additions & 2 deletions playground/tests/nuxt/mock-component-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ it('should mock', async () => {
expect(component.html()).toMatchInlineSnapshot(`
"<div>Mocked</div>
<div> I am a global component </div>
<!-- TODO: <NuxtPage /> -->
<a href=\\"/test\\">Test link</a>"
<div>Index page</div>
<a href=\\"/test\\"> Test link </a>"
`)
})
4 changes: 2 additions & 2 deletions playground/tests/nuxt/mock-component-2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ it('should mock', async () => {
expect(component.html()).toMatchInlineSnapshot(`
"<div> Mocked 1 * 2 = 2</div>
<div> I am a global component </div>
<!-- TODO: <NuxtPage /> -->
<a href=\\"/test\\">Test link</a>"
<div>Index page</div>
<a href=\\"/test\\"> Test link </a>"
`)
})
5 changes: 3 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.