Skip to content

Commit

Permalink
fix(vitest): use correct type for defineProject to allow usage in mer…
Browse files Browse the repository at this point in the history
…geConfig (#4498)
  • Loading branch information
sheremet-va committed Nov 14, 2023
1 parent ac3d300 commit 7dee832
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/config.ts
Expand Up @@ -26,10 +26,10 @@ export function defineConfig(config: UserConfigExport): UserConfigExport {
return config
}

export function defineProject(config: UserProjectConfigExport) {
export function defineProject<T extends UserProjectConfigExport>(config: T): T {
return config
}

export function defineWorkspace(config: (string | (UserProjectConfigExport & { extends?: string }))[]) {
export function defineWorkspace<T extends (string | (UserProjectConfigExport & { extends?: string }))[]>(config: T): T {
return config
}
2 changes: 1 addition & 1 deletion test/config/package.json
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"private": true,
"scripts": {
"test": "vitest run"
"test": "vitest run --typecheck"
},
"devDependencies": {
"vite": "latest",
Expand Down
34 changes: 34 additions & 0 deletions test/config/test/config-types.test-d.ts
@@ -0,0 +1,34 @@
import { describe, expectTypeOf, test } from 'vitest'
import { type UserWorkspaceConfig, 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>()

describe('define project helper', () => {
test('cannot define non-project fields on a project config', () => {
expectProjectTestConfig.toHaveProperty('name')
expectMainTestConfig.toHaveProperty('name')

expectProjectTestConfig.not.toHaveProperty('pool')
expectMainTestConfig.toHaveProperty('pool')

expectProjectTestConfig.not.toHaveProperty('coverage')
expectMainTestConfig.toHaveProperty('coverage')
})
})

describe('merge config helper', () => {
test('types are not conflicting', () => {
expectTypeOf(mergeConfig(
defineConfig({}),
defineProject({ test: { name: 'test' } }),
)).toMatchTypeOf<Record<string, unknown>>()
})
})

describe('workspace config', () => {
test('correctly defines return type', () => {
expectTypeOf(defineWorkspace([{ test: { name: 'test' } }])).items.toMatchTypeOf<UserWorkspaceConfig>()
expectTypeOf(defineWorkspace(['packages/*'])).items.toBeString()
})
})
6 changes: 6 additions & 0 deletions test/config/tsconfig.json
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.base.json",
"exclude": [
"**/dist/**"
]
}

0 comments on commit 7dee832

Please sign in to comment.