diff --git a/packages/vitest/src/typecheck/parse.ts b/packages/vitest/src/typecheck/parse.ts index 214000c7a092..d6337197f446 100644 --- a/packages/vitest/src/typecheck/parse.ts +++ b/packages/vitest/src/typecheck/parse.ts @@ -1,6 +1,6 @@ import url from 'node:url' import { writeFile } from 'node:fs/promises' -import { dirname, join } from 'pathe' +import { basename, dirname, join, resolve } from 'pathe' import { getTsconfig as getTsconfigContent } from 'get-tsconfig' import type { TypecheckConfig } from '../types' import type { RawErrsMap, TscErrorInfo } from './types' @@ -58,11 +58,12 @@ export async function makeTscErrorInfo( } export async function getTsconfig(root: string, config: TypecheckConfig) { - const configName = config.tsconfig?.includes('jsconfig.json') - ? 'jsconfig.json' - : undefined + const configName = config.tsconfig ? basename(config.tsconfig) : undefined + const configSearchPath = config.tsconfig + ? dirname(resolve(root, config.tsconfig)) + : root - const tsconfig = getTsconfigContent(config.tsconfig || root, configName) + const tsconfig = getTsconfigContent(configSearchPath, configName) if (!tsconfig) throw new Error('no tsconfig.json found') diff --git a/test/typescript/test/__snapshots__/runner.test.ts.snap b/test/typescript/test/__snapshots__/runner.test.ts.snap index f1add4402a6c..6c56b9333cbf 100644 --- a/test/typescript/test/__snapshots__/runner.test.ts.snap +++ b/test/typescript/test/__snapshots__/runner.test.ts.snap @@ -86,3 +86,10 @@ TypeCheckError: Expected 1 arguments, but got 0. | ^ 5| })" `; + +exports[`should fail > typecheks with custom tsconfig 1`] = ` +"TypeCheckError: Expected 1 arguments, but got 0. +TypeCheckError: Expected 1 arguments, but got 0. +TypeCheckError: Expected 1 arguments, but got 0. +TypeCheckError: Expected 1 arguments, but got 0." +`; diff --git a/test/typescript/test/runner.test.ts b/test/typescript/test/runner.test.ts index fa7b369c54a1..a23d836c4fab 100644 --- a/test/typescript/test/runner.test.ts +++ b/test/typescript/test/runner.test.ts @@ -48,4 +48,46 @@ describe('should fail', async () => { } }) }, 30_000) + + it('typecheks with custom tsconfig', async () => { + const { stderr } = await execa('npx', [ + 'vitest', + 'typecheck', + '--run', + '--dir', + resolve(__dirname, '..', './failing'), + '--config', + resolve(__dirname, './vitest.custom.config.ts'), + ], { + cwd: root, + reject: false, + env: { + ...process.env, + CI: 'true', + NO_COLOR: 'true', + }, + }) + + expect(stderr).toBeTruthy() + const lines = String(stderr).split(/\n/g) + const msg = lines + .filter(i => i.includes('TypeCheckError: ')) + .reverse() + .join('\n') + .trim() + .replace(root, '') + expect(stderr).not.toMatch('files found, exiting with code') + // only one test file is failed, because only one is included in tsconfig + // + file with .only modifier + expect(msg).toMatchSnapshot() + + expect(stderr).toContain('FAIL fail.test-d.ts') // included in tsconfig + expect(stderr).toContain('FAIL only.test-d.ts') // .only + + // not included in tsconfig + expect(stderr).not.toContain('expect-error.test-d.ts') + expect(stderr).not.toContain('js-fail.test-d.js') + expect(stderr).not.toContain('js.test-d.js') + expect(stderr).not.toContain('test.test-d.ts') + }, 30_000) }) diff --git a/test/typescript/test/vitest.custom.config.ts b/test/typescript/test/vitest.custom.config.ts new file mode 100644 index 000000000000..fe059cc66808 --- /dev/null +++ b/test/typescript/test/vitest.custom.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + typecheck: { + allowJs: true, + include: ['**/*.test-d.*'], + tsconfig: '../tsconfig.custom.json', + }, + }, +}) diff --git a/test/typescript/tsconfig.custom.json b/test/typescript/tsconfig.custom.json new file mode 100644 index 000000000000..b0e4cb62238a --- /dev/null +++ b/test/typescript/tsconfig.custom.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "exclude": [ + "**/dist/**" + ], + "include": [ + "**/fail.test-d.ts" + ] +}