Skip to content

Commit

Permalink
fix: allow configuring default starting url, and respect baseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 28, 2023
1 parent 9c8eeab commit 9e46795
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/nuxt-vitest/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
24 changes: 13 additions & 11 deletions src/vitest-environment-nuxt/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -12,17 +13,9 @@ import {
export default <Environment>{
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<string>
__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,
Expand Down Expand Up @@ -87,3 +80,12 @@ export default <Environment>{
}
},
}

interface NuxtWindow extends Window {
__app: App
__registry: Set<string>
__NUXT__: any
$fetch: any
fetch: any
IntersectionObserver: any
}

0 comments on commit 9e46795

Please sign in to comment.