Skip to content

Commit 1ad9a5b

Browse files
authored
Fix AstroContainer usage in Vitest client environments (#14894)
1 parent c17767a commit 1ad9a5b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

.changeset/icy-impalas-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes support for Astro component rendering in Vitest test suites using a “client” environment such as `happy-dom` or `jsdom`

packages/astro/src/vite-plugin-astro/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,14 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
231231

232232
// If an Astro component is imported in code used on the client, we return an empty
233233
// module so that Vite doesn’t bundle the server-side Astro code for the client.
234-
if (!options?.ssr) {
234+
if (
235+
!options?.ssr &&
236+
// Workaround to allow tests run with Vitest in a “client” environment to still render
237+
// Astro components. See https://github.com/withastro/astro/issues/14883
238+
// TODO: In a future major we should remove this and require people test Astro rendering in
239+
// an SSR test environment, which more closely matches the real-world Vite environment.
240+
!process.env.VITEST
241+
) {
235242
return {
236243
code: `export default import.meta.env.DEV
237244
? () => {

0 commit comments

Comments
 (0)