Skip to content

Commit

Permalink
fix(helpers): simplify extractSourceCode
Browse files Browse the repository at this point in the history
Resolves #19
  • Loading branch information
noomorph committed May 4, 2024
1 parent b248944 commit 6e08a75
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions e2e/presets/code-snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};
Expand Down
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,13 @@ declare module 'jest-allure2-reporter' {
getFileNavigator(filePath: string | string[]): Promise<FileNavigator | undefined>;
/**
* 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<ExtractSourceCodeHelperResult | undefined>;
extractSourceCode(location: AllureTestItemSourceLocation | undefined, includeComments?: boolean): Promise<ExtractSourceCodeHelperResult | undefined>;
/**
* 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.
Expand Down
17 changes: 8 additions & 9 deletions src/options/helpers/extractSourceCode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {
AllureTestItemMetadata,
AllureTestItemSourceLocation,
ExtractSourceCodeHelperResult,
KeyedHelperCustomizer,
} from 'jest-allure2-reporter';
Expand All @@ -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<ExtractSourceCodeHelperResult | undefined> {
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) {
Expand Down

0 comments on commit 6e08a75

Please sign in to comment.