Skip to content

Commit

Permalink
add fallback mechanism for source locations
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed May 3, 2024
1 parent d96cbc1 commit 0ce4335
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
3 changes: 2 additions & 1 deletion e2e/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = {
transform: {
'^.+\\.coffee$': path.join(__dirname, 'coffee-transformer.js'),
},
testEnvironment: 'jest-allure2-reporter/environment-node',
setupFilesAfterEnv: process.env.JEST_ENVIRONMENT ? ['<rootDir>/setup.js'] : [],
testEnvironment: process.env.JEST_ENVIRONMENT ?? 'jest-allure2-reporter/environment-node',
testMatch: ['<rootDir>/tests/**/*.js', '<rootDir>/tests/**/*.ts', '<rootDir>/tests/**/*.coffee'],
};
7 changes: 4 additions & 3 deletions e2e/presets/code-snippets.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const path = require('node:path');

const description = async ({ $, testCaseMetadata }) => {
const text = testCaseMetadata.description?.join('\n\n') ?? '';
const codes = await $.extractSourceCode(testCaseMetadata, true);
const description = async ({ $, testRunMetadata, testFileMetadata, testCaseMetadata }) => {
const metadata = testCaseMetadata ?? testFileMetadata ?? testRunMetadata;
const text = metadata.description?.join('\n\n') ?? '';
const codes = await $.extractSourceCode(metadata, true);
const snippets = codes.map($.source2markdown);
return [text, ...snippets].filter(t => t != null).join('\n\n');
};
Expand Down
6 changes: 4 additions & 2 deletions e2e/presets/remark.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const descriptionHtml = async ({ $, result }) => {
const descriptionHtml = async ({ $, value, result }) => {
const description = await result.description;
return description ? $.markdown2html(description) : '';
const html = await value;
const markdown = description ? await $.markdown2html(description) : '';
return [html, markdown].filter(Boolean).join('\n\n');
};

/** @type {import('jest-allure2-reporter').ReporterOptions} */
Expand Down
2 changes: 2 additions & 0 deletions e2e/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Object.assign(global, require('jest-allure2-reporter/api'));
delete global.default;
2 changes: 1 addition & 1 deletion src/reporter/JestAllure2Reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class JestAllure2Reporter extends JestMetadataReporter {
JestAllure2Reporter.query.testCaseResult(testCaseResult).lastInvocation!,
);

fallbacks.onTestCaseResult(testCaseMetadata);
fallbacks.onTestCaseResult(test, testCaseResult, testCaseMetadata);
}

async onTestFileResult(test: Test, testResult: TestResult, aggregatedResult: AggregatedResult) {
Expand Down
24 changes: 16 additions & 8 deletions src/reporter/fallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Test } from '@jest/reporters';
import type { Test, TestCaseResult } from '@jest/reporters';
import type { AllureTestItemMetadata, AllureTestFileMetadata } from 'jest-allure2-reporter';

import type { AllureMetadataProxy } from '../metadata';
Expand All @@ -7,7 +7,7 @@ import { ThreadService } from './ThreadService';

const threadService = new ThreadService();

export async function onTestFileStart(
export function onTestFileStart(
test: Test,
testFileMetadata: AllureMetadataProxy<AllureTestFileMetadata>,
) {
Expand All @@ -21,16 +21,24 @@ export async function onTestFileStart(
.push('labels', [{ name: 'thread', value: String(1 + threadId).padStart(2, '0') }]);
}

export async function onTestCaseResult(
export function onTestCaseResult(
test: Test,
testCase: TestCaseResult,
testCaseMetadata: AllureMetadataProxy<AllureTestItemMetadata>,
) {
const stop = testCaseMetadata.get('stop') ?? Number.NaN;
if (Number.isNaN(stop)) {
testCaseMetadata.set('stop', Date.now());
}
testCaseMetadata.defaults({
sourceLocation: testCase.location
? {
fileName: test.path,
lineNumber: testCase.location.line,
columnNumber: testCase.location.column + 1,
}
: undefined,
stop: Date.now(),
});
}

export async function onTestFileResult(
export function onTestFileResult(
test: Test,
testFileMetadata: AllureMetadataProxy<AllureTestFileMetadata>,
) {
Expand Down
11 changes: 9 additions & 2 deletions src/reporter/postProcessMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ export async function postProcessMetadata(
}

const allDescribeBlocks = [...testFile.allDescribeBlocks()];
const allHooks = allDescribeBlocks.flatMap((describeBlock) => [
const allHookDefinitions = allDescribeBlocks.flatMap((describeBlock) => [
...describeBlock.hookDefinitions(),
]);

const batch = [testFile, ...allDescribeBlocks, ...allHooks, ...testFile.allTestEntries()];
const batch = [
testFile,
...allDescribeBlocks,
...allHookDefinitions,
...testFile.allTestEntries(),
...testFile.allTestInvocations(),
...testFile.allInvocations(),
];

await Promise.all(
batch.map(async (metadata) => {
Expand Down

0 comments on commit 0ce4335

Please sign in to comment.