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(typecheck): correctly resolve custom tsconfig path #3342

Merged
merged 2 commits into from May 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions 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'
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
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
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
@@ -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
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"exclude": [
"**/dist/**"
],
"include": [
"**/fail.test-d.ts"
]
}