Skip to content

Commit

Permalink
fix: use package.json name for a workspace project if not provided (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 26, 2024
1 parent 7c993e9 commit 48fba19
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
19 changes: 15 additions & 4 deletions 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'
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
if (typeof name !== 'string' || !name)
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
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
@@ -0,0 +1,4 @@
{
"name": "@vitest/space_3",
"private": true
}
1 change: 0 additions & 1 deletion test/workspaces/space_3/vitest.config.ts
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

0 comments on commit 48fba19

Please sign in to comment.