Skip to content

Commit

Permalink
resurrect docblock :happy:
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Mar 16, 2024
1 parent a973c56 commit 142c242
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 62 deletions.
4 changes: 2 additions & 2 deletions e2e/src/programmatic/grouping/names.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { allure } from 'jest-allure2-reporter/api';

describe('Names', () => {
/* Regular beforeAll */
// Regular beforeAll
beforeAll(() => {
});
/** Docblock beforeAll */
Expand All @@ -20,7 +20,7 @@ describe('Names', () => {
allure.displayName('Programmatic beforeAll');
});

/* Regular afterEach */
// Regular afterEach
afterEach(() => {
});
/** Docblock afterEach */
Expand Down
2 changes: 1 addition & 1 deletion src/api/annotations/$Label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { LabelName } from 'jest-allure2-reporter';

import { LABELS } from '../../metadata/constants';

export const $Label = (name: LabelName, ...values: unknown[]) =>
export const $Label = (name: LabelName | string, ...values: unknown[]) =>
$Push(LABELS, ...values.map((value) => ({ name, value: `${value}` })));
1 change: 1 addition & 0 deletions src/metadata/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const PREFIX = 'allure2' as const;

export const DOCBLOCK = [PREFIX, 'docblock'] as const;
export const CURRENT_STEP = [PREFIX, 'currentStep'] as const;
export const DESCRIPTION = [PREFIX, 'description'] as const;
export const DESCRIPTION_HTML = [PREFIX, 'descriptionHtml'] as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ import type {
LinkType,
} from 'jest-allure2-reporter';

import { asArray } from '../utils';

const ALL_LABELS = Object.keys(
assertType<Record<LabelName, 0>>({
epic: 0,
feature: 0,
owner: 0,
package: 0,
parentSuite: 0,
severity: 0,
story: 0,
subSuite: 0,
suite: 0,
tag: 0,
testClass: 0,
testMethod: 0,
thread: 0,
}),
) as LabelName[];

const ALL_LINKS = Object.keys(
assertType<Record<LinkType, 0>>({
issue: 0,
tms: 0,
}),
) as LinkType[];

export function mapTestStepDocblock({
comments,
}: AllureTestItemDocblock): AllureTestStepMetadata {
Expand All @@ -25,46 +52,31 @@ export function mapTestCaseDocblock(
const metadata: AllureTestCaseMetadata = {};
const { comments, pragmas = {} } = context;

const labels = (
['epic', 'feature', 'owner', 'severity', 'story', 'tag'] as const
).flatMap((name) => mapMaybeArray(pragmas[name], createLabelMapper(name)));
const labels = ALL_LABELS.flatMap((name) =>
asArray(pragmas[name]).map(createLabelMapper(name)),
);

if (labels.length > 0) metadata.labels = labels;

const links = (['issue', 'tms'] as const)
.flatMap((name) => mapMaybeArray(pragmas[name], createLinkMapper(name)))
.filter(Boolean);
const links = ALL_LINKS.flatMap((name) =>
asArray(pragmas[name]).map(createLinkMapper(name)),
).filter(Boolean);

if (links.length > 0) metadata.links = links;

if (comments || pragmas.description)
metadata.description = [
...(comments ? [comments] : []),
...(pragmas.description || []),
...asArray(comments),
...asArray(pragmas.description),
];

if (pragmas.descriptionHtml) {
metadata.descriptionHtml = mapMaybeArray(pragmas.descriptionHtml, (x) => x);
metadata.descriptionHtml = asArray(pragmas.descriptionHtml);
}

return metadata;
}

function mapMaybeArray<T, R>(
value: T | T[] | undefined,
mapper: (value: T) => R,
): R[] {
if (value == null) {
return [];
}

if (Array.isArray(value)) {
return value.map(mapper);
}

return [mapper(value)];
}

export const mapTestFileDocblock = mapTestCaseDocblock;

function createLabelMapper(name: LabelName) {
Expand All @@ -74,3 +86,7 @@ function createLabelMapper(name: LabelName) {
function createLinkMapper(type?: LinkType) {
return (url: string): Link => ({ type, url, name: url });
}

function assertType<T>(value: T) {
return value;
}
28 changes: 16 additions & 12 deletions src/metadata/squasher/MetadataSquasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import type {
TestInvocationMetadata,
} from 'jest-metadata';
import type {
AllureTestItemDocblock,
AllureNestedTestStepMetadata,
AllureTestCaseMetadata,
AllureTestFileMetadata,
AllureTestItemMetadata,
} from 'jest-allure2-reporter';

import { PREFIX } from '../constants';
import { DOCBLOCK, PREFIX } from '../constants';
import * as docblock from '../docblockMapping';

import { MetadataSelector } from './MetadataSelector';
import {
Expand All @@ -19,12 +20,6 @@ import {
mergeTestStepMetadata,
} from './mergers';

export type MetadataSquasherConfig = {
getDocblockMetadata: <T extends AllureTestItemMetadata>(
metadata: Metadata | undefined,
) => T | undefined;
};

export class MetadataSquasher {
protected readonly _fileSelector: MetadataSelector<
Metadata,
Expand All @@ -41,24 +36,33 @@ export class MetadataSquasher {
AllureNestedTestStepMetadata
>;

constructor(config: MetadataSquasherConfig) {
constructor() {
this._fileSelector = new MetadataSelector({
empty: () => ({}),
getDocblock: config.getDocblockMetadata,
getDocblock: (metadata) =>
docblock.mapTestFileDocblock(
metadata.get<AllureTestItemDocblock>(DOCBLOCK, {}),
),
getMetadata: (metadata) => metadata.get<AllureTestFileMetadata>(PREFIX),
mergeUnsafe: mergeTestFileMetadata,
});

this._testSelector = new MetadataSelector({
empty: () => ({}),
getDocblock: config.getDocblockMetadata,
getDocblock: (metadata) =>
docblock.mapTestCaseDocblock(
metadata.get<AllureTestItemDocblock>(DOCBLOCK, {}),
),
getMetadata: (metadata) => metadata.get<AllureTestCaseMetadata>(PREFIX),
mergeUnsafe: mergeTestCaseMetadata,
});

this._stepSelector = new MetadataSelector({
empty: () => ({}),
getDocblock: config.getDocblockMetadata,
getDocblock: (metadata) =>
docblock.mapTestStepDocblock(
metadata.get<AllureTestItemDocblock>(DOCBLOCK, {}),
),
getMetadata: (metadata) =>
metadata.get<AllureNestedTestStepMetadata>(PREFIX),
mergeUnsafe: mergeTestStepMetadata,
Expand Down
15 changes: 4 additions & 11 deletions src/options/utils/aggregateLabelCustomizers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import type { Label } from 'jest-allure2-reporter';

import { flatMapAsync } from '../../utils/flatMapAsync';
import { asArray } from '../../utils';

import { asExtractor } from './asExtractor';

Expand Down Expand Up @@ -62,7 +63,9 @@ export function aggregateLabelCustomizers<C extends Customizer>(
};
const extracted = await extractor(aContext);
const value = asArray(extracted);
return value ? value.map((value) => ({ name, value }) as Label) : [];
return value.length > 0
? value.map((value) => ({ name, value }) as Label)
: [];
})),
];

Expand All @@ -71,13 +74,3 @@ export function aggregateLabelCustomizers<C extends Customizer>(

return combined;
}

function asArray<T extends string>(
value: T | T[] | undefined,
): T[] | undefined {
if (Array.isArray(value)) {
return value.length > 0 ? value : undefined;
} else {
return value ? [value] : [];
}
}
8 changes: 6 additions & 2 deletions src/reporter-plugins/docblock/docblockPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ export const docblockPlugin: PluginConstructor = () => {
}
}

if (!extracted && fileName && lineNumber != null) {
if (!extracted && fileName) {
const navigator = await FileNavigatorCache.instance.resolve(fileName);
extracted = extractJsdocAbove(navigator, lineNumber);

extracted =
lineNumber == null
? extract(navigator.sourceCode)
: extractJsdocAbove(navigator, lineNumber);
}

if (extracted) {
Expand Down
24 changes: 14 additions & 10 deletions src/reporter/JestAllure2Reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ export class JestAllure2Reporter extends JestMetadataReporter {

await this._postProcessMetadata(); // Run before squashing

const docblockParser: any = { find: () => void 0 }; // TODO: await initParser();
const squasher = new MetadataSquasher({
getDocblockMetadata: (metadata) =>
metadata && docblockParser.find(metadata),
});
const squasher = new MetadataSquasher();

for (const testResult of results.testResults) {
const testFileContext: TestFileExtractorContext = {
Expand Down Expand Up @@ -427,11 +423,19 @@ export class JestAllure2Reporter extends JestMetadataReporter {
}

private async _postProcessMetadata() {
const batch = state.testFiles.flatMap((testFile) => [
testFile,
...testFile.allDescribeBlocks(),
...testFile.allTestEntries(),
]);
const batch = state.testFiles.flatMap((testFile) => {
const allDescribeBlocks = [...testFile.allDescribeBlocks()];
const allHooks = allDescribeBlocks.flatMap((describeBlock) => [
...describeBlock.hookDefinitions(),
]);

return [
testFile,
...allDescribeBlocks,
...allHooks,
...testFile.allTestEntries(),
];
});

await Promise.all(
batch.map(async (metadata) => {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/asArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function asArray<T extends string>(value: T | T[] | undefined): T[] {
if (Array.isArray(value)) {
return value.length > 0 ? value : [];
} else {
return value ? [value] : [];
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './asArray';
export * from './attempt';
export * from './autoIndent';
export * from './constant';
Expand Down

0 comments on commit 142c242

Please sign in to comment.