Skip to content

Commit

Permalink
chore: remove tmp, add expectTypeOf to globals, fix type errors count
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 2, 2022
1 parent 9dc7f7d commit 663dfb0
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 95 deletions.
1 change: 1 addition & 0 deletions packages/vitest/globals.d.ts
Expand Up @@ -3,6 +3,7 @@ declare global {
const test: typeof import('vitest')['test']
const describe: typeof import('vitest')['describe']
const it: typeof import('vitest')['it']
const expectTypeOf: typeof import('vitest')['expectTypeOf']
const expect: typeof import('vitest')['expect']
const assert: typeof import('vitest')['assert']
const vitest: typeof import('vitest')['vitest']
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/constants.ts
Expand Up @@ -36,6 +36,8 @@ export const globalApis = [
'chai',
'expect',
'assert',
// typecheck
'expectTypeOf',
// utils
'vitest',
'vi',
Expand Down
23 changes: 18 additions & 5 deletions packages/vitest/src/node/core.ts
Expand Up @@ -34,6 +34,7 @@ export class Vitest {
coverageProvider: CoverageProvider | null | undefined
logger: Logger
pool: WorkerPool | undefined
typechecker: Typechecker | undefined

vitenode: ViteNodeServer = undefined!

Expand Down Expand Up @@ -151,13 +152,21 @@ export class Vitest {
async typecheck() {
const testsFilesList = await this.globTestFiles()
const checker = new Typechecker(this, testsFilesList)
this.typechecker = checker
checker.onParseEnd(async ({ files, sourceErrors }) => {
await this.report('onCollected', files)
await this.report('onFinished', files)
if (!files.length)
this.logger.printNoTestFound()
else
await this.report('onFinished', files)
if (sourceErrors.length && !this.config.typecheck.ignoreSourceErrors) {
process.exitCode = 1
await this.logger.printSourceTypeErrors(sourceErrors)
}
if (!files.length) {
const exitCode = this.config.passWithNoTests ? 0 : 1
process.exit(exitCode)
}
if (this.config.watch) {
await this.report('onWatcherStart', files, [
...sourceErrors,
Expand All @@ -167,13 +176,16 @@ export class Vitest {
})
checker.onParseStart(async () => {
await this.report('onInit', this)
await this.report('onCollected', checker.getTestFiles())
const files = checker.getTestFiles()
if (files)
await this.report('onCollected', checker.getTestFiles())
})
checker.onWatcherRerun(async () => {
const { files } = checker.getResult()
await this.report('onWatcherRerun', files.map(f => f.filepath), 'File change detected. Triggering rerun.')
await this.report('onWatcherRerun', testsFilesList, 'File change detected. Triggering rerun.')
await checker.collectTests()
await this.report('onCollected', checker.getTestFiles())
const files = checker.getTestFiles()
if (files)
await this.report('onCollected', checker.getTestFiles())
})
await checker.collectTests()
await checker.start()
Expand Down Expand Up @@ -503,6 +515,7 @@ export class Vitest {
this.closingPromise = Promise.allSettled([
this.pool?.close(),
this.server.close(),
this.typechecker?.clean(),
].filter(Boolean)).then((results) => {
results.filter(r => r.status === 'rejected').forEach((err) => {
this.logger.error('error during close', (err as PromiseRejectedResult).reason)
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/node/reporters/base.ts
Expand Up @@ -240,9 +240,9 @@ export abstract class BaseReporter implements Reporter {
logger.log(padTitle('Test Files'), getStateString(files))
logger.log(padTitle('Tests'), getStateString(tests))
if (this.mode === 'typecheck') {
// has only failed types
const typechecks = getTests(tests).filter(t => t.type === 'typecheck')
logger.log(padTitle('Type Errors'), getStateString(typechecks, 'typechecks', false))
// has only failed checks
const typechecks = getTests(files).filter(t => t.type === 'typecheck')
logger.log(padTitle('Type Errors'), getStateString(typechecks, 'errors', false))
}
logger.log(padTitle('Start at'), formatTimeString(this._timeStart))
if (this.watchFilters)
Expand Down
6 changes: 3 additions & 3 deletions test/typescript/test.test-d.ts
@@ -1,4 +1,4 @@
import { describe, expectTypeOf, test } from 'vitest'
// import { describe, expectTypeOf, test } from 'vitest'

describe('test', () => {
test('some-test', () => {
Expand All @@ -10,7 +10,7 @@ describe('test', () => {

describe('test2', () => {
test('some-test 2', () => {
expectTypeOf(45).toBe('45')
expectTypeOf(45).toBe(45)
})
})
})
Expand All @@ -25,7 +25,7 @@ describe('test', () => {
// .toBe(45)

// expectTypeOf<never>().toBeNever()
// expectTypeOf('hren').toBe(45)
expectTypeOf('hren').toBe(45)
// expectTypeOf({ wolk: 'true' }).toHaveProperty('wol')
// expectTypeOf({ wolk: 'true' }).not.toHaveProperty('wol')
// expectTypeOf((v): v is boolean => true).asserts.toBe<boolean>()
Expand Down
6 changes: 6 additions & 0 deletions test/typescript/tsconfig.json
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["vitest/globals"]
}
}
80 changes: 0 additions & 80 deletions test/typescript/tsconfig.tmp.json

This file was deleted.

8 changes: 4 additions & 4 deletions test/typescript/vitest.config.ts
Expand Up @@ -4,10 +4,10 @@ export default defineConfig({
test: {
typecheck: {
checker: 'vue-tsc',
include: [
'**/*.test-d.ts',
'**/*.test-d.js',
],
// include: [
// // '**/*.test-d.ts',
// // '**/*.test-d.js',
// ],
allowJs: true,
},
},
Expand Down

0 comments on commit 663dfb0

Please sign in to comment.