From 9e46795d066a712b55286622c0c27d2b98a87853 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 28 Jun 2023 12:24:44 +0100 Subject: [PATCH] fix: allow configuring default starting url, and respect `baseURL` --- src/nuxt-vitest/config.ts | 5 +++++ src/vitest-environment-nuxt/index.ts | 24 +++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/nuxt-vitest/config.ts b/src/nuxt-vitest/config.ts index 60816aa1d..1d6418e2e 100644 --- a/src/nuxt-vitest/config.ts +++ b/src/nuxt-vitest/config.ts @@ -160,6 +160,11 @@ declare module 'vitest' { interface EnvironmentOptions { nuxt?: { rootDir?: string + /** + * The starting URL for your Nuxt window environment + * @default {http://localhost:3000} + */ + url?: string overrides?: NuxtConfig } } diff --git a/src/vitest-environment-nuxt/index.ts b/src/vitest-environment-nuxt/index.ts index e433b4215..905b36fc0 100644 --- a/src/vitest-environment-nuxt/index.ts +++ b/src/vitest-environment-nuxt/index.ts @@ -1,6 +1,7 @@ import type { Environment } from 'vitest' import { Window, GlobalWindow } from 'happy-dom' import { createFetch } from 'ofetch' +import { joinURL } from 'ufo' import { createApp, toNodeListener } from 'h3' import type { App } from 'h3' import { populateGlobal } from 'vitest/environments' @@ -12,17 +13,9 @@ import { export default { name: 'nuxt', async setup(_, environmentOptions) { - const win = new (GlobalWindow || Window)({ - // Happy-dom defaults to `about:blank` - url: 'http://localhost:3000', - }) as any as Window & { - __app: App - __registry: Set - __NUXT__: any - $fetch: any - fetch: any - IntersectionObserver: any - } + const startingURL = environmentOptions.url || joinURL('http://localhost:3000', environmentOptions?.nuxtRuntimeConfig.app?.baseURL || '/') + + const win: NuxtWindow = new (GlobalWindow || Window)({ url: startingURL }) as any win.__NUXT__ = { serverRendered: false, @@ -87,3 +80,12 @@ export default { } }, } + +interface NuxtWindow extends Window { + __app: App + __registry: Set + __NUXT__: any + $fetch: any + fetch: any + IntersectionObserver: any +}