From c6c184f00c77ac06239a56ef9639381ea02a291f Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Sat, 6 Apr 2024 19:38:28 +0300 Subject: [PATCH] so it runs --- e2e/jest.config.js | 10 +- index.d.ts | 14 +-- src/metadata/squasher/MetadataSquasher.ts | 2 +- src/options/common/compositeExtractor.ts | 11 +-- src/options/common/last.ts | 11 ++- src/options/common/mergerExtractor.ts | 4 +- src/options/custom/__utils__/contexts.ts | 12 ++- .../custom/labels/simplifyLabelsMap.ts | 2 +- src/options/custom/testCaseSteps.test.ts | 38 +++++--- src/options/custom/testCaseSteps.ts | 67 ++++++------- src/options/custom/testStep.test.ts | 6 +- src/options/custom/testStep.ts | 2 +- src/options/index.ts | 19 +++- src/options/types.ts | 40 ++++++-- src/reporter/JestAllure2Reporter.ts | 97 +++++++++++-------- src/reporter/resolveTestItem.ts | 57 +++++++++++ src/utils/resolvePromisedProperties.ts | 19 ---- 17 files changed, 263 insertions(+), 148 deletions(-) create mode 100644 src/reporter/resolveTestItem.ts delete mode 100644 src/utils/resolvePromisedProperties.ts diff --git a/e2e/jest.config.js b/e2e/jest.config.js index dd15de1..c47028f 100644 --- a/e2e/jest.config.js +++ b/e2e/jest.config.js @@ -27,8 +27,14 @@ const jestAllure2ReporterOptions = { 'package.version': await $.manifest('', 'version'), }); }, + testRun: { + ignored: false, + }, + testFile: { + ignored: false, + }, testCase: { - name: ({ testCase }) => + displayName: ({ testCase }) => [...testCase.ancestorTitles, testCase.title].join(' ยป '), labels: { parentSuite: ({ filePath }) => filePath[0], @@ -43,7 +49,7 @@ const jestAllure2ReporterOptions = { owner: ({ value }) => value ?? 'Unknown', }, links: { - issue: ({ value }) => ({ ...value, url: `https://youtrack.jetbrains.com/issue/${value.url}/` }), + issue: 'https://youtrack.jetbrains.com/issue/{{name}}', }, }, }; diff --git a/index.d.ts b/index.d.ts index be2c4bb..8c0f980 100644 --- a/index.d.ts +++ b/index.d.ts @@ -80,7 +80,7 @@ declare module 'jest-allure2-reporter' { /** * Customize how individual test steps are reported. */ - testStep?: TestStepCustomizer; + testStep?: TestStepCustomizer; } export interface ReporterConfig extends ReporterOptionsAugmentation { @@ -244,7 +244,7 @@ declare module 'jest-allure2-reporter' { * Global customizations for how test steps are reported, e.g. * beforeAll, beforeEach, afterEach, afterAll hooks and custom steps. */ - export interface TestStepCustomizer { + export interface TestStepCustomizer { /** * Extractor to omit test steps from the report. */ @@ -395,12 +395,12 @@ declare module 'jest-allure2-reporter' { aggregatedResult: AggregatedResult; filePath: string[]; testRunMetadata: AllureTestRunMetadata; - testFile: TestResult; - testFileMetadata: AllureTestFileMetadata; - testCase: TestCaseResult; - testCaseMetadata: AllureTestCaseMetadata; + testFile?: TestResult; + testFileMetadata?: AllureTestFileMetadata; + testCase?: TestCaseResult; + testCaseMetadata?: AllureTestCaseMetadata; testStepMetadata: AllureTestStepMetadata; - result: Partial>; + result: Partial>; } export interface Helpers extends HelpersAugmentation { diff --git a/src/metadata/squasher/MetadataSquasher.ts b/src/metadata/squasher/MetadataSquasher.ts index 63582ff..4989469 100644 --- a/src/metadata/squasher/MetadataSquasher.ts +++ b/src/metadata/squasher/MetadataSquasher.ts @@ -70,7 +70,7 @@ export class MetadataSquasher { attachments: test_definition_and_below_direct.attachments, description: test_vertical.description, descriptionHtml: test_vertical.descriptionHtml, - displayName: test_definition_and_below.displayName, + displayName: test_definition_and_below_direct.displayName, fullName: test_definition_and_below.fullName, historyId: test_definition_and_below.historyId, labels: test_vertical.labels, diff --git a/src/options/common/compositeExtractor.ts b/src/options/common/compositeExtractor.ts index 74fbe27..242f2bf 100644 --- a/src/options/common/compositeExtractor.ts +++ b/src/options/common/compositeExtractor.ts @@ -22,9 +22,6 @@ function getPropertyContext( get value() { return context.value[key] as MaybePromise; }, - get result() { - return context.value; - }, }; } @@ -39,11 +36,13 @@ export function compositeExtractor( const propertyCustomizer = customizer[key]; const descriptor: ProxyPropertyDescriptor = { enumerable: true, - get: propertyCustomizer - ? onceWithLoopDetection(() => propertyCustomizer(propertyContext)) - : () => propertyContext.value, + get: onceWithLoopDetection(computePropertyValue), }; + function computePropertyValue() { + return propertyCustomizer(propertyContext); + } + return [key, descriptor] as const; }), ); diff --git a/src/options/common/last.ts b/src/options/common/last.ts index 9973c58..124f1ff 100644 --- a/src/options/common/last.ts +++ b/src/options/common/last.ts @@ -1,10 +1,11 @@ import type { MaybeNullish, MaybePromise, PropertyExtractorContext } from 'jest-allure2-reporter'; -import { isPromiseLike } from '../../utils'; +import { thruMaybePromise } from '../../utils'; -export const last = async ( +export const last = ( context: PropertyExtractorContext<{}, MaybePromise>>, -): Promise => { - const value = isPromiseLike(context.value) ? await context.value : context.value; - return value?.at(-1); +): MaybePromise => { + return thruMaybePromise, T | undefined>(context.value, (value) => + value?.at(-1), + ); }; diff --git a/src/options/common/mergerExtractor.ts b/src/options/common/mergerExtractor.ts index c7b0b5c..9129c36 100644 --- a/src/options/common/mergerExtractor.ts +++ b/src/options/common/mergerExtractor.ts @@ -5,7 +5,7 @@ import type { PropertyExtractor, } from 'jest-allure2-reporter'; -import { isPromiseLike, thruMaybePromise } from '../../utils'; +import { thruMaybePromise } from '../../utils'; import { composeExtractors2 } from './composeExtractors2'; @@ -28,7 +28,7 @@ export function mergerExtractor( const value = maybeExtractor; return (context): MaybePromise => { const base = context.value; - return isPromiseLike(base) ? base.then((v) => ({ ...v, ...value })) : { ...base, ...value }; + return thruMaybePromise(base, (v) => ({ ...v, ...value })); }; } diff --git a/src/options/custom/__utils__/contexts.ts b/src/options/custom/__utils__/contexts.ts index eecdc9d..27fdb2c 100644 --- a/src/options/custom/__utils__/contexts.ts +++ b/src/options/custom/__utils__/contexts.ts @@ -45,7 +45,17 @@ export const createTestCaseContext = ( TestCaseExtractorContext, PromisedProperties > => ({ - ...createTestStepContext({}), + result: {}, + aggregatedResult: {} as any, + testRunMetadata: {} as any, + testCase: {} as any, + testCaseMetadata: {} as any, + filePath: [], + testFile: {} as any, + testFileMetadata: {} as any, + $: {} as any, + globalConfig: {} as any, + config: {} as any, value: { ignored: false, historyId: '', diff --git a/src/options/custom/labels/simplifyLabelsMap.ts b/src/options/custom/labels/simplifyLabelsMap.ts index 3ac64b3..df7f449 100644 --- a/src/options/custom/labels/simplifyLabelsMap.ts +++ b/src/options/custom/labels/simplifyLabelsMap.ts @@ -48,7 +48,7 @@ function inflateLabels( name: string, ): PropertyExtractor>, MaybePromise> { function repair(value: string | undefined): Label | undefined { - return value == null ? undefined : { value, name }; + return value ? { value, name } : undefined; } function repairMaybeArray(value: MaybeArray): Label[] { diff --git a/src/options/custom/testCaseSteps.test.ts b/src/options/custom/testCaseSteps.test.ts index 85ecaf6..54b5eb0 100644 --- a/src/options/custom/testCaseSteps.test.ts +++ b/src/options/custom/testCaseSteps.test.ts @@ -1,26 +1,42 @@ +import type { + PropertyExtractorContext, + PromisedProperties, + TestCaseExtractorContext, + AllureTestStepResult, +} from 'jest-allure2-reporter'; + +import { testStep } from './testStep'; import { testCaseSteps } from './testCaseSteps'; import { createTestCaseContext } from './__utils__/contexts'; describe('testCaseSteps', () => { it('should extract nested steps', async () => { let counter = 0; - const testStep = jest.fn().mockImplementation(() => ({ - displayName: `Step ${++counter}`, - })); + const displayName = jest.fn().mockImplementation(() => `Step ${++counter}`); + const testStepExtractor = testStep({ displayName }); + const testCase = testCaseSteps(testStepExtractor, 'testCaseMetadata'); + const context: PropertyExtractorContext< + TestCaseExtractorContext, + PromisedProperties[] + > = { + ...createTestCaseContext(), + value: [], + }; - const testCase = testCaseSteps(testStep, 'testCaseMetadata'); - const context = createTestCaseContext(); context.testCaseMetadata.steps = [{}, {}]; const result = testCase(context); - if (Array.isArray(result?.steps)) { - expect(result?.steps).toHaveLength(2); - expect(result?.steps?.[0]?.displayName).toBe('Step 1'); - expect(result?.steps?.[1]?.displayName).toBe('Step 2'); + if (Array.isArray(result)) { + expect(result).toHaveLength(2); + expect(result[0].displayName).toBe('Step 1'); + expect(result[1].displayName).toBe('Step 2'); + // Testing memoization + expect(result[0].displayName).toBe('Step 1'); + expect(result[1].displayName).toBe('Step 2'); } else { - expect(result?.steps).toBeInstanceOf(Array); + expect(result).toBeInstanceOf(Array); } - expect(testStep).toHaveBeenCalledTimes(2); + expect(displayName).toHaveBeenCalledTimes(2); }); }); diff --git a/src/options/custom/testCaseSteps.ts b/src/options/custom/testCaseSteps.ts index 7b73e4f..f4465b3 100644 --- a/src/options/custom/testCaseSteps.ts +++ b/src/options/custom/testCaseSteps.ts @@ -1,43 +1,40 @@ -import type { TestCaseExtractorContext } from 'jest-allure2-reporter'; +import type { + AllureTestItemMetadata, + AllureTestStepResult, + TestStepExtractorContext, + PromisedProperties, +} from 'jest-allure2-reporter'; -import type { TestCaseExtractor, TestStepExtractor } from '../types'; -import { isNonNullish, maybePromiseAll, onceWithLoopDetection } from '../../utils'; +import type { TestStepExtractor, TestStepsExtractor } from '../types'; -interface HasMetadata { - testCaseMetadata?: TestCaseExtractorContext['testCaseMetadata']; - testFileMetadata?: TestCaseExtractorContext['testFileMetadata']; - testRunMetadata?: TestCaseExtractorContext['testRunMetadata']; -} +type HasMetadata = Context & { + [key in Key]: AllureTestItemMetadata; +}; -export function testCaseSteps( - testStep: TestStepExtractor, - metadataKey: keyof HasMetadata, -): TestCaseExtractor { - return (context) => { - Object.defineProperty(context.value, 'steps', { - enumerable: true, - get: onceWithLoopDetection(() => { - const steps = context[metadataKey]?.steps; - if (steps && steps.length > 0) { - return maybePromiseAll( - steps.map((testStepMetadata) => { - const stepContext = { - ...context, - testStepMetadata, - result: {}, - value: undefined as never, - }; +export function testCaseSteps< + BaseContext extends Partial>, + Key extends keyof BaseContext, + Context extends HasMetadata, +>( + testStep: TestStepExtractor, + metadataKey: Key, +): TestStepsExtractor { + return (context): PromisedProperties[] => { + const steps = context[metadataKey]?.steps; + if (!steps || steps.length === 0) { + return []; + } - return testStep(stepContext); - }), - (allSteps) => allSteps.filter(isNonNullish), - ); - } + return steps.map((testStepMetadata) => { + const testStepContext: any = { + ...context, + testStepMetadata, + result: {}, + value: {}, + }; - return; - }), + testStepContext.result = testStep(testStepContext); + return testStep(testStepContext); }); - - return context.value; }; } diff --git a/src/options/custom/testStep.test.ts b/src/options/custom/testStep.test.ts index 2114085..9802bea 100644 --- a/src/options/custom/testStep.test.ts +++ b/src/options/custom/testStep.test.ts @@ -15,7 +15,7 @@ describe('testStepCustomizer', () => { PromisedProperties > => ({ value, - result: undefined as never, + result: {} as any, aggregatedResult: {} as any, testRunMetadata: {} as any, testStepMetadata: {} as any, @@ -61,7 +61,7 @@ describe('testStepCustomizer', () => { expect(asyncExtractor).toHaveBeenCalledWith( expect.objectContaining({ value: defaultValue, - result: context.value, + result: context.result, }), ); }, @@ -99,7 +99,7 @@ describe('testStepCustomizer', () => { expect(syncExtractor).toHaveBeenCalledWith( expect.objectContaining({ value: defaultValue, - result: context.value, + result: context.result, }), ); }, diff --git a/src/options/custom/testStep.ts b/src/options/custom/testStep.ts index c9b7b0d..833be30 100644 --- a/src/options/custom/testStep.ts +++ b/src/options/custom/testStep.ts @@ -11,10 +11,10 @@ import { parameters } from './parameters'; const fallback: PropertyExtractor<{}, any> = (context) => context.value; +export function testStep(customizer: null | undefined): undefined; export function testStep( customizer: TestStepCustomizer, ): TestStepExtractor; -export function testStep(customizer: null | undefined): undefined; export function testStep( customizer: TestStepCustomizer | null | undefined, ): TestStepExtractor | undefined; diff --git a/src/options/index.ts b/src/options/index.ts index ef00522..749307a 100644 --- a/src/options/index.ts +++ b/src/options/index.ts @@ -1,12 +1,23 @@ import type { ReporterOptions } from 'jest-allure2-reporter'; +import { testCaseSteps } from './custom'; import { defaultOptions } from './default'; import { extendOptions } from './extendOptions'; -import type { ReporterConfig } from './types'; +import type { ReporterConfig, ReporterFinalConfig } from './types'; import { defaultOverrides } from './override'; -export function resolveOptions(custom?: ReporterOptions | undefined): ReporterConfig { - return extendOptions(extendOptions(defaultOptions(), custom), defaultOverrides()); +export function resolveOptions(custom?: ReporterOptions | undefined): ReporterFinalConfig { + const config: ReporterConfig = extendOptions( + extendOptions(defaultOptions(), custom), + defaultOverrides(), + ); + + return { + ...config, + testFileSteps: testCaseSteps(config.testStep, 'testFileMetadata'), + testCaseSteps: testCaseSteps(config.testStep, 'testCaseMetadata'), + testRunSteps: testCaseSteps(config.testStep, 'testRunMetadata'), + } as ReporterFinalConfig; } -export { type ReporterConfig } from './types'; +export { type ReporterFinalConfig as ReporterConfig } from './types'; diff --git a/src/options/types.ts b/src/options/types.ts index f14d71a..0e16ba2 100644 --- a/src/options/types.ts +++ b/src/options/types.ts @@ -10,12 +10,12 @@ import type { TestCaseExtractorContext, TestFileExtractorContext, TestRunExtractorContext, - TestStepExtractorContext, Primitive, PromisedProperties, MarkdownProcessorOptions, SourceCodeProcessorConfig, MaybePromise, + TestStepExtractorContext, } from 'jest-allure2-reporter'; export type CompositeExtractor = { @@ -39,36 +39,62 @@ export interface ReporterConfig { testStep: TestStepExtractor; } -export type ExecutorExtractor = PropertyExtractor< +export interface ReporterFinalConfig extends ReporterConfig { + categories: CategoriesExtractor; + environment: EnvironmentExtractor; + executor: ExecutorExtractor; + helpers: HelpersExtractor; + testCase: TestCaseExtractor; + testFile: TestCaseExtractor; + testRun: TestCaseExtractor; + testStep: TestStepExtractor; + testCaseSteps: TestStepsExtractor; + testFileSteps: TestStepsExtractor; + testRunSteps: TestStepsExtractor; +} + +export type ExecutorExtractor = PropertyExtractor< GlobalExtractorContext, + MaybePromise | T, MaybePromise >; -export type HelpersExtractor = PropertyExtractor< +export type HelpersExtractor = PropertyExtractor< GlobalExtractorContext, + PromisedProperties | T, PromisedProperties >; -export type CategoriesExtractor = PropertyExtractor< +export type CategoriesExtractor = PropertyExtractor< GlobalExtractorContext, + MaybePromise | T, MaybePromise >; -export type EnvironmentExtractor = PropertyExtractor< +export type EnvironmentExtractor = PropertyExtractor< GlobalExtractorContext, + MaybePromise> | T, MaybePromise> >; -export type TestCaseExtractor = PropertyExtractor< +export type TestCaseExtractor = PropertyExtractor< Context, + PromisedProperties | T, PromisedProperties >; -export type TestStepExtractor = PropertyExtractor< +export type TestStepExtractor = PropertyExtractor< Context, + PromisedProperties | T, PromisedProperties >; +export type TestStepsExtractor = PropertyExtractor< + Context, + PromisedProperties[] | T, + PromisedProperties[] +>; + export type TestCaseCompositeExtractor = CompositeExtractor; export type TestStepCompositeExtractor = CompositeExtractor; diff --git a/src/reporter/JestAllure2Reporter.ts b/src/reporter/JestAllure2Reporter.ts index 8bac277..ca3bbee 100644 --- a/src/reporter/JestAllure2Reporter.ts +++ b/src/reporter/JestAllure2Reporter.ts @@ -29,19 +29,18 @@ import type { import { type ReporterConfig, resolveOptions } from '../options'; import { AllureMetadataProxy, MetadataSquasher } from '../metadata'; import { stringifyValues } from '../utils'; -import { resolvePromisedProperties } from '../utils/resolvePromisedProperties'; import * as fallbacks from './fallbacks'; import { overwriteDirectory } from './overwriteDirectory'; import { postProcessMetadata } from './postProcessMetadata'; import { writeTest } from './allureCommons'; +import { resolvePromisedItem, resolvePromisedTestCase } from './resolveTestItem'; export class JestAllure2Reporter extends JestMetadataReporter { - private _$: Helpers | undefined; + private _globalContext: GlobalExtractorContext | null = null; private readonly _allure: AllureRuntime; private readonly _config: ReporterConfig; private readonly _globalConfig: Config.GlobalConfig; - private readonly _globalContext: GlobalExtractorContext; constructor(globalConfig: Config.GlobalConfig, options: ReporterOptions) { super(globalConfig); @@ -59,12 +58,6 @@ export class JestAllure2Reporter extends JestMetadataReporter { attachments: this._config.attachments, injectGlobals: this._config.injectGlobals, }); - - this._globalContext = { - $: {} as Helpers, - globalConfig: this._globalConfig, - config: this._config, - }; } async onRunStart( @@ -73,14 +66,36 @@ export class JestAllure2Reporter extends JestMetadataReporter { ): Promise { await super.onRunStart(aggregatedResult, options); - this._$ = await resolvePromisedProperties( - this._config.helpers({ ...this._globalContext, value: this._globalContext.$ }), - ); + const config = this._config; + + const globalContext = { + $: {} as Helpers, + globalConfig: this._globalConfig, + config, + value: undefined, + }; + + globalContext.$ = await resolvePromisedItem(globalContext, config.helpers, '$'); - this._globalContext.$ = this._$!; + this._globalContext = globalContext; - if (this._config.overwrite) { - await overwriteDirectory(this._config.resultsDir); + if (config.overwrite) { + await overwriteDirectory(config.resultsDir); + } + + const environment = await config.environment(globalContext); + if (environment) { + this._allure.writeEnvironmentInfo(stringifyValues(environment)); + } + + const executor = await config.executor(globalContext); + if (executor) { + this._allure.writeExecutorInfo(executor); + } + + const categories = await config.categories(globalContext); + if (categories) { + this._allure.writeCategoriesDefinitions(categories as Category[]); } } @@ -110,7 +125,7 @@ export class JestAllure2Reporter extends JestMetadataReporter { const testFileMetadata = new AllureMetadataProxy(rawMetadata); fallbacks.onTestFileResult(test, testFileMetadata); - await postProcessMetadata(this._$ as Helpers, rawMetadata); + await postProcessMetadata(this._globalContext!.$, rawMetadata); } async onRunComplete( @@ -124,34 +139,22 @@ export class JestAllure2Reporter extends JestMetadataReporter { const testRunMetadata = JestAllure2Reporter.query.globalMetadata(); const globalMetadataProxy = new AllureMetadataProxy(testRunMetadata); - const globalContext: PropertyExtractorContext = { - $: this._$ as Helpers, + const globalContext = this._globalContext!; + const testRunContext: PropertyExtractorContext = { + ...globalContext, aggregatedResult, - globalConfig: this._globalConfig, - config: this._config, testRunMetadata: globalMetadataProxy.get(), result: {}, - value: undefined as never, + value: undefined, }; - const environment = await config.environment(globalContext); - if (environment) { - this._allure.writeEnvironmentInfo(stringifyValues(environment)); - } - - const executor = await config.executor(globalContext); - if (executor) { - this._allure.writeExecutorInfo(executor); - } - - const categories = await config.categories(globalContext); - if (categories) { - this._allure.writeCategoriesDefinitions(categories as Category[]); - } - const squasher = new MetadataSquasher(); - const allureRunTest = await resolvePromisedProperties(config.testRun(globalContext)); + const allureRunTest = await resolvePromisedTestCase( + testRunContext, + config.testRun, + config.testRunSteps, + ); if (allureRunTest) { writeTest({ containerName: `Test Run (${process.pid})`, @@ -161,16 +164,20 @@ export class JestAllure2Reporter extends JestMetadataReporter { } for (const testResult of aggregatedResult.testResults) { - const testFileContext: PropertyExtractorContext = { - ...globalContext, + const testFileContext: PropertyExtractorContext = { + ...testRunContext, filePath: path - .relative(globalContext.globalConfig.rootDir, testResult.testFilePath) + .relative(testRunContext.globalConfig.rootDir, testResult.testFilePath) .split(path.sep), testFile: testResult, testFileMetadata: squasher.testFile(JestAllure2Reporter.query.testResult(testResult)), }; - const allureFileTest = await resolvePromisedProperties(config.testFile(testFileContext)); + const allureFileTest = await resolvePromisedTestCase( + testFileContext, + config.testFile, + config.testFileSteps, + ); if (allureFileTest) { writeTest({ containerName: `${testResult.testFilePath}`, @@ -186,13 +193,17 @@ export class JestAllure2Reporter extends JestMetadataReporter { for (const testInvocationMetadata of allInvocations) { const testCaseMetadata = squasher.testInvocation(testInvocationMetadata); - const testCaseContext: PropertyExtractorContext = { + const testCaseContext: PropertyExtractorContext = { ...testFileContext, testCase: testCaseResult, testCaseMetadata, }; - const allureTest = await resolvePromisedProperties(config.testCase(testCaseContext)); + const allureTest = await resolvePromisedTestCase( + testCaseContext, + config.testCase, + config.testCaseSteps, + ); if (!allureTest) { continue; } diff --git a/src/reporter/resolveTestItem.ts b/src/reporter/resolveTestItem.ts new file mode 100644 index 0000000..54cc8ff --- /dev/null +++ b/src/reporter/resolveTestItem.ts @@ -0,0 +1,57 @@ +import type { + AllureTestCaseResult, + AllureTestStepResult, + PromisedProperties, + PropertyExtractor, + TestRunExtractorContext, +} from 'jest-allure2-reporter'; + +import { log } from '../logger'; + +export async function resolvePromisedTestCase( + context: Context, + extractor: PropertyExtractor>, + _stepsExtractor: PropertyExtractor[]>, +): Promise { + const promised = preparePromisedItem(context, extractor, 'result'); + try { + if (await promised.ignored) { + return; + } + + const item = await resolvePromisedProperties(promised); + return item; + } catch (error: unknown) { + log.warn({ err: error }, 'Failed to resolve test case', error); + return; + } +} + +export async function resolvePromisedItem( + context: Context, + extractor: PropertyExtractor>, + resultKey: ResultKey, +): Promise { + return resolvePromisedProperties(preparePromisedItem(context, extractor, resultKey)); +} + +function preparePromisedItem( + context: Context, + extractor: PropertyExtractor>, + resultKey: ResultKey, +): PromisedProperties { + const value = {} as Result; + const firstPass = extractor({ ...context, value, [resultKey]: value }); + const secondPass = extractor({ ...context, value, [resultKey]: firstPass }); + + return secondPass; +} + +async function resolvePromisedProperties(maybePromised: PromisedProperties): Promise { + const promised = await maybePromised; + const entries = Object.entries(promised); + const resolvedEntries = await Promise.all( + entries.map(async ([key, value]) => [key, await value]), + ); + return Object.fromEntries(resolvedEntries); +} diff --git a/src/utils/resolvePromisedProperties.ts b/src/utils/resolvePromisedProperties.ts deleted file mode 100644 index 17b22e2..0000000 --- a/src/utils/resolvePromisedProperties.ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { MaybePromise, PromisedProperties } from 'jest-allure2-reporter'; - -import { log } from '../logger'; - -export async function resolvePromisedProperties( - maybePromised: MaybePromise>, -): Promise { - try { - const promised = await maybePromised; - const entries = Object.entries(promised); - const resolvedEntries = await Promise.all( - entries.map(async ([key, value]) => [key, await value]), - ); - return Object.fromEntries(resolvedEntries); - } catch (error: unknown) { - log.warn({ err: error }, 'Failed to resolve promised properties'); - return; - } -}