diff --git a/packages/vitest/src/node/plugins/workspace.ts b/packages/vitest/src/node/plugins/workspace.ts index f6df0efd2c81..d04d29d30639 100644 --- a/packages/vitest/src/node/plugins/workspace.ts +++ b/packages/vitest/src/node/plugins/workspace.ts @@ -1,4 +1,5 @@ -import { basename, dirname, relative } from 'pathe' +import { existsSync, readFileSync } from 'node:fs' +import { basename, dirname, relative, resolve } from 'pathe' import type { UserConfig as ViteConfig, Plugin as VitePlugin } from 'vite' import { configDefaults } from '../../defaults' import { generateScopedClassName } from '../../integrations/css/css-modules' @@ -35,10 +36,20 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp const root = testConfig.root || viteConfig.root || options.root let name = testConfig.name if (!name) { - if (typeof options.workspacePath === 'string') - name = basename(options.workspacePath.endsWith('/') ? options.workspacePath.slice(0, -1) : dirname(options.workspacePath)) - else + if (typeof options.workspacePath === 'string') { + // if there is a package.json, read the name from it + const dir = options.workspacePath.endsWith('/') + ? options.workspacePath.slice(0, -1) + : dirname(options.workspacePath) + const pkgJsonPath = resolve(dir, 'package.json') + if (existsSync(pkgJsonPath)) + name = JSON.parse(readFileSync(pkgJsonPath, 'utf-8')).name + if (typeof name !== 'string' || !name) + name = basename(dir) + } + else { name = options.workspacePath.toString() + } } const config: ViteConfig = { diff --git a/test/watch/test/workspaces.test.ts b/test/watch/test/workspaces.test.ts index 30fcdbeb0c69..87cff84823d4 100644 --- a/test/watch/test/workspaces.test.ts +++ b/test/watch/test/workspaces.test.ts @@ -65,7 +65,7 @@ it('editing a file that is imported in different workspaces reruns both files', writeFileSync(srcMathFile, `${srcMathContent}\n`, 'utf8') await vitest.waitForStdout('RERUN src/math.ts') - await vitest.waitForStdout('|space_3| math.space-3-test.ts') + await vitest.waitForStdout('|@vitest/space_3| math.space-3-test.ts') await vitest.waitForStdout('|space_1| test/math.spec.ts') await vitest.waitForStdout('Test Files 2 passed') }) @@ -100,7 +100,7 @@ it('adding a new test file matching core project config triggers re-run', async // Test case should not be run by other projects expect(vitest.stdout).not.include('|space_1|') - expect(vitest.stdout).not.include('|space_3|') + expect(vitest.stdout).not.include('|@vitest/space_3|') expect(vitest.stdout).not.include('|node|') expect(vitest.stdout).not.include('|happy-dom|') }) @@ -114,7 +114,7 @@ it('adding a new test file matching project specific config triggers re-run', as await vitest.waitForStdout('Running added dynamic test') await vitest.waitForStdout('RERUN space_3/new-dynamic.space-3-test.ts') - await vitest.waitForStdout('|space_3| new-dynamic.space-3-test.ts') + await vitest.waitForStdout('|@vitest/space_3| new-dynamic.space-3-test.ts') // Wait for tests to end await vitest.waitForStdout('Waiting for file changes') diff --git a/test/workspaces/space_3/package.json b/test/workspaces/space_3/package.json new file mode 100644 index 000000000000..db61b2ef3abf --- /dev/null +++ b/test/workspaces/space_3/package.json @@ -0,0 +1,4 @@ +{ + "name": "@vitest/space_3", + "private": true +} diff --git a/test/workspaces/space_3/vitest.config.ts b/test/workspaces/space_3/vitest.config.ts index 55bdced7da5d..956f9e5c8c84 100644 --- a/test/workspaces/space_3/vitest.config.ts +++ b/test/workspaces/space_3/vitest.config.ts @@ -3,7 +3,6 @@ import { defineProject } from 'vitest/config' export default defineProject({ test: { include: ['**/*.space-3-test.ts'], - name: 'space_3', environment: 'node', globalSetup: './localSetup.ts', },