Skip to content

Commit a1da571

Browse files
authoredMay 24, 2023
fix(typecheck): show tsc errors not related to test files (#3441)
1 parent 1668179 commit a1da571

File tree

6 files changed

+83
-9
lines changed

6 files changed

+83
-9
lines changed
 

‎packages/vitest/src/node/workspace.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,18 @@ export class WorkspaceProject {
209209
this.ctx.state.collectFiles(checker.getTestFiles())
210210
await this.report('onTaskUpdate', checker.getTestPacks())
211211
await this.report('onCollected')
212+
const failedTests = hasFailed(files)
213+
const exitCode = !failedTests && checker.getExitCode()
214+
if (exitCode) {
215+
const error = new Error(checker.getOutput())
216+
error.stack = ''
217+
this.ctx.state.catchError(error, 'Typecheck Error')
218+
}
212219
if (!files.length) {
213220
this.ctx.logger.printNoTestFound()
214221
}
215222
else {
216-
if (hasFailed(files))
223+
if (failedTests)
217224
process.exitCode = 1
218225
await this.report('onFinished', files)
219226
}
@@ -224,6 +231,7 @@ export class WorkspaceProject {
224231
// if there are source errors, we are showing it, and then terminating process
225232
if (!files.length) {
226233
const exitCode = this.config.passWithNoTests ? (process.exitCode ?? 0) : 1
234+
await this.close()
227235
process.exit(exitCode)
228236
}
229237
if (this.config.watch) {

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

+17-8
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ export class Typechecker {
3737
sourceErrors: [],
3838
}
3939

40+
private _output = ''
4041
private _tests: Record<string, FileInformation> | null = {}
4142
private tempConfigPath?: string
4243
private allowJs?: boolean
43-
private process!: ExecaChildProcess
44+
private process?: ExecaChildProcess
4445

4546
constructor(protected ctx: WorkspaceProject, protected files: string[]) { }
4647

@@ -221,6 +222,14 @@ export class Typechecker {
221222
this.allowJs = typecheck.allowJs || config.allowJs || false
222223
}
223224

225+
public getExitCode() {
226+
return this.process?.exitCode != null && this.process.exitCode
227+
}
228+
229+
public getOutput() {
230+
return this._output
231+
}
232+
224233
public async start() {
225234
if (!this.tempConfigPath)
226235
throw new Error('tsconfig was not initialized')
@@ -233,7 +242,7 @@ export class Typechecker {
233242
args.push('--watch')
234243
if (typecheck.allowJs)
235244
args.push('--allowJs', '--checkJs')
236-
let output = ''
245+
this._output = ''
237246
const child = execa(typecheck.checker, args, {
238247
cwd: root,
239248
stdout: 'pipe',
@@ -243,28 +252,28 @@ export class Typechecker {
243252
await this._onParseStart?.()
244253
let rerunTriggered = false
245254
child.stdout?.on('data', (chunk) => {
246-
output += chunk
255+
this._output += chunk
247256
if (!watch)
248257
return
249-
if (output.includes('File change detected') && !rerunTriggered) {
258+
if (this._output.includes('File change detected') && !rerunTriggered) {
250259
this._onWatcherRerun?.()
251260
this._result.sourceErrors = []
252261
this._result.files = []
253262
this._tests = null // test structure might've changed
254263
rerunTriggered = true
255264
}
256-
if (/Found \w+ errors*. Watching for/.test(output)) {
265+
if (/Found \w+ errors*. Watching for/.test(this._output)) {
257266
rerunTriggered = false
258-
this.prepareResults(output).then((result) => {
267+
this.prepareResults(this._output).then((result) => {
259268
this._result = result
260269
this._onParseEnd?.(result)
261270
})
262-
output = ''
271+
this._output = ''
263272
}
264273
})
265274
if (!watch) {
266275
await child
267-
this._result = await this.prepareResults(output)
276+
this._result = await this.prepareResults(this._output)
268277
await this._onParseEnd?.(this._result)
269278
}
270279
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ TypeCheckError: Expected 1 arguments, but got 0.
8787
5| })"
8888
`;
8989
90+
exports[`should fail > typechecks empty "include" but with tests 1`] = `
91+
"Testing types with tsc and vue-tsc is an experimental feature.
92+
Breaking changes might not follow semver, please pin Vitest's version when using it.
93+
94+
⎯⎯⎯⎯⎯⎯ Typecheck Error ⎯⎯⎯⎯⎯⎯⎯
95+
Error: error TS18003: No inputs were found in config file '<root>/tsconfig.vitest-temp.json'. Specified 'include' paths were '[\\"src\\"]' and 'exclude' paths were '[\\"**/dist/**\\",\\"./dist\\"]'.
96+
97+
"
98+
`;
99+
100+
exports[`should fail > typechecks with custom tsconfig 1`] = `
101+
"TypeCheckError: Expected 1 arguments, but got 0.
102+
TypeCheckError: Expected 1 arguments, but got 0.
103+
TypeCheckError: Expected 1 arguments, but got 0.
104+
TypeCheckError: Expected 1 arguments, but got 0."
105+
`;
106+
90107
exports[`should fail > typecheks with custom tsconfig 1`] = `
91108
"TypeCheckError: Expected 1 arguments, but got 0.
92109
TypeCheckError: Expected 1 arguments, but got 0.

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

+21
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,25 @@ describe('should fail', async () => {
7474
expect(stderr).not.toContain('js.test-d.js')
7575
expect(stderr).not.toContain('test.test-d.ts')
7676
}, 30_000)
77+
78+
it('typechecks empty "include" but with tests', async () => {
79+
const { stderr } = await runVitestCli(
80+
{
81+
cwd: root,
82+
env: {
83+
...process.env,
84+
CI: 'true',
85+
NO_COLOR: 'true',
86+
},
87+
},
88+
'typecheck',
89+
'--run',
90+
'--dir',
91+
resolve(__dirname, '..', './failing'),
92+
'--config',
93+
resolve(__dirname, './vitest.empty.config.ts'),
94+
)
95+
96+
expect(stderr.replace(resolve(__dirname, '..'), '<root>')).toMatchSnapshot()
97+
}, 30_000)
7798
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
typecheck: {
6+
include: ['**/fail.test-d.ts'],
7+
tsconfig: '../tsconfig.empty.json',
8+
},
9+
},
10+
})

‎test/typescript/tsconfig.empty.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+
"src"
8+
]
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.