Select the types of activity you want to include in your feed.
[READ-ONLY] Mirror of https://github.com/danielroe/nuxt-vitest. An vitest environment with support for testing code that needs a Nuxt runtime environment
Select the types of activity you want to include in your feed.
feat!: option to replace `happy-dom` with `jsdom` (#121)
BREAKING CHANGE: you now need to explicitly add `happy-dom` or `jsdom` as a dev dependency based on which `domEnvironment` you want to be in. (`happy-dom` is set by default.)
···174174 * @default {nuxt-test}
175175 */
176176 rootId?: string
177177+ /**
178178+ * The name of the DOM environment to use.
179179+ *
180180+ * It also needs to be installed as a dev dependency in your project.
181181+ *
182182+ * @default {happy-dom}
183183+ */
184184+ domEnvironment?: 'happy-dom' | 'jsdom'
177185 }
178186 }
179187}
+21-19
packages/vitest-environment-nuxt/src/index.ts
···11import type { Environment } from 'vitest'
22-import { Window, GlobalWindow } from 'happy-dom'
32import { createFetch } from 'ofetch'
43import { joinURL } from 'ufo'
54import { createApp, toNodeListener } from 'h3'
66-import type { App } from 'h3'
75import { populateGlobal } from 'vitest/environments'
86import {
97 createCall,
108 createFetch as createLocalFetch,
119} from 'unenv/runtime/fetch/index'
1010+import type { NuxtBuiltinEnvironment } from './types'
1111+import happyDom from './env/happy-dom'
1212+import jsdom from './env/jsdom'
12131314export default <Environment>{
1415 name: 'nuxt',
1515- async setup(_, environmentOptions) {
1616- const startingURL = environmentOptions.url || joinURL('http://localhost:3000', environmentOptions?.nuxtRuntimeConfig.app?.baseURL || '/')
1717-1818- const win: NuxtWindow = new (GlobalWindow || Window)({ url: startingURL }) as any
1616+ async setup(global, environmentOptions) {
1717+ const url = joinURL('http://localhost:3000', environmentOptions?.nuxtRuntimeConfig.app?.baseURL || '/')
1818+ const { window: win, teardown } = await {
1919+ 'happy-dom': happyDom,
2020+ jsdom
2121+ }[environmentOptions.nuxt.domEnvironment as NuxtBuiltinEnvironment || 'happy-dom'](global, {
2222+ ...environmentOptions,
2323+ happyDom: {
2424+ url,
2525+ ...environmentOptions?.happyDom,
2626+ },
2727+ jsdom: {
2828+ url,
2929+ ...environmentOptions?.jsdom,
3030+ }
3131+ })
19322033 win.__NUXT__ = {
2134 serverRendered: false,
···5669 return localFetch(init, options)
5770 }
58715959- win.$fetch = createFetch({ fetch: win.fetch, Headers: win.Headers as any })
7272+ win.$fetch = createFetch({ fetch: win.fetch, Headers: win.Headers })
60736174 win.__registry = registry
6275 win.__app = h3App
···7184 return {
7285 // called after all tests with this env have been run
7386 teardown() {
7474- win.happyDOM.cancelAsync()
7575- // @ts-expect-error
8787+ teardown()
7688 keys.forEach(key => delete global[key])
7777- // @ts-expect-error
7889 originals.forEach((v, k) => (global[k] = v))
7990 },
8091 }
8192 },
8282-}
8383-8484-interface NuxtWindow extends Window {
8585- __app: App
8686- __registry: Set<string>
8787- __NUXT__: any
8888- $fetch: any
8989- fetch: any
9090- IntersectionObserver: any
9193}
+16
packages/vitest-environment-nuxt/src/types.ts
···11+import { App } from "h3"
22+33+export type NuxtBuiltinEnvironment = 'happy-dom' | 'jsdom'
44+export interface NuxtWindow extends Window {
55+ __app: App
66+ __registry: Set<string>
77+ __NUXT__: any
88+ $fetch: any
99+ fetch: any
1010+ IntersectionObserver: any
1111+ Headers: any
1212+}
1313+export type EnvironmentNuxt = (global: any, options: Record<string, any>) => Promise<{
1414+ window: NuxtWindow,
1515+ teardown(): void
1616+}>