Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use package.json name for a workspace project if not provided #5608

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 15 additions & 4 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
else
AriPerkkio marked this conversation as resolved.
Show resolved Hide resolved
name = basename(dir)
}
else {
name = options.workspacePath.toString()
}
}

const config: ViteConfig = {
Expand Down
6 changes: 3 additions & 3 deletions test/watch/test/workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down Expand Up @@ -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|')
})
Expand All @@ -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')
Expand Down
4 changes: 4 additions & 0 deletions test/workspaces/space_3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@vitest/space_3",
"private": true
}
1 change: 0 additions & 1 deletion test/workspaces/space_3/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
Loading