diff --git a/examples/app-vitest-browser/package.json b/examples/app-vitest-browser/package.json index 7c714652f..50ee63402 100644 --- a/examples/app-vitest-browser/package.json +++ b/examples/app-vitest-browser/package.json @@ -14,6 +14,7 @@ "@nuxt/test-utils": "latest", "@vitest/browser-playwright": "5.0.5", "@vue/test-utils": "2.5.1", + "dequal": "2.0.3", "nuxt": "4.6.3", "typescript": "6.0.3", "vitest": "5.0.5", diff --git a/examples/app-vitest-browser/test/nuxt/components/optimize-deps.spec.ts b/examples/app-vitest-browser/test/nuxt/components/optimize-deps.spec.ts new file mode 100644 index 000000000..79b8913dd --- /dev/null +++ b/examples/app-vitest-browser/test/nuxt/components/optimize-deps.spec.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from 'vitest' +import { mountSuspended } from '@nuxt/test-utils/runtime' +import { dequal } from 'dequal' + +describe('optimizeDeps', () => { + it('can import a dependency that is not pre-bundled', async () => { + const component = await mountSuspended(defineComponent({ + render: () => h('div', String(dequal({ a: 1 }, { a: 1 }))), + })) + expect(component.text()).toBe('true') + }) +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3262dab03..ffe48bc85 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -465,6 +465,9 @@ importers: '@vue/test-utils': specifier: 2.5.1 version: 2.5.1(@vue/compiler-dom@3.5.44)(@vue/server-renderer@3.5.44)(vue@3.5.44(typescript@6.0.3)) + dequal: + specifier: 2.0.3 + version: 2.0.3 nuxt: specifier: 4.6.3 version: 4.6.3(@babel/plugin-syntax-jsx@7.29.7(@babel/core@7.29.7(supports-color@11.0.0)))(@babel/plugin-syntax-typescript@7.29.7(@babel/core@7.29.7(supports-color@11.0.0)))(@oxc-project/types@0.155.0)(@parcel/watcher@2.6.0)(@types/node@24.19.2)(@vue/compiler-sfc@3.5.44)(acorn@8.18.0)(better-sqlite3@13.0.3)(confbox@0.3.1)(db0@0.4.1)(dotenv@17.4.2)(esbuild@0.28.2)(eslint@10.13.0(jiti@2.7.0)(supports-color@11.0.0))(giget@3.3.1)(idb-keyval@6.3.0)(ioredis@5.11.1(supports-color@11.0.0))(lightningcss@1.33.0)(mlly@1.8.2)(optionator@0.9.4)(oxc-parser@0.155.0)(rolldown@1.2.15)(rollup-plugin-visualizer@7.1.1(rolldown@1.2.15)(rollup@4.65.0))(rollup@4.65.0)(srvx@1.0.5)(supports-color@11.0.0)(terser@5.51.2)(typescript@6.0.3)(vite@8.3.5(@types/node@24.19.2)(esbuild@0.28.2)(jiti@2.7.0)(terser@5.51.2)(yaml@2.9.1))(vue-tsc@3.3.13(typescript@6.0.3))(yaml@2.9.1) diff --git a/src/config.ts b/src/config.ts index de0ed9df2..b5b7d2d84 100644 --- a/src/config.ts +++ b/src/config.ts @@ -176,13 +176,8 @@ export async function getVitestConfigFromNuxt( optimizeDeps: { ...vitestMajor >= 5 && testConfig.browser?.enabled ? { - // Browser mode in vitest 5 reuses the project's Vite server: dependency - // discovery must stay on so vitest's CJS internals are prebundled, and deps - // imported through nuxt virtual modules are listed upfront because they - // can't be found by vite automatically which reloads the page. - // We leave `noDiscovery` unchanged to respect vitest defaults and user config. include: ['@testing-library/vue', '#nuxt-test-utils/h3'], - // aliased to mocks, excluded from pre-bundling to avoid runtime discovery + // aliased to mocks exclude: ['@vue/devtools-kit', '@vue/devtools-core'], } : { @@ -291,6 +286,10 @@ export async function getVitestConfigFromNuxt( options.viteConfig, // (overrideable) defaults { + ...vitestMajor >= 5 && testConfig.browser?.enabled && { + // deps discovered mid-run change the optimizer hash and duplicate loaded modules + optimizeDeps: { noDiscovery: true }, + }, test: { environmentOptions: { nuxt: { diff --git a/test/unit/config.spec.ts b/test/unit/config.spec.ts index 041e5801b..fa88c97ec 100644 --- a/test/unit/config.spec.ts +++ b/test/unit/config.spec.ts @@ -54,4 +54,30 @@ describe('getVitestConfigFromNuxt', () => { public: { debug: { hydration: true } }, }) }) + + it('should disable dependency discovery in browser mode unless set in the nuxt config', async () => { + const nuxt = { + options: { + appDir: process.cwd(), + modulesDir: [process.cwd()], + runtimeConfig: {}, + routeRules: {}, + app: {}, + build: { transpile: [] }, + }, + } as unknown as Nuxt + const userConfig = { test: { browser: { enabled: true } } } + + const config = await getVitestConfigFromNuxt({ + nuxt, + viteConfig: { plugins: [] } as unknown as ViteConfig, + }, {}, userConfig) + expect(config.optimizeDeps!.noDiscovery).toBe(true) + + const configWithDiscovery = await getVitestConfigFromNuxt({ + nuxt, + viteConfig: { plugins: [], optimizeDeps: { noDiscovery: false } } as unknown as ViteConfig, + }, {}, userConfig) + expect(configWithDiscovery.optimizeDeps!.noDiscovery).toBe(false) + }) })