From 01ceaf7a745c40f4af23e6c86912a09b88868960 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 4 May 2023 21:43:08 +0100 Subject: [PATCH 1/6] fix(nuxt-vitest): update promise name --- packages/nuxt-vitest/src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt-vitest/src/config.ts b/packages/nuxt-vitest/src/config.ts index 10c6b224..8f2a03fb 100644 --- a/packages/nuxt-vitest/src/config.ts +++ b/packages/nuxt-vitest/src/config.ts @@ -87,7 +87,7 @@ export async function getVitestConfigFromNuxt( transform(code, id) { if (id.match(/nuxt3?\/.*\/entry\./)) { return code.replace( - /(? Date: Thu, 4 May 2023 21:44:14 +0100 Subject: [PATCH 2/6] fix: pass global properties and injections to `mountSuspended` --- packages/vitest-environment-nuxt/src/runtime/mount.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/vitest-environment-nuxt/src/runtime/mount.ts b/packages/vitest-environment-nuxt/src/runtime/mount.ts index e85d175e..f5619f43 100644 --- a/packages/vitest-environment-nuxt/src/runtime/mount.ts +++ b/packages/vitest-environment-nuxt/src/runtime/mount.ts @@ -9,6 +9,8 @@ import NuxtRoot from '#build/root-component.mjs' export async function mountSuspended< T extends DefineComponent >(component: T) { + // @ts-expect-error untyped global __unctx__ + const vueApp = globalThis.__unctx__.get('nuxt-app').tryUse().vueApp return new Promise>>(resolve => { const vm = mount( { @@ -22,6 +24,10 @@ export async function mountSuspended< }, { global: { + config: { + globalProperties: vueApp.config.globalProperties, + }, + provide: vueApp._context.provides, components: { RouterLink, }, From d14b98b4b219a8084add1a3f302afbeadc06c899 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 4 May 2023 21:44:40 +0100 Subject: [PATCH 3/6] fix: mock `disconnect` on observer --- packages/vitest-environment-nuxt/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vitest-environment-nuxt/src/index.ts b/packages/vitest-environment-nuxt/src/index.ts index 5b0ffe56..b1c73840 100644 --- a/packages/vitest-environment-nuxt/src/index.ts +++ b/packages/vitest-environment-nuxt/src/index.ts @@ -42,6 +42,7 @@ export default { class IntersectionObserver { observe() {} unobserve() {} + disconnect() {} } const h3App = createApp() From 7ef7690f95ebd72e20f1b1520ab609afcb0a6ea8 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 4 May 2023 21:45:20 +0100 Subject: [PATCH 4/6] test: test `` as well --- playground/app.vue | 6 ++++-- playground/tests/nuxt/index.spec.ts | 4 ++-- playground/tests/nuxt/mock-component-1.spec.ts | 4 ++-- playground/tests/nuxt/mock-component-2.spec.ts | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/playground/app.vue b/playground/app.vue index 64aaa70b..cbf057f3 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -1,8 +1,10 @@ diff --git a/playground/tests/nuxt/index.spec.ts b/playground/tests/nuxt/index.spec.ts index 6915d37b..c4aca538 100644 --- a/playground/tests/nuxt/index.spec.ts +++ b/playground/tests/nuxt/index.spec.ts @@ -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', () => { @@ -39,7 +45,7 @@ describe('test utils', () => { expect(component.html()).toMatchInlineSnapshot(` "
This is an auto-imported component
I am a global component
-
/blank
+
Index page
Test link " `) }) @@ -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(` + "
This is an auto-imported component
+
I am a global component
+
/
+ Test link " + `) + }) }) diff --git a/playground/tests/nuxt/mock-component-1.spec.ts b/playground/tests/nuxt/mock-component-1.spec.ts index c84ee330..640e75a9 100644 --- a/playground/tests/nuxt/mock-component-1.spec.ts +++ b/playground/tests/nuxt/mock-component-1.spec.ts @@ -16,7 +16,7 @@ it('should mock', async () => { expect(component.html()).toMatchInlineSnapshot(` "
Mocked
I am a global component
-
/blank
+
Index page
Test link " `) }) diff --git a/playground/tests/nuxt/mock-component-2.spec.ts b/playground/tests/nuxt/mock-component-2.spec.ts index 006623b5..8d086baa 100644 --- a/playground/tests/nuxt/mock-component-2.spec.ts +++ b/playground/tests/nuxt/mock-component-2.spec.ts @@ -9,7 +9,7 @@ it('should mock', async () => { expect(component.html()).toMatchInlineSnapshot(` "
Mocked 1 * 2 = 2
I am a global component
-
/blank
+
Index page
Test link " `) }) From c1a29154449a3848d2aac221f0f5381909d984cf Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 4 May 2023 23:55:46 +0100 Subject: [PATCH 6/6] fix: add `vue-router` to peerDeps --- packages/vitest-environment-nuxt/package.json | 1 + packages/vitest-environment-nuxt/src/runtime/mount.ts | 2 +- pnpm-lock.yaml | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/vitest-environment-nuxt/package.json b/packages/vitest-environment-nuxt/package.json index 3a0d35ab..794beed3 100644 --- a/packages/vitest-environment-nuxt/package.json +++ b/packages/vitest-environment-nuxt/package.json @@ -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" } } diff --git a/packages/vitest-environment-nuxt/src/runtime/mount.ts b/packages/vitest-environment-nuxt/src/runtime/mount.ts index 3f4198bb..d2401f68 100644 --- a/packages/vitest-environment-nuxt/src/runtime/mount.ts +++ b/packages/vitest-environment-nuxt/src/runtime/mount.ts @@ -1,6 +1,6 @@ import { mount, VueWrapper } from '@vue/test-utils' import { h, DefineComponent, Suspense, nextTick } from 'vue' -import { RouteLocationRaw } from 'vue-router' +import type { RouteLocationRaw } from 'vue-router' import { RouterLink } from './components/RouterLink' diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 91b2801a..13884440 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -135,6 +135,9 @@ importers: vue: specifier: ^3.2.45 version: 3.2.47 + vue-router: + specifier: ^4.0.0 + version: 4.1.6(vue@3.2.47) playground: devDependencies: @@ -1917,7 +1920,6 @@ packages: /@vue/devtools-api@6.5.0: resolution: {integrity: sha512-o9KfBeaBmCKl10usN4crU53fYtC1r7jJwdGKjPT24t348rHxgfpZ0xL3Xm/gLUYnc0oTp8LAmrxOeLyu6tbk2Q==} - dev: true /@vue/reactivity-transform@3.2.47: resolution: {integrity: sha512-m8lGXw8rdnPVVIdIFhf0LeQ/ixyHkH5plYuS83yop5n7ggVJU+z5v0zecwEnX7fa7HNLBhh2qngJJkxpwEEmYA==} @@ -9185,7 +9187,6 @@ packages: dependencies: '@vue/devtools-api': 6.5.0 vue: 3.2.47 - dev: true /vue@3.2.47: resolution: {integrity: sha512-60188y/9Dc9WVrAZeUVSDxRQOZ+z+y5nO2ts9jWXSTkMvayiWxCWOWtBQoYjLeccfXkiiPZWAHcV+WTPhkqJHQ==}