Skip to content

Commit

Permalink
fix: error formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Feb 16, 2024
1 parent 2a88496 commit e89497c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 48 deletions.
29 changes: 9 additions & 20 deletions src/options/default-options/testCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ import path from 'node:path';

import type { TestCaseResult } from '@jest/reporters';
import type {
AllureTestStepMetadata,
ExtractorContext,
ResolvedTestCaseCustomizer,
TestCaseCustomizer,
TestCaseExtractorContext,
} from 'jest-allure2-reporter';
import type {
Label,
ResolvedTestCaseCustomizer,
Stage,
Status,
StatusDetails,
AllureTestStepMetadata,
TestCaseCustomizer,
TestCaseExtractorContext,
} from 'jest-allure2-reporter';

import {
aggregateLabelCustomizers,
composeExtractors,
stripStatusDetails,
} from '../utils';
import { getStatusDetails } from '../../utils';

const identity = <T>(context: ExtractorContext<T>) => context.value;
const last = <T>(context: ExtractorContext<T[]>) => context.value?.at(-1);
Expand Down Expand Up @@ -70,7 +68,10 @@ export const testCase: ResolvedTestCaseCustomizer = {
testCaseMetadata.status ?? getTestCaseStatus(testCase),
statusDetails: ({ testCase, testCaseMetadata }) =>
stripStatusDetails(
testCaseMetadata.statusDetails ?? getTestCaseStatusDetails(testCase),
testCaseMetadata.statusDetails ??
stripStatusDetails(
getStatusDetails((testCase.failureMessages ?? []).join('\n')),
),
),
attachments: ({ testCaseMetadata }) => testCaseMetadata.attachments ?? [],
parameters: ({ testCaseMetadata }) => testCaseMetadata.parameters ?? [],
Expand Down Expand Up @@ -155,15 +156,3 @@ function getTestCaseStage(testCase: TestCaseResult): Stage {
}
}
}

function getTestCaseStatusDetails(
testCase: TestCaseResult,
): StatusDetails | undefined {
const message = (testCase.failureMessages ?? []).join('\n');
return message
? stripStatusDetails({
message: message.split('\n', 1)[0],
trace: message,
})
: undefined;
}
20 changes: 3 additions & 17 deletions src/options/default-options/testFile.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import fs from 'node:fs';
import path from 'node:path';

import type { TestResult } from '@jest/reporters';
import type {
ExtractorContext,
TestFileExtractorContext,
ResolvedTestFileCustomizer,
TestCaseCustomizer,
} from 'jest-allure2-reporter';
import type { Label, Link, StatusDetails } from 'jest-allure2-reporter';
import type { Label, Link } from 'jest-allure2-reporter';

import {
aggregateLabelCustomizers,
composeExtractors,
stripStatusDetails,
} from '../utils';
import { getStatusDetails } from '../../utils';

const identity = <T>(context: ExtractorContext<T>) => context.value;
const last = <T>(context: ExtractorContext<T[]>) => context.value?.at(-1);
Expand Down Expand Up @@ -42,7 +42,7 @@ export const testFile: ResolvedTestFileCustomizer = {
status: ({ testFile }) =>
testFile.testExecError == null ? 'passed' : 'broken',
statusDetails: ({ testFile }) =>
stripStatusDetails(getTestFileStatusDetails(testFile)),
stripStatusDetails(getStatusDetails(testFile.testExecError)),
attachments: ({ testFileMetadata }) => testFileMetadata.attachments ?? [],
parameters: ({ testFileMetadata }) => testFileMetadata.parameters ?? [],
labels: composeExtractors<Label[], TestFileExtractorContext<Label[]>>(
Expand All @@ -66,17 +66,3 @@ export const testFile: ResolvedTestFileCustomizer = {
links: ({ testFileMetadata }: TestFileExtractorContext<Link[]>) =>
testFileMetadata.links ?? [],
};

function getTestFileStatusDetails(
testFile: TestResult,
): StatusDetails | undefined {
const message =
testFile.testExecError?.stack || `${testFile.testExecError || ''}`;

return message
? stripStatusDetails({
message: message.split('\n', 2).join('\n'),
trace: message,
})
: undefined;
}
6 changes: 2 additions & 4 deletions src/runtime/__snapshots__/AllureRuntime.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ exports[`AllureRuntime should add attachments within the steps 1`] = `
"start": 1,
"status": "failed",
"statusDetails": {
"message": "Sync error",
"trace": "Test stack",
"message": "Error: Sync error",
},
"stop": 2,
},
Expand Down Expand Up @@ -81,8 +80,7 @@ exports[`AllureRuntime should add attachments within the steps 1`] = `
"start": 5,
"status": "broken",
"statusDetails": {
"message": "Async error",
"trace": "Test stack",
"message": "Error: Async error",
},
"stop": 6,
},
Expand Down
22 changes: 16 additions & 6 deletions src/utils/getStatusDetails.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { StatusDetails } from 'jest-allure2-reporter';

import { isError } from './isError';

export function getStatusDetails(
maybeError: unknown,
): StatusDetails | undefined {
if (maybeError) {
const error = maybeError as Error;
if (error.message) {
return {
message: error.message,
trace: error.stack,
};
}
const trace =
isError(maybeError) || typeof error === 'string'
? String(error)
: error.stack || error.message || JSON.stringify(error);

const stackIndex = trace.indexOf('\n at ');
return stackIndex === -1
? {
message: trace,
}
: {
message: trace.slice(0, stackIndex),
trace: ' ' + trace.slice(stackIndex).trimStart(),
};
}

return;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/isError.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function isError(error: unknown): error is Error {
export function isError(error: unknown): error is Error {
return error instanceof Error;
}

0 comments on commit e89497c

Please sign in to comment.