Skip to content

Commit

Permalink
fix(typecheck): correctly resolve custom tsconfig path (#3342)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored May 10, 2023
1 parent 2aff8c5 commit c53ae07
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/vitest/src/typecheck/parse.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions test/typescript/test/__snapshots__/runner.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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."
`;
42 changes: 42 additions & 0 deletions test/typescript/test/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<rootDir>')
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)
})
11 changes: 11 additions & 0 deletions test/typescript/test/vitest.custom.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
typecheck: {
allowJs: true,
include: ['**/*.test-d.*'],
tsconfig: '../tsconfig.custom.json',
},
},
})
9 changes: 9 additions & 0 deletions test/typescript/tsconfig.custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"exclude": [
"**/dist/**"
],
"include": [
"**/fail.test-d.ts"
]
}

0 comments on commit c53ae07

Please sign in to comment.