Skip to content

Commit

Permalink
fix(config): workspace config breaks with multiple entries
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 13, 2023
1 parent fbfa234 commit e93aa56
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/vitest/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function defineProject<T extends UserProjectConfigExport>(config: T): T {
return config
}

export function defineWorkspace<T extends (string | (UserProjectConfigExport & { extends?: string }))[]>(config: T): T {
type Workspace = (string | (UserProjectConfigExport & { extends?: string }))

export function defineWorkspace(config: Workspace[]): Workspace[] {
return config
}
73 changes: 67 additions & 6 deletions test/config/test/config-types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest'
import { type UserWorkspaceConfig, defineConfig, defineProject, defineWorkspace, mergeConfig } from 'vitest/config'
import { assertType, describe, expectTypeOf, test } from 'vitest'
import { defineConfig, defineProject, defineWorkspace, mergeConfig } from 'vitest/config'

const expectMainTestConfig = expectTypeOf(defineConfig).parameter(0).resolves.toHaveProperty('test').exclude<undefined>()
const expectProjectTestConfig = expectTypeOf(defineProject).parameter(0).resolves.toHaveProperty('test').exclude<undefined>()
Expand All @@ -26,9 +26,70 @@ describe('merge config helper', () => {
})
})

describe('workspace config', () => {
test('correctly defines return type', () => {
expectTypeOf(defineWorkspace([{ test: { name: 'test' } }])).items.toMatchTypeOf<UserWorkspaceConfig>()
expectTypeOf(defineWorkspace(['packages/*'])).items.toBeString()
describe('define workspace helper', () => {
type DefineWorkspaceParameter = Parameters<typeof defineWorkspace>[0]

test('allows string', () => {
assertType<DefineWorkspaceParameter>(['./path/to/workspace'])
})

test('allows config object', () => {
assertType<DefineWorkspaceParameter>([{
test: {
name: 'Workspace Project #1',
include: ['string'],

// @ts-expect-error -- Not allowed here
coverage: {},
},
}])
})

test('allows mixing strings and config objects', () => {
// Starts with string as first item - this would break if array entries are inferred from the first item
assertType<DefineWorkspaceParameter>([
'./path/to/project',
{
test: {
name: 'Workspace Project #1',
include: ['string'],

// @ts-expect-error -- Not allowed here
coverage: {},
},
},
'./path/to/another/project',
{
extends: 'workspace custom field',
test: {
name: 'Workspace Project #2',
include: ['string'],

// @ts-expect-error -- Not allowed here
coverage: {},
},
},
])

// Starts with config object as first item
assertType<DefineWorkspaceParameter>([
{
extends: 'workspace custom field',
test: {
name: 'Workspace Project #1',
include: ['string'],

// @ts-expect-error -- Not allowed here
coverage: {},
},
},
'./path/to/project',
{ test: { name: 'Workspace Project #1' } },
'./path/to/another/project',
])
})

test('return type matches parameters', () => {
expectTypeOf(defineWorkspace).returns.toMatchTypeOf<DefineWorkspaceParameter>()
})
})

0 comments on commit e93aa56

Please sign in to comment.