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(types): defineWorkspace fix intellisense and report type errors #4743

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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]>()
})
})
Loading