Skip to content

Commit c53ae07

Browse files
authoredMay 10, 2023
fix(typecheck): correctly resolve custom tsconfig path (#3342)
1 parent 2aff8c5 commit c53ae07

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed
 

‎packages/vitest/src/typecheck/parse.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import url from 'node:url'
22
import { writeFile } from 'node:fs/promises'
3-
import { dirname, join } from 'pathe'
3+
import { basename, dirname, join, resolve } from 'pathe'
44
import { getTsconfig as getTsconfigContent } from 'get-tsconfig'
55
import type { TypecheckConfig } from '../types'
66
import type { RawErrsMap, TscErrorInfo } from './types'
@@ -58,11 +58,12 @@ export async function makeTscErrorInfo(
5858
}
5959

6060
export async function getTsconfig(root: string, config: TypecheckConfig) {
61-
const configName = config.tsconfig?.includes('jsconfig.json')
62-
? 'jsconfig.json'
63-
: undefined
61+
const configName = config.tsconfig ? basename(config.tsconfig) : undefined
62+
const configSearchPath = config.tsconfig
63+
? dirname(resolve(root, config.tsconfig))
64+
: root
6465

65-
const tsconfig = getTsconfigContent(config.tsconfig || root, configName)
66+
const tsconfig = getTsconfigContent(configSearchPath, configName)
6667

6768
if (!tsconfig)
6869
throw new Error('no tsconfig.json found')

‎test/typescript/test/__snapshots__/runner.test.ts.snap

+7
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ TypeCheckError: Expected 1 arguments, but got 0.
8686
| ^
8787
5| })"
8888
`;
89+
90+
exports[`should fail > typecheks with custom tsconfig 1`] = `
91+
"TypeCheckError: Expected 1 arguments, but got 0.
92+
TypeCheckError: Expected 1 arguments, but got 0.
93+
TypeCheckError: Expected 1 arguments, but got 0.
94+
TypeCheckError: Expected 1 arguments, but got 0."
95+
`;

‎test/typescript/test/runner.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,46 @@ describe('should fail', async () => {
4848
}
4949
})
5050
}, 30_000)
51+
52+
it('typecheks with custom tsconfig', async () => {
53+
const { stderr } = await execa('npx', [
54+
'vitest',
55+
'typecheck',
56+
'--run',
57+
'--dir',
58+
resolve(__dirname, '..', './failing'),
59+
'--config',
60+
resolve(__dirname, './vitest.custom.config.ts'),
61+
], {
62+
cwd: root,
63+
reject: false,
64+
env: {
65+
...process.env,
66+
CI: 'true',
67+
NO_COLOR: 'true',
68+
},
69+
})
70+
71+
expect(stderr).toBeTruthy()
72+
const lines = String(stderr).split(/\n/g)
73+
const msg = lines
74+
.filter(i => i.includes('TypeCheckError: '))
75+
.reverse()
76+
.join('\n')
77+
.trim()
78+
.replace(root, '<rootDir>')
79+
expect(stderr).not.toMatch('files found, exiting with code')
80+
// only one test file is failed, because only one is included in tsconfig
81+
// + file with .only modifier
82+
expect(msg).toMatchSnapshot()
83+
84+
expect(stderr).toContain('FAIL fail.test-d.ts') // included in tsconfig
85+
expect(stderr).toContain('FAIL only.test-d.ts') // .only
86+
87+
// not included in tsconfig
88+
expect(stderr).not.toContain('expect-error.test-d.ts')
89+
expect(stderr).not.toContain('js-fail.test-d.js')
90+
expect(stderr).not.toContain('js.test-d.js')
91+
expect(stderr).not.toContain('test.test-d.ts')
92+
}, 30_000)
5193
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
typecheck: {
6+
allowJs: true,
7+
include: ['**/*.test-d.*'],
8+
tsconfig: '../tsconfig.custom.json',
9+
},
10+
},
11+
})

‎test/typescript/tsconfig.custom.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"exclude": [
4+
"**/dist/**"
5+
],
6+
"include": [
7+
"**/fail.test-d.ts"
8+
]
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.