From b2ae7abb07e21325cf9b6c0adfdee14179be0180 Mon Sep 17 00:00:00 2001 From: Ryan Parker Date: Tue, 7 Jul 2020 20:01:57 -0500 Subject: [PATCH] feat(allurereporter): accepting environmentInfo and jiraUrl from jest environmentt options --- src/allure-node-environment.ts | 6 +-- src/allure-reporter.ts | 73 +++++++++++++++++++++------------- src/jest-allure-interface.ts | 6 ++- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/allure-node-environment.ts b/src/allure-node-environment.ts index 1201f48..75cafb1 100644 --- a/src/allure-node-environment.ts +++ b/src/allure-node-environment.ts @@ -12,7 +12,7 @@ export default class AllureNodeEnvironment extends NodeEnvironment { constructor(config: Config.ProjectConfig, context: EnvironmentContext) { super(config); - const allureConfig: IAllureConfig = {resultsDir: 'allure-results', ...config.testEnvironmentOptions}; + const allureConfig: IAllureConfig = {resultsDir: 'allure-results'}; this.docblockPragmas = context.docblockPragmas; this.testPath = context.testPath ? context.testPath.replace(config.rootDir, '') : ''; @@ -25,12 +25,12 @@ export default class AllureNodeEnvironment extends NodeEnvironment { this.testPath = this.testPath.split('__tests__/')[1]; } - this.reporter = new AllureReporter(new AllureRuntime(allureConfig)); + this.reporter = new AllureReporter(new AllureRuntime(allureConfig), config.testEnvironmentOptions?.enironmentInfo, config.testEnvironmentOptions?.jiraUrl); this.global.allure = this.reporter.getImplementation(); if (this.docblockPragmas?.prototype !== undefined) { - console.log(this.docblockPragmas); + console.log('this.docblockPragmas:', this.docblockPragmas); } } diff --git a/src/allure-reporter.ts b/src/allure-reporter.ts index 380c55e..a27b9e2 100644 --- a/src/allure-reporter.ts +++ b/src/allure-reporter.ts @@ -22,11 +22,15 @@ export default class AllureReporter { private readonly suites: AllureGroup[] = []; private readonly steps: AllureStep[] = []; private runningTest: AllureTest | null = null; + private readonly jiraUrl: string; - constructor(private readonly allureRuntime: AllureRuntime) { } + constructor(private readonly allureRuntime: AllureRuntime, environmentInfo: Record, jiraUrl: string) { + this.environmentInfo(environmentInfo); + this.jiraUrl = jiraUrl; + } public getImplementation(): JestAllureInterface { - return new JestAllureInterface(this, this.allureRuntime); + return new JestAllureInterface(this, this.allureRuntime, this.jiraUrl); } get currentSuite(): AllureGroup | null { @@ -82,10 +86,9 @@ export default class AllureReporter { this.currentExecutable.stage = Stage.FINISHED; if (error) { - this.currentExecutable.status = Status.FAILED; + const {status, message, trace} = this.handleError(error); - const message = stripAnsi(error.message); - const trace = stripAnsi(error.stack ?? '').replace(message, ''); + this.currentExecutable.status = status; this.currentExecutable.statusDetails = {message, trace}; } else { @@ -161,29 +164,7 @@ export default class AllureReporter { } } - // If (error.matcherResult) { - // console.log('error.matcherResult:', error.matcherResult); - // } else { - // console.log('error:', error); - // } - - const status = error.matcherResult ? Status.FAILED : Status.BROKEN; - - const isSnapshotFailure = error.matcherResult?.name === 'toMatchSnapshot'; - - let message: string; - let trace: string; - - if (isSnapshotFailure) { - const [matcherHint, ...snapshotDiff] = stripAnsi(error.matcherResult.message()).split('@@'); - - message = matcherHint; - trace = snapshotDiff.join(''); - // Console.log({message, trace}); - } else { - message = stripAnsi(error.message); - trace = stripAnsi(error.stack ?? '').replace(message, ''); - } + const {status, message, trace} = this.handleError(error); this.endTest(status, {message, trace}); } @@ -223,6 +204,42 @@ export default class AllureReporter { this.currentTest = null; } + private handleError(error: Error | any) { + if (error.matcherResult) { + console.log('error.matcherResult:', error.matcherResult); + } else { + console.log('error:', error); + } + + const status = error.matcherResult ? Status.FAILED : Status.BROKEN; + + const isSnapshotFailure = error.matcherResult?.name === 'toMatchSnapshot'; + + let message: string; + let trace: string; + + if (isSnapshotFailure) { + const [matcherHint, ...snapshotDiff] = stripAnsi(error.matcherResult.message()).split('@@'); + + message = matcherHint; + trace = snapshotDiff.join(''); + } else { + message = stripAnsi(error.message); + trace = stripAnsi(error.stack ?? '').replace(message, ''); + } + + if (!message && status) { + message = status; + trace = ''; + } + + return { + status, + message, + trace + }; + } + private collectTestParentNames( parent: jest.Circus.TestEntry | jest.Circus.DescribeBlock | undefined ) { diff --git a/src/jest-allure-interface.ts b/src/jest-allure-interface.ts index 78c2c8a..68513c2 100644 --- a/src/jest-allure-interface.ts +++ b/src/jest-allure-interface.ts @@ -17,14 +17,16 @@ import type AllureReporter from './allure-reporter'; import StepWrapper from './step-wrapper'; export default class JestAllureInterface extends Allure { - public jiraUrl = ''; + public jiraUrl: string; public tmsUrl = ''; constructor( private readonly reporter: AllureReporter, - runtime: AllureRuntime + runtime: AllureRuntime, + jiraUrl?: string ) { super(runtime); + this.jiraUrl = jiraUrl ?? ''; } public get currentExecutable(): AllureStep | AllureTest | ExecutableItemWrapper {