Skip to content

Commit

Permalink
fix(types): defineWorkspace fix intellisense and report type errors (
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Dec 15, 2023
1 parent 486a3e6 commit 9cc3668
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 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
}
2 changes: 1 addition & 1 deletion test/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"private": true,
"scripts": {
"test": "vitest run --typecheck"
"test": "vitest run --typecheck.enabled"
},
"devDependencies": {
"vite": "latest",
Expand Down
79 changes: 73 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,76 @@ 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', () => {
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: {},
},
},
])
})

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

expectTypeOf(defineWorkspace([
'./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: {},
},
},
])).items.toMatchTypeOf<DefineWorkspaceParameter[number]>()
})
})

0 comments on commit 9cc3668

Please sign in to comment.