Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/app-vitest-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
})
})
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
}
: {
Expand Down Expand Up @@ -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: {
Expand Down
26 changes: 26 additions & 0 deletions test/unit/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Loading