Skip to content

Commit 48fba19

Browse files
authoredApr 26, 2024··
fix: use package.json name for a workspace project if not provided (#5608)
1 parent 7c993e9 commit 48fba19

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed
 

‎packages/vitest/src/node/plugins/workspace.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { basename, dirname, relative } from 'pathe'
1+
import { existsSync, readFileSync } from 'node:fs'
2+
import { basename, dirname, relative, resolve } from 'pathe'
23
import type { UserConfig as ViteConfig, Plugin as VitePlugin } from 'vite'
34
import { configDefaults } from '../../defaults'
45
import { generateScopedClassName } from '../../integrations/css/css-modules'
@@ -35,10 +36,20 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
3536
const root = testConfig.root || viteConfig.root || options.root
3637
let name = testConfig.name
3738
if (!name) {
38-
if (typeof options.workspacePath === 'string')
39-
name = basename(options.workspacePath.endsWith('/') ? options.workspacePath.slice(0, -1) : dirname(options.workspacePath))
40-
else
39+
if (typeof options.workspacePath === 'string') {
40+
// if there is a package.json, read the name from it
41+
const dir = options.workspacePath.endsWith('/')
42+
? options.workspacePath.slice(0, -1)
43+
: dirname(options.workspacePath)
44+
const pkgJsonPath = resolve(dir, 'package.json')
45+
if (existsSync(pkgJsonPath))
46+
name = JSON.parse(readFileSync(pkgJsonPath, 'utf-8')).name
47+
if (typeof name !== 'string' || !name)
48+
name = basename(dir)
49+
}
50+
else {
4151
name = options.workspacePath.toString()
52+
}
4253
}
4354

4455
const config: ViteConfig = {

‎test/watch/test/workspaces.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ it('editing a file that is imported in different workspaces reruns both files',
6565
writeFileSync(srcMathFile, `${srcMathContent}\n`, 'utf8')
6666

6767
await vitest.waitForStdout('RERUN src/math.ts')
68-
await vitest.waitForStdout('|space_3| math.space-3-test.ts')
68+
await vitest.waitForStdout('|@vitest/space_3| math.space-3-test.ts')
6969
await vitest.waitForStdout('|space_1| test/math.spec.ts')
7070
await vitest.waitForStdout('Test Files 2 passed')
7171
})
@@ -100,7 +100,7 @@ it('adding a new test file matching core project config triggers re-run', async
100100

101101
// Test case should not be run by other projects
102102
expect(vitest.stdout).not.include('|space_1|')
103-
expect(vitest.stdout).not.include('|space_3|')
103+
expect(vitest.stdout).not.include('|@vitest/space_3|')
104104
expect(vitest.stdout).not.include('|node|')
105105
expect(vitest.stdout).not.include('|happy-dom|')
106106
})
@@ -114,7 +114,7 @@ it('adding a new test file matching project specific config triggers re-run', as
114114

115115
await vitest.waitForStdout('Running added dynamic test')
116116
await vitest.waitForStdout('RERUN space_3/new-dynamic.space-3-test.ts')
117-
await vitest.waitForStdout('|space_3| new-dynamic.space-3-test.ts')
117+
await vitest.waitForStdout('|@vitest/space_3| new-dynamic.space-3-test.ts')
118118

119119
// Wait for tests to end
120120
await vitest.waitForStdout('Waiting for file changes')

‎test/workspaces/space_3/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@vitest/space_3",
3+
"private": true
4+
}

‎test/workspaces/space_3/vitest.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { defineProject } from 'vitest/config'
33
export default defineProject({
44
test: {
55
include: ['**/*.space-3-test.ts'],
6-
name: 'space_3',
76
environment: 'node',
87
globalSetup: './localSetup.ts',
98
},

0 commit comments

Comments
 (0)
Please sign in to comment.