-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: load nuxt app within
setupFiles
(#260)
- Loading branch information
Showing
11 changed files
with
142 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFileSync } from 'node:fs' | ||
import { createUnplugin } from "unplugin" | ||
|
||
const PLUGIN_NAME = 'nuxt:vitest:nuxt-root-stub' | ||
|
||
interface NuxtRootStubPluginOptions { | ||
entry: string | ||
rootStubPath: string | ||
} | ||
|
||
export const NuxtRootStubPlugin = createUnplugin((options: NuxtRootStubPluginOptions) => { | ||
return { | ||
name: PLUGIN_NAME, | ||
enforce: 'pre', | ||
vite: { | ||
async resolveId(id) { | ||
if (id.endsWith('nuxt-vitest-app-entry')) { | ||
return id | ||
} | ||
}, | ||
async load(id) { | ||
if (id.endsWith('nuxt-vitest-app-entry')) { | ||
const entryContents = readFileSync(options.entry, 'utf-8') | ||
return entryContents.replace('#build/root-component.mjs', options.rootStubPath) | ||
} | ||
} | ||
}, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
if ( | ||
typeof window !== 'undefined' && | ||
// @ts-expect-error undefined property | ||
window.__NUXT_VITEST_ENVIRONMENT__ | ||
) { | ||
// @ts-expect-error alias to allow us to transform the entrypoint | ||
await import('#app/nuxt-vitest-app-entry').then(r => r.default()) | ||
// We must manually call `page:finish` to snc route after navigation | ||
// as there is no `<NuxtPage>` instantiated by default. | ||
const nuxtApp = useNuxtApp() | ||
await nuxtApp.callHook('page:finish') | ||
useRouter().afterEach(() => nuxtApp.callHook('page:finish')) | ||
} | ||
|
||
export {} |
Oops, something went wrong.