From 328c1680fc78c0b821001b6246fd172dc6ee110b Mon Sep 17 00:00:00 2001 From: Dmitrii Gridnev Date: Thu, 29 Aug 2024 16:47:25 +0200 Subject: [PATCH] feature: improve error collection The error stack trace contains more useful debugging information: browser, code, etc. --- qase-testcafe/changelog.md | 6 +++++ qase-testcafe/package.json | 2 +- qase-testcafe/src/factory.ts | 16 +++++++++++-- qase-testcafe/src/reporter.ts | 44 +++++++++++------------------------ 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/qase-testcafe/changelog.md b/qase-testcafe/changelog.md index 34b3befd..74c48844 100644 --- a/qase-testcafe/changelog.md +++ b/qase-testcafe/changelog.md @@ -1,3 +1,9 @@ +# qase-testcafe@2.0.2 + +## What's new + +Improved error collection. The error stack trace contains more useful debugging information: browser, code, etc. + # qase-testcafe@2.0.1 ## What's new diff --git a/qase-testcafe/package.json b/qase-testcafe/package.json index 613e928d..48393930 100644 --- a/qase-testcafe/package.json +++ b/qase-testcafe/package.json @@ -1,6 +1,6 @@ { "name": "testcafe-reporter-qase", - "version": "2.0.1", + "version": "2.0.2", "description": "Qase TMS TestCafe Reporter", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/qase-testcafe/src/factory.ts b/qase-testcafe/src/factory.ts index 8df4eafd..d3ab31a3 100644 --- a/qase-testcafe/src/factory.ts +++ b/qase-testcafe/src/factory.ts @@ -15,8 +15,20 @@ export const factory = (options: TestcafeQaseOptionsType) => { reportFixtureStart: () => { /* empty */ }, - reportTestDone: async (name: string, testRunInfo: TestRunInfoType, meta: Record) => { - await reporter.reportTestDone(name, testRunInfo, meta); + async reportTestDone( + name: string, + testRunInfo: TestRunInfoType, + meta: Record + ): Promise { + return reporter.reportTestDone( + name, + testRunInfo, + meta, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error Inject testrail error formatting method with bound context + // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument + this.formatError.bind(this) + ); }, reportTaskDone: async () => { await reporter.reportTaskDone(); diff --git a/qase-testcafe/src/reporter.ts b/qase-testcafe/src/reporter.ts index 1886eca5..2085fe59 100644 --- a/qase-testcafe/src/reporter.ts +++ b/qase-testcafe/src/reporter.ts @@ -23,10 +23,11 @@ interface TestRunErrorFormattableAdapterType { screenshotPath: string; testRunId: string; testRunPhase: string; + type: string; code?: string; isTestCafeError?: boolean; callsite?: CallsiteRecordType; - errMsg?: string; + errMsg: string; diff?: boolean; id?: string; } @@ -96,32 +97,6 @@ export class TestcafeQaseReporter { return TestStatusEnum.passed; } - /** - * @param {TestRunErrorFormattableAdapterType[]} errors - * @returns {Error} - * @private - */ - private static transformErrors(errors: TestRunErrorFormattableAdapterType[]): Error { - const [errorMessages, errorStacks] = errors.reduce<[string[], string[]]>( - ([messages, stacks], error) => { - const stack = - error.callsite?.stackFrames?.map((line) => String(line)) ?? []; - - messages.push(error.errMsg ?? 'Error'); - stacks.push(stack.join('\n')); - - return [messages, stacks]; - }, - [[], []], - ); - - const error = new Error(errorMessages.join('\n\n')); - - error.stack = errorStacks.join('\n\n'); - - return error; - } - /** * @param {ScreenshotType[]} screenshots * @returns {Attachment[]} @@ -179,13 +154,22 @@ export class TestcafeQaseReporter { * @param {string} title * @param {TestRunInfoType} testRunInfo * @param {Record} meta + * @param formatError */ public reportTestDone = async ( title: string, testRunInfo: TestRunInfoType, meta: Record, + formatError: (error: any, prefix: string) => string, ) => { - const error = TestcafeQaseReporter.transformErrors(testRunInfo.errs); + const errorLog = testRunInfo.errs + .map((error, index) => formatError(error, `${index + 1} `).replace( + // eslint-disable-next-line no-control-regex + /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, + '', + )) + .join('\n'); + const metadata = this.getMeta(meta); await this.reporter.addTestResult({ author: null, @@ -194,11 +178,11 @@ export class TestcafeQaseReporter { start_time: null, end_time: null, duration: testRunInfo.durationMs, - stacktrace: error.stack ?? null, + stacktrace: errorLog, thread: null, }, fields: metadata[metadataEnum.fields], - message: error.message, + message: errorLog ? errorLog.split('\n')[0] ?? '' : '', muted: false, params: metadata[metadataEnum.parameters], group_params: metadata[metadataEnum.groupParameters],