Skip to content

Commit

Permalink
so it runs
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Apr 6, 2024
1 parent 1f776a4 commit c6c184f
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 148 deletions.
10 changes: 8 additions & 2 deletions e2e/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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}}',
},
},
};
Expand Down
14 changes: 7 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ declare module 'jest-allure2-reporter' {
/**
* Customize how individual test steps are reported.
*/
testStep?: TestStepCustomizer<TestStepExtractorContext>;
testStep?: TestStepCustomizer;
}

export interface ReporterConfig extends ReporterOptionsAugmentation {
Expand Down Expand Up @@ -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<Context = {}> {
export interface TestStepCustomizer<Context = TestStepExtractorContext> {
/**
* Extractor to omit test steps from the report.
*/
Expand Down Expand Up @@ -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<PromisedProperties<AllureTestStepResult>>;
result: Partial<PromisedProperties<AllureTestCaseResult>>;
}

export interface Helpers extends HelpersAugmentation {
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/squasher/MetadataSquasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions src/options/common/compositeExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ function getPropertyContext<Context, Shape, K extends keyof Shape>(
get value() {
return context.value[key] as MaybePromise<Shape[K]>;
},
get result() {
return context.value;
},
};
}

Expand All @@ -39,11 +36,13 @@ export function compositeExtractor<Context, Shape>(
const propertyCustomizer = customizer[key];
const descriptor: ProxyPropertyDescriptor<Shape, K> = {
enumerable: true,
get: propertyCustomizer
? onceWithLoopDetection(() => propertyCustomizer(propertyContext))
: () => propertyContext.value,
get: onceWithLoopDetection(computePropertyValue),
};

function computePropertyValue() {
return propertyCustomizer(propertyContext);
}

return [key, descriptor] as const;
}),
);
Expand Down
11 changes: 6 additions & 5 deletions src/options/common/last.ts
Original file line number Diff line number Diff line change
@@ -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 <T>(
export const last = <T>(
context: PropertyExtractorContext<{}, MaybePromise<MaybeNullish<T[]>>>,
): Promise<T | undefined> => {
const value = isPromiseLike(context.value) ? await context.value : context.value;
return value?.at(-1);
): MaybePromise<T | undefined> => {
return thruMaybePromise<MaybeNullish<T[]>, T | undefined>(context.value, (value) =>
value?.at(-1),
);
};
4 changes: 2 additions & 2 deletions src/options/common/mergerExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
PropertyExtractor,
} from 'jest-allure2-reporter';

import { isPromiseLike, thruMaybePromise } from '../../utils';
import { thruMaybePromise } from '../../utils';

import { composeExtractors2 } from './composeExtractors2';

Expand All @@ -28,7 +28,7 @@ export function mergerExtractor<Context, Value extends {}>(
const value = maybeExtractor;
return (context): MaybePromise<Value> => {
const base = context.value;
return isPromiseLike(base) ? base.then((v) => ({ ...v, ...value })) : { ...base, ...value };
return thruMaybePromise(base, (v) => ({ ...v, ...value }));
};
}

Expand Down
12 changes: 11 additions & 1 deletion src/options/custom/__utils__/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ export const createTestCaseContext = (
TestCaseExtractorContext,
PromisedProperties<AllureTestCaseResult>
> => ({
...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: '',
Expand Down
2 changes: 1 addition & 1 deletion src/options/custom/labels/simplifyLabelsMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function inflateLabels<Context>(
name: string,
): PropertyExtractor<Context, MaybePromise<MaybeArray<string>>, MaybePromise<Label[]>> {
function repair(value: string | undefined): Label | undefined {
return value == null ? undefined : { value, name };
return value ? { value, name } : undefined;
}

function repairMaybeArray(value: MaybeArray<string>): Label[] {
Expand Down
38 changes: 27 additions & 11 deletions src/options/custom/testCaseSteps.test.ts
Original file line number Diff line number Diff line change
@@ -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<AllureTestStepResult>[]
> = {
...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);
});
});
67 changes: 32 additions & 35 deletions src/options/custom/testCaseSteps.ts
Original file line number Diff line number Diff line change
@@ -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 extends keyof Context> = Context & {
[key in Key]: AllureTestItemMetadata;
};

export function testCaseSteps<Context extends HasMetadata>(
testStep: TestStepExtractor<Context>,
metadataKey: keyof HasMetadata,
): TestCaseExtractor<Context> {
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<Omit<TestStepExtractorContext, 'testStepMetadata'>>,
Key extends keyof BaseContext,
Context extends HasMetadata<BaseContext, Key>,
>(
testStep: TestStepExtractor<TestStepExtractorContext>,
metadataKey: Key,
): TestStepsExtractor<Context, void> {
return (context): PromisedProperties<AllureTestStepResult>[] => {
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 = {

Check warning on line 29 in src/options/custom/testCaseSteps.ts

View workflow job for this annotation

GitHub Actions / Sanity

Unexpected any. Specify a different type
...context,
testStepMetadata,
result: {},
value: {},
};

return;
}),
testStepContext.result = testStep(testStepContext);
return testStep(testStepContext);
});

return context.value;
};
}
6 changes: 3 additions & 3 deletions src/options/custom/testStep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('testStepCustomizer', () => {
PromisedProperties<AllureTestStepResult>
> => ({
value,
result: undefined as never,
result: {} as any,
aggregatedResult: {} as any,
testRunMetadata: {} as any,
testStepMetadata: {} as any,
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('testStepCustomizer', () => {
expect(asyncExtractor).toHaveBeenCalledWith(
expect.objectContaining({
value: defaultValue,
result: context.value,
result: context.result,
}),
);
},
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('testStepCustomizer', () => {
expect(syncExtractor).toHaveBeenCalledWith(
expect.objectContaining({
value: defaultValue,
result: context.value,
result: context.result,
}),
);
},
Expand Down
2 changes: 1 addition & 1 deletion src/options/custom/testStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context>(
customizer: TestStepCustomizer<Context>,
): TestStepExtractor<Context>;
export function testStep(customizer: null | undefined): undefined;
export function testStep<Context>(
customizer: TestStepCustomizer<Context> | null | undefined,
): TestStepExtractor<Context> | undefined;
Expand Down
19 changes: 15 additions & 4 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Loading

0 comments on commit c6c184f

Please sign in to comment.