diff --git a/packages/vitest/src/node/plugins/browserLoader.ts b/packages/vitest/src/node/plugins/browserLoader.ts index 89e42486f..b282eb825 100644 --- a/packages/vitest/src/node/plugins/browserLoader.ts +++ b/packages/vitest/src/node/plugins/browserLoader.ts @@ -214,6 +214,9 @@ export async function createClusterServer( await server.listen(config.api.port) } else { deferredListen.set(server, { port: config.api.port }) + // Vite runs plugin buildStart hooks on listen; global setup and --changed + // transform modules before the first browser launch + await server.environments.client.pluginContainer.buildStart() } contribution.setupRpc(parent) return { server, parent } diff --git a/test/e2e/test/related-deps.test.ts b/test/e2e/test/related-deps.test.ts index 74a20e9a7..caa0155f9 100644 --- a/test/e2e/test/related-deps.test.ts +++ b/test/e2e/test/related-deps.test.ts @@ -1654,6 +1654,46 @@ test('the environment comment is ignored for browser tests', async () => { `) }) +test('browser tests importing an unrelated CSS module are not selected', async () => { + const root = `${process.cwd()}/vitest-test-${crypto.randomUUID()}` + useFS(root, { + 'vitest.config.js': ` + import { playwright } from '@vitest/browser-playwright' + export default { + test: { + browser: { + enabled: true, + headless: true, + provider: playwright(), + instances: [{ browser: 'chromium' }], + }, + }, + } + `, + 'src/button.module.css': '.button { color: red; }', + 'src/button.js': ` + import styles from './button.module.css' + export const buttonClass = () => styles.button + `, + 'src/other.module.css': '.other { color: blue; }', + 'src/other.js': ` + import styles from './other.module.css' + export const otherClass = () => styles.other + `, + 'button.test.js': testFile('button', `import './src/button.js'`), + 'other.test.js': testFile('other', `import './src/other.js'`), + }) + const vitest = await createVitest({ root, watch: false, related: ['src/button.js'] }) + onTestFinished(() => vitest.close()) + + const specifications = await vitest.getRelevantTestSpecifications() + expect(specifications.map((spec) => spec.moduleId.slice(root.length + 1))).toMatchInlineSnapshot(` + [ + "button.test.js", + ] + `) +}) + test('the run uses the environment comment read when filtering', async () => { const root = `${process.cwd()}/vitest-test-${crypto.randomUUID()}` const { editFile } = useFS(root, {