From 5ca9e04979be773d373a3d3649afcd649b853e5c Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:53:45 +0900 Subject: [PATCH 1/4] chore: set `resolve.mainFields` and `resolve.conditions` for SSR environment --- packages/vitest/src/node/plugins/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 5b687e27e421..3402ee3e3c83 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -113,6 +113,17 @@ export async function VitestPlugin( // https://github.com/vitejs/vite/pull/16453 emptyOutDir: false, }, + // @ts-expect-error environments only exists in Vite 6 + environments: { + ssr: { + resolve: { + // by default Vite resolves `module` field, which not always a native ESM module + // setting this option can bypass that and fallback to cjs version + mainFields: [], + conditions: ['node'], + }, + }, + }, test: { poolOptions: { threads: { From bfa17d8b143cdb2ce856318c14f04ae90fd80be0 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:14:43 +0900 Subject: [PATCH 2/4] chore: add it to workspace plugin too --- packages/vitest/src/node/plugins/workspace.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/vitest/src/node/plugins/workspace.ts b/packages/vitest/src/node/plugins/workspace.ts index bdffdbe16117..1961eaffe172 100644 --- a/packages/vitest/src/node/plugins/workspace.ts +++ b/packages/vitest/src/node/plugins/workspace.ts @@ -96,6 +96,17 @@ export function WorkspaceVitestPlugin( ), }, }, + // @ts-expect-error environments only exists in Vite 6 + environments: { + ssr: { + resolve: { + // by default Vite resolves `module` field, which not always a native ESM module + // setting this option can bypass that and fallback to cjs version + mainFields: [], + conditions: ['node'], + }, + }, + }, test: { name, }, From 80f87af81c97c919c8c773e6fd46fad40b37540b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Wed, 13 Nov 2024 13:02:30 +0900 Subject: [PATCH 3/4] chore: use ts-ignore --- packages/vitest/src/node/plugins/index.ts | 3 ++- packages/vitest/src/node/plugins/workspace.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 3402ee3e3c83..3aa5afa36941 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -113,7 +113,8 @@ export async function VitestPlugin( // https://github.com/vitejs/vite/pull/16453 emptyOutDir: false, }, - // @ts-expect-error environments only exists in Vite 6 + // eslint-disable-next-line ts/ban-ts-comment + // @ts-ignore Vite 6 compat environments: { ssr: { resolve: { diff --git a/packages/vitest/src/node/plugins/workspace.ts b/packages/vitest/src/node/plugins/workspace.ts index 1961eaffe172..5d204d254eac 100644 --- a/packages/vitest/src/node/plugins/workspace.ts +++ b/packages/vitest/src/node/plugins/workspace.ts @@ -96,7 +96,8 @@ export function WorkspaceVitestPlugin( ), }, }, - // @ts-expect-error environments only exists in Vite 6 + // eslint-disable-next-line ts/ban-ts-comment + // @ts-ignore Vite 6 compat environments: { ssr: { resolve: { From 45c904b22170555137924ace87d97c1e65265ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Wed, 13 Nov 2024 13:07:05 +0900 Subject: [PATCH 4/4] chore: fix format --- packages/vitest/src/node/plugins/index.ts | 2 +- packages/vitest/src/node/plugins/workspace.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 3aa5afa36941..b24a0e06fce8 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -113,7 +113,7 @@ export async function VitestPlugin( // https://github.com/vitejs/vite/pull/16453 emptyOutDir: false, }, - // eslint-disable-next-line ts/ban-ts-comment + // eslint-disable-next-line ts/ban-ts-comment // @ts-ignore Vite 6 compat environments: { ssr: { diff --git a/packages/vitest/src/node/plugins/workspace.ts b/packages/vitest/src/node/plugins/workspace.ts index 5d204d254eac..8f4e3c9027e0 100644 --- a/packages/vitest/src/node/plugins/workspace.ts +++ b/packages/vitest/src/node/plugins/workspace.ts @@ -96,7 +96,7 @@ export function WorkspaceVitestPlugin( ), }, }, - // eslint-disable-next-line ts/ban-ts-comment + // eslint-disable-next-line ts/ban-ts-comment // @ts-ignore Vite 6 compat environments: { ssr: {