Skip to content

Commit

Permalink
refactor: remove unnecessary async/await (#5661)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed May 6, 2024
1 parent 6faf8f8 commit 2a80e95
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 69 deletions.
8 changes: 4 additions & 4 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface PrintErrorResult {
}

// use Logger with custom Console to capture entire error printing
export async function captuerPrintError(
export function capturePrintError(
error: unknown,
ctx: Vitest,
project: WorkspaceProject,
Expand All @@ -41,14 +41,14 @@ export async function captuerPrintError(
callback()
},
})
const result = await printError(error, project, {
const result = printError(error, project, {
showCodeFrame: false,
logger: new Logger(ctx, writable, writable),
})
return { nearest: result?.nearest, output }
}

export async function printError(error: unknown, project: WorkspaceProject | undefined, options: PrintErrorOptions): Promise<PrintErrorResult | undefined> {
export function printError(error: unknown, project: WorkspaceProject | undefined, options: PrintErrorOptions): PrintErrorResult | undefined {
const { showCodeFrame = true, fullStack = false, type } = options
const logger = options.logger
let e = error as ErrorWithDiff
Expand Down Expand Up @@ -140,7 +140,7 @@ export async function printError(error: unknown, project: WorkspaceProject | und

if (typeof e.cause === 'object' && e.cause && 'name' in e.cause) {
(e.cause as any).name = `Caused by: ${(e.cause as any).name}`
await printError(e.cause, project, { fullStack, showCodeFrame: false, logger: options.logger })
printError(e.cause, project, { fullStack, showCodeFrame: false, logger: options.logger })
}

handleImportOutsideModuleError(e.stack || e.stackStr || '', logger)
Expand Down
20 changes: 10 additions & 10 deletions packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export class Logger {
this.console.log(`${CURSOR_TO_START}${ERASE_DOWN}${log}`)
}

async printError(err: unknown, options: ErrorOptions = {}) {
printError(err: unknown, options: ErrorOptions = {}) {
const { fullStack = false, type } = options
const project = options.project ?? this.ctx.getCoreWorkspaceProject() ?? this.ctx.projects[0]
await printError(err, project, {
printError(err, project, {
fullStack,
type,
showCodeFrame: true,
Expand Down Expand Up @@ -195,28 +195,28 @@ export class Logger {
this.log()
}

async printUnhandledErrors(errors: unknown[]) {
printUnhandledErrors(errors: unknown[]) {
const errorMessage = c.red(c.bold(
`\nVitest caught ${errors.length} unhandled error${errors.length > 1 ? 's' : ''} during the test run.`
+ '\nThis might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.',
))
this.log(c.red(divider(c.bold(c.inverse(' Unhandled Errors ')))))
this.log(errorMessage)
await Promise.all(errors.map(async (err) => {
await this.printError(err, { fullStack: true, type: (err as ErrorWithDiff).type || 'Unhandled Error' })
}))
errors.forEach((err) => {
this.printError(err, { fullStack: true, type: (err as ErrorWithDiff).type || 'Unhandled Error' })
})
this.log(c.red(divider()))
}

async printSourceTypeErrors(errors: TypeCheckError[]) {
printSourceTypeErrors(errors: TypeCheckError[]) {
const errorMessage = c.red(c.bold(
`\nVitest found ${errors.length} error${errors.length > 1 ? 's' : ''} not related to your test files.`,
))
this.log(c.red(divider(c.bold(c.inverse(' Source Errors ')))))
this.log(errorMessage)
await Promise.all(errors.map(async (err) => {
await this.printError(err, { fullStack: true })
}))
errors.forEach((err) => {
this.printError(err, { fullStack: true })
})
this.log(c.red(divider()))
}
}
2 changes: 1 addition & 1 deletion packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
(await import('../../api/setup')).setup(ctx)
}
catch (err) {
await ctx.logger.printError(err, { fullStack: true })
ctx.logger.printError(err, { fullStack: true })
process.exit(1)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
await project.setServer(options, server)
}
catch (err) {
await project.ctx.logger.printError(err, { fullStack: true })
project.ctx.logger.printError(err, { fullStack: true })
process.exit(1)
}

Expand Down
35 changes: 17 additions & 18 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export abstract class BaseReporter implements Reporter {
return relativePath(this.ctx.config.root, path)
}

async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
this.end = performance.now()

await this.reportSummary(files, errors)
this.reportSummary(files, errors)
if (errors.length) {
if (!this.ctx.config.dangerouslyIgnoreUnhandledErrors)
process.exitCode = 1
Expand Down Expand Up @@ -103,7 +103,7 @@ export abstract class BaseReporter implements Reporter {
}
}

async onWatcherStart(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
onWatcherStart(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
this.resetLastRunLog()

const failed = errors.length > 0 || hasFailed(files)
Expand Down Expand Up @@ -156,7 +156,7 @@ export abstract class BaseReporter implements Reporter {
this.ctx.logger.logUpdate.clear()
}

async onWatcherRerun(files: string[], trigger?: string) {
onWatcherRerun(files: string[], trigger?: string) {
this.resetLastRunLog()
this.watchFilters = files

Expand Down Expand Up @@ -215,15 +215,15 @@ export abstract class BaseReporter implements Reporter {
)))
}

async reportSummary(files: File[], errors: unknown[]) {
await this.printErrorsSummary(files, errors)
reportSummary(files: File[], errors: unknown[]) {
this.printErrorsSummary(files, errors)
if (this.mode === 'benchmark')
await this.reportBenchmarkSummary(files)
this.reportBenchmarkSummary(files)
else
await this.reportTestSummary(files, errors)
this.reportTestSummary(files, errors)
}

async reportTestSummary(files: File[], errors: unknown[]) {
reportTestSummary(files: File[], errors: unknown[]) {
const tests = getTests(files)
const logger = this.ctx.logger

Expand Down Expand Up @@ -286,7 +286,7 @@ export abstract class BaseReporter implements Reporter {
logger.log()
}

private async printErrorsSummary(files: File[], errors: unknown[]) {
private printErrorsSummary(files: File[], errors: unknown[]) {
const logger = this.ctx.logger
const suites = getSuites(files)
const tests = getTests(files)
Expand All @@ -302,23 +302,23 @@ export abstract class BaseReporter implements Reporter {
if (failedSuites.length) {
logger.error(c.red(divider(c.bold(c.inverse(` Failed Suites ${failedSuites.length} `)))))
logger.error()
await this.printTaskErrors(failedSuites, errorDivider)
this.printTaskErrors(failedSuites, errorDivider)
}

if (failedTests.length) {
logger.error(c.red(divider(c.bold(c.inverse(` Failed Tests ${failedTests.length} `)))))
logger.error()

await this.printTaskErrors(failedTests, errorDivider)
this.printTaskErrors(failedTests, errorDivider)
}
if (errors.length) {
await logger.printUnhandledErrors(errors)
logger.printUnhandledErrors(errors)
logger.error()
}
return tests
}

async reportBenchmarkSummary(files: File[]) {
reportBenchmarkSummary(files: File[]) {
const logger = this.ctx.logger
const benches = getTests(files)

Expand Down Expand Up @@ -346,7 +346,7 @@ export abstract class BaseReporter implements Reporter {
}
}

private async printTaskErrors(tasks: Task[], errorDivider: () => void) {
private printTaskErrors(tasks: Task[], errorDivider: () => void) {
const errorsQueue: [error: ErrorWithDiff | undefined, tests: Task[]][] = []
for (const task of tasks) {
// merge identical errors
Expand Down Expand Up @@ -376,16 +376,15 @@ export abstract class BaseReporter implements Reporter {
this.ctx.logger.error(`${c.red(c.bold(c.inverse(' FAIL ')))} ${formatProjectName(projectName)}${name}`)
}
const project = this.ctx.getProjectByTaskId(tasks[0].id)
await this.ctx.logger.printError(error, { project })
this.ctx.logger.printError(error, { project })
errorDivider()
await Promise.resolve()
}
}

registerUnhandledRejection() {
const onUnhandledRejection = async (err: unknown) => {
process.exitCode = 1
await this.ctx.logger.printError(err, { fullStack: true, type: 'Unhandled Rejection' })
this.ctx.logger.printError(err, { fullStack: true, type: 'Unhandled Rejection' })
this.ctx.logger.error('\n\n')
process.exit(1)
}
Expand Down
18 changes: 9 additions & 9 deletions packages/vitest/src/node/reporters/benchmark/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export class TableReporter extends BaseReporter {
renderer?: ReturnType<typeof createTableRenderer>
rendererOptions: TableRendererOptions = {} as any

async onTestRemoved(trigger?: string) {
await this.stopListRender()
onTestRemoved(trigger?: string) {
this.stopListRender()
this.ctx.logger.clearScreen(c.yellow('Test removed...') + (trigger ? c.dim(` [ ${this.relative(trigger)} ]\n`) : ''), true)
const files = this.ctx.state.getFiles(this.watchFilters)
createTableRenderer(files, this.rendererOptions).stop()
this.ctx.logger.log()
await super.reportSummary(files, this.ctx.state.getUnhandledErrors())
super.reportSummary(files, this.ctx.state.getUnhandledErrors())
super.onWatcherStart()
}

Expand Down Expand Up @@ -69,9 +69,9 @@ export class TableReporter extends BaseReporter {
}

async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
await this.stopListRender()
this.stopListRender()
this.ctx.logger.log()
await super.onFinished(files, errors)
super.onFinished(files, errors)

// write output for future comparison
let outputFile = this.ctx.config.benchmark?.outputJson
Expand All @@ -87,17 +87,17 @@ export class TableReporter extends BaseReporter {
}

async onWatcherStart() {
await this.stopListRender()
this.stopListRender()
await super.onWatcherStart()
}

async stopListRender() {
await this.renderer?.stop()
stopListRender() {
this.renderer?.stop()
this.renderer = undefined
}

async onWatcherRerun(files: string[], trigger?: string) {
await this.stopListRender()
this.stopListRender()
await super.onWatcherRerun(files, trigger)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export function createTableRenderer(_tasks: Task[], options: TableRendererOption
update()
return this
},
async stop() {
stop() {
if (timer) {
clearInterval(timer)
timer = undefined
Expand Down
18 changes: 9 additions & 9 deletions packages/vitest/src/node/reporters/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export class DefaultReporter extends BaseReporter {
}

async onTestRemoved(trigger?: string) {
await this.stopListRender()
this.stopListRender()
this.ctx.logger.clearScreen(c.yellow('Test removed...') + (trigger ? c.dim(` [ ${this.relative(trigger)} ]\n`) : ''), true)
const files = this.ctx.state.getFiles(this.watchFilters)
createListRenderer(files, this.rendererOptions).stop()
this.ctx.logger.log()
await super.reportSummary(files, this.ctx.state.getUnhandledErrors())
super.reportSummary(files, this.ctx.state.getUnhandledErrors())
super.onWatcherStart()
}

Expand All @@ -43,24 +43,24 @@ export class DefaultReporter extends BaseReporter {
}
}

async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
await this.stopListRender()
onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
this.stopListRender()
this.ctx.logger.log()
await super.onFinished(files, errors)
super.onFinished(files, errors)
}

async onWatcherStart(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
await this.stopListRender()
this.stopListRender()
await super.onWatcherStart(files, errors)
}

async stopListRender() {
await this.renderer?.stop()
stopListRender() {
this.renderer?.stop()
this.renderer = undefined
}

async onWatcherRerun(files: string[], trigger?: string) {
await this.stopListRender()
this.stopListRender()
await super.onWatcherRerun(files, trigger)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/reporters/dot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class DotReporter extends BaseReporter {
async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
await this.stopListRender()
this.ctx.logger.log()
await super.onFinished(files, errors)
super.onFinished(files, errors)
}

async onWatcherStart() {
Expand All @@ -34,7 +34,7 @@ export class DotReporter extends BaseReporter {

async onWatcherRerun(files: string[], trigger?: string) {
await this.stopListRender()
await super.onWatcherRerun(files, trigger)
super.onWatcherRerun(files, trigger)
}

onUserConsoleLog(log: UserConsoleLog) {
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/node/reporters/github-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getTasks } from '@vitest/runner/utils'
import stripAnsi from 'strip-ansi'
import type { File, Reporter, Vitest } from '../../types'
import { getFullName } from '../../utils'
import { captuerPrintError } from '../error'
import { capturePrintError } from '../error'
import type { WorkspaceProject } from '../workspace'

export class GithubActionsReporter implements Reporter {
Expand All @@ -12,7 +12,7 @@ export class GithubActionsReporter implements Reporter {
this.ctx = ctx
}

async onFinished(files: File[] = [], errors: unknown[] = []) {
onFinished(files: File[] = [], errors: unknown[] = []) {
// collect all errors and associate them with projects
const projectErrors = new Array<{ project: WorkspaceProject; title: string; error: unknown }>()
for (const error of errors) {
Expand Down Expand Up @@ -42,7 +42,7 @@ export class GithubActionsReporter implements Reporter {

// format errors via `printError`
for (const { project, title, error } of projectErrors) {
const result = await captuerPrintError(error, this.ctx, project)
const result = capturePrintError(error, this.ctx, project)
const stack = result?.nearest
if (!stack)
continue
Expand Down
Loading

0 comments on commit 2a80e95

Please sign in to comment.