From bb84cc867186cbc653754a3225d6d966f8c31c61 Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Fri, 17 May 2024 16:07:07 +0300 Subject: [PATCH] fix: increase error resilience --- package.json | 2 +- src/jest-reporter/ReporterServer.ts | 57 +++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 385230b..ed83e4d 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,7 @@ "dependencies": { "bunyamin": "^1.5.2", "funpermaproxy": "^1.1.0", - "jest-environment-emit": "^1.0.6", + "jest-environment-emit": "^1.0.8", "lodash.merge": "^4.6.2", "lodash.snakecase": "^4.1.1", "node-ipc": "9.2.1", diff --git a/src/jest-reporter/ReporterServer.ts b/src/jest-reporter/ReporterServer.ts index 2c1f97d..b23dbec 100644 --- a/src/jest-reporter/ReporterServer.ts +++ b/src/jest-reporter/ReporterServer.ts @@ -72,13 +72,24 @@ export class ReporterServer { } async onRunStart(): Promise { - await this.#ipc.start(); + this.#log.debug.begin(__REPORTER(''), 'test run'); + + try { + await this.#ipc.start(); + } catch (error) { + this.#log.error(error, 'Caught unhandled error in onRunStart'); + } } onTestFileStart(testPath: string): void { this.#log.debug.begin(__REPORTER(testPath), __FILE(this.#rootDir, testPath)); - const testFileMetadata = this.#fallbackAPI.reportTestFile(testPath); - this.#associate.filePath(testPath, testFileMetadata); + + try { + const testFileMetadata = this.#fallbackAPI.reportTestFile(testPath); + this.#associate.filePath(testPath, testFileMetadata); + } catch (error) { + this.#log.error(error, 'Caught unhandled error in onTestFileStart'); + } } onTestCaseStart(testPath: string, testCaseStartInfo: unknown): void { @@ -92,26 +103,40 @@ export class ReporterServer { onTestCaseResult(testPath: string, testCaseResult: TestCaseResult): void { this.#log.debug(__REPORTER(testPath), 'onTestCaseResult'); - const lastTestEntry = this.#fallbackAPI.reportTestCase(testPath, testCaseResult); - this.#associate.testCaseResult(testCaseResult, lastTestEntry); + try { + const lastTestEntry = this.#fallbackAPI.reportTestCase(testPath, testCaseResult); + this.#associate.testCaseResult(testCaseResult, lastTestEntry); + } catch (error: unknown) { + this.#log.error(error, 'Caught unhandled error in onTestCaseResult'); + } } onTestFileResult(testPath: string, testResult: TestResult): void { - const allTestEntries = this.#fallbackAPI.reportTestFileResult(testResult); - const testResults = testResult.testResults; - for (let i = 0; i < testResults.length; i++) { - this.#associate.testCaseResult(testResults[i], allTestEntries[i]); + try { + const allTestEntries = this.#fallbackAPI.reportTestFileResult(testResult); + const testResults = testResult.testResults; + for (let i = 0; i < testResults.length; i++) { + this.#associate.testCaseResult(testResults[i], allTestEntries[i]); + } + } catch (error: unknown) { + this.#log.error(error, 'Caught unhandled error in onTestFileResult'); + } finally { + this.#log.debug.end(__REPORTER(testPath)); } - - this.#log.debug.end(__REPORTER(testPath)); } async onRunComplete(): Promise { - await this.#ipc.stop(); - - if (process.env.JEST_BUNYAMIN_DIR) { - await new Promise((resolve) => setTimeout(resolve, 1000)); - await aggregateLogs(); + try { + await this.#ipc.stop(); + } catch (error: unknown) { + this.#log.error(error, 'Caught unhandled error in onRunComplete'); + } finally { + this.#log.debug.end(__REPORTER('')); + + if (process.env.JEST_BUNYAMIN_DIR) { + await new Promise((resolve) => setTimeout(resolve, 1000)); + await aggregateLogs(); + } } } }