From 6e08a75f18c68873600df8d790c8fe041ac84ef5 Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Sat, 4 May 2024 16:03:28 +0300 Subject: [PATCH] fix(helpers): simplify extractSourceCode Resolves #19 --- e2e/presets/code-snippets.js | 4 ++-- index.d.ts | 6 +++--- src/options/helpers/extractSourceCode.ts | 17 ++++++++--------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/e2e/presets/code-snippets.js b/e2e/presets/code-snippets.js index 5d6149c..ef29178 100644 --- a/e2e/presets/code-snippets.js +++ b/e2e/presets/code-snippets.js @@ -6,8 +6,8 @@ const description = async ({ $, testRunMetadata, testFileMetadata, testCaseMetad const steps = metadata.steps || []; const before = steps.filter(s => s.hookType?.startsWith('before')); const after = steps.filter(s => s.hookType?.startsWith('after')); - const allMetadata = [...before, metadata, ...after]; - const codes = await Promise.all(allMetadata.map(m => $.extractSourceCode(m, true))); + const locations = [...before, metadata, ...after].map(m => m.sourceLocation); + const codes = await Promise.all(locations.map(loc => $.extractSourceCode(loc, true))); const snippets = codes.filter(Boolean).map($.source2markdown); return [text, ...snippets].filter(t => t != null).join('\n\n'); }; diff --git a/index.d.ts b/index.d.ts index 4eec7bf..0e69015 100644 --- a/index.d.ts +++ b/index.d.ts @@ -402,13 +402,13 @@ declare module 'jest-allure2-reporter' { getFileNavigator(filePath: string | string[]): Promise; /** * Extracts the source code of the current test case or step. - * @param metadata - the metadata object of the test case or step. + * @param location - the location of the source code to extract. * @param includeComments - whether to include comments before the actual code. * @returns the extracted source code or undefined if the source code is not found. * @example - * ({ $, testFileMetadata }) => $.extractSourceCode(testFileMetadata) + * ({ $, testFileMetadata }) => $.extractSourceCode(testFileMetadata.sourceLocation) */ - extractSourceCode(metadata: AllureTestItemMetadata, includeComments?: boolean): Promise; + extractSourceCode(location: AllureTestItemSourceLocation | undefined, includeComments?: boolean): Promise; /** * Extracts the manifest of the current project or a specific package. * Pass a callback to extract specific data from the manifest – this way you can omit async/await. diff --git a/src/options/helpers/extractSourceCode.ts b/src/options/helpers/extractSourceCode.ts index 82734c0..1ddb277 100644 --- a/src/options/helpers/extractSourceCode.ts +++ b/src/options/helpers/extractSourceCode.ts @@ -1,5 +1,5 @@ import type { - AllureTestItemMetadata, + AllureTestItemSourceLocation, ExtractSourceCodeHelperResult, KeyedHelperCustomizer, } from 'jest-allure2-reporter'; @@ -14,25 +14,24 @@ export const extractSourceCode: KeyedHelperCustomizer<'extractSourceCode'> = ({ const config = reporterConfig as ReporterConfig; return async function extractSourceCodeHelper( - item: AllureTestItemMetadata, + location: AllureTestItemSourceLocation, includeComments = false, ): Promise { - let result: ExtractSourceCodeHelperResult = {}; + if (isEmpty(location)) { + return undefined; + } - const context = item.sourceLocation; const plugins = config.sourceCode ? Object.values(config.sourceCode.plugins) : []; - if (isEmpty(context)) { - return undefined; - } + let result: ExtractSourceCodeHelperResult = {}; for (const p of plugins) { try { - result = defaults(result, await p.extractSourceCode?.(context, includeComments)); + result = defaults(result, await p.extractSourceCode?.(location, includeComments)); } catch (error: unknown) { log.warn( error, - `Plugin "${p.name}" failed to extract source code for ${context.fileName}:${context.lineNumber}:${context.columnNumber}`, + `Plugin "${p.name}" failed to extract source code for ${location.fileName}:${location.lineNumber}:${location.columnNumber}`, ); } if (result?.code) {