Skip to content

Commit

Permalink
numerous fixes and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed May 4, 2024
1 parent 0ce4335 commit 2cc5357
Show file tree
Hide file tree
Showing 64 changed files with 581 additions and 904 deletions.
6 changes: 6 additions & 0 deletions e2e/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ const jestAllure2ReporterOptions = {
tms: 'https://test.testrail.io/index.php?/cases/view/{{name}}',
},
},
testRun: {
ignored: false,
},
testFile: {
ignored: false,
},
testStep: {
ignored: ({ testStepMetadata }) => testStepMetadata.displayName?.includes('(Jest)'),
},
Expand Down
8 changes: 6 additions & 2 deletions e2e/presets/code-snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ const path = require('node:path');
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);
const steps = metadata.steps || [];
const before = steps.filter(s => s.hookType?.startsWith('before'));
const after = steps.filter(s => s.hookType?.startsWith('after'));
const allMetadata = [...before, metadata, ...after];
const codes = await Promise.all(allMetadata.map(m => $.extractSourceCode(m, true)));
const snippets = codes.filter(Boolean).map($.source2markdown);
return [text, ...snippets].filter(t => t != null).join('\n\n');
};

Expand Down
29 changes: 0 additions & 29 deletions e2e/test_plan.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
# Test Plan

1. Test cases:
* by **status**:
* passed
* failed
* broken
* skipped
* unknown
* by **severity**:
* default
* custom
* for an individual test
* for a test suite
* by **category**:
* Product defect
* Test defect
Expand All @@ -26,19 +15,6 @@
* from a broken "afterAll" hook
* from broken test suite files
* broken due to a failed test environment setup
* with **labels**:
* flaky
* custom tag (value)
* custom label (key=value)
* maintainer?
* lead?
* JIRA
* TMS
* custom
* with **Description**
* with **Parameters**
* with **Links**
* with **Environment**
* with **Execution**:
* Set up
* Test body
Expand Down Expand Up @@ -66,11 +42,6 @@
* Package
* ...
3. Report-scoped features:
* Environment
* Full
* Zero
* Filtered (strings)
* Filtered (callback)
* Executor (build agent name)
4. Test run history
* Duration
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/sanity/decorators/Step.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Step } from '../../../../dist/api';
import { Step } from 'jest-allure2-reporter/api';

describe('Step', () => {
class FactorialCalculator {
Expand Down
6 changes: 3 additions & 3 deletions e2e/tests/sanity/runtime/createAttachment.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const os = require('node:os');

test('should wrap a function with automatic attachment of its result', async () => {
const diagnostics = () => JSON.stringify({ available_memory: os.freemem() });
const diagnostics = () => ({ available_memory: os.freemem() });
const wrapped = allure.createAttachment(diagnostics, 'diagnostics-{{0}}.json');

// Creates 'diagnostics-before.json' attachment
expect(wrapped('before')).toMatch(/{"available_memory":\d+}/);
expect(wrapped('before')).toEqual({ available_memory: expect.any(Number) });
// Consume some memory
expect(Array.from({ length: 100000 }, () => Math.random())).toHaveLength(100000);
// Creates 'diagnostics-after.json' attachment; the returned value is not changed by the wrapper
expect(wrapped('after')).toMatch(/{"available_memory":\d+}/);
expect(wrapped('after')).toEqual({ available_memory: expect.any(Number) });
});
65 changes: 19 additions & 46 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ declare module 'jest-allure2-reporter' {
* Configure how external attachments are attached to the report.
*/
attachments?: AttachmentsOptions;
/**
* Tweak the way markdown is processed.
* You can enable or disable the processor, add remark plugins, etc.
*/
markdown?: MarkdownProcessorOptions;
/**
* Tweak the way source code and docblocks are extracted from test files.
*/
Expand Down Expand Up @@ -119,13 +114,6 @@ declare module 'jest-allure2-reporter' {
/** @see {@link AttachmentsOptions#contentHandler} */
export type BuiltinContentAttachmentHandler = 'write';

export interface MarkdownProcessorOptions {
enabled?: boolean;
keepSource?: boolean;
remarkPlugins?: MaybeWithOptions<unknown>[];
rehypePlugins?: MaybeWithOptions<unknown>[];
}

export interface SourceCodeProcessorOptions {
enabled?: boolean;
plugins?: Record<string, SourceCodePluginCustomizer | unknown | [SourceCodePluginCustomizer, unknown?]>;
Expand All @@ -140,11 +128,11 @@ declare module 'jest-allure2-reporter' {
export interface SourceCodePlugin {
readonly name: string;

extractDocblock?(context: Readonly<SourceCodeExtractionContext>): MaybePromise<AllureTestItemDocblock | undefined>;
extractSourceCode?(context: Readonly<SourceCodeExtractionContext>): MaybePromise<ExtractSourceCodeHelperResult | undefined>;
extractDocblock?(context: Readonly<DocblockExtractionContext>): MaybePromise<AllureTestItemDocblock | undefined>;
extractSourceCode?(location: Readonly<AllureTestItemSourceLocation>, includeComments: boolean): MaybePromise<ExtractSourceCodeHelperResult | undefined>;
}

export interface SourceCodeExtractionContext extends AllureTestItemSourceLocation {
export interface DocblockExtractionContext extends AllureTestItemSourceLocation {
transformedCode?: string;
}

Expand Down Expand Up @@ -408,30 +396,19 @@ declare module 'jest-allure2-reporter' {
/**
* Provides an optimized way to navigate through the test file content.
* Accepts a file path in a string or a split array format.
* Returns undefined if the file is not found or cannot be read.
* @param filePath - the path to the file to navigate, split by directory separators or as a single string.
* @returns a file navigator object or undefined if the file is not found or cannot be read.
*/
getFileNavigator(filePath: string | string[]): Promise<FileNavigator | undefined>;
/**
* Extracts the source code of the current test case, test step or a test file.
* Pass `true` as the second argument to extract source code recursively from all steps.
*
* Extracts the source code of the current test case or step.
* @param metadata - the metadata object of the test case or step.
* @param includeComments - whether to include comments before the actual code.
* @returns the extracted source code or undefined if the source code is not found.
* @example
* ({ $, testFileMetadata }) => $.extractSourceCode(testFileMetadata)
* @example
* ({ $, testCaseMetadata }) => $.extractSourceCode(testCaseMetadata, true)
*/
extractSourceCode: ExtractSourceCodeHelper;
/**
* Extracts the executor information from the current environment.
* Pass `true` as the argument to include local executor information.
* By default, supports GitHub Actions and Buildkite.
*
* @example
* ({ $ }) => $.getExecutorInfo()
* @example
* ({ $ }) => $.getExecutorInfo(true)
*/
getExecutorInfo: GetExecutorInfoHelper;
extractSourceCode(metadata: AllureTestItemMetadata, includeComments?: boolean): Promise<ExtractSourceCodeHelperResult | undefined>;
/**
* Extracts the manifest of the current project or a specific package.
* Pass a callback to extract specific data from the manifest – this way you can omit async/await.
Expand All @@ -446,6 +423,13 @@ declare module 'jest-allure2-reporter' {
* ({ $ }) => (await $.manifest('jest')).version
*/
manifest: ManifestHelper;
/**
* Strips ANSI escape codes from the given string or object.
* @example
* $.stripAnsi('Hello, \u001b[31mworld\u001b[0m!')
* @example
* $.stripAnsi({ message: 'Hello, \u001b[31mworld\u001b[0m!' })
*/
stripAnsi: StripAnsiHelper;
}

Expand All @@ -461,17 +445,6 @@ declare module 'jest-allure2-reporter' {
readLine(lineNumber?: number): string;
}

export interface ExtractSourceCodeHelper {
(metadata: AllureTestItemMetadata, recursive?: false): Promise<ExtractSourceCodeHelperResult | undefined>;
(metadata: AllureTestItemMetadata, recursive: true): Promise<ExtractSourceCodeHelperResult[]>;
(metadata: AllureTestItemMetadata, recursive: boolean): Promise<MaybeArray<ExtractSourceCodeHelperResult> | undefined>;
}

export interface GetExecutorInfoHelper {
(): MaybePromise<ExecutorInfo | undefined>;
(includeLocal: true): MaybePromise<ExecutorInfo>;
}

export interface ManifestHelper {
(packageName?: string): Promise<Record<string, any> | undefined>;
<T>(extractor: string[] | ManifestHelperExtractor<T>): Promise<T | undefined>;
Expand Down Expand Up @@ -672,7 +645,7 @@ declare module 'jest-allure2-reporter' {

export interface AllureTestCaseResult {
uuid?: string;
ignored: boolean;
ignored?: boolean;
historyId: Primitive;
displayName: string;
fullName: string;
Expand All @@ -691,7 +664,7 @@ declare module 'jest-allure2-reporter' {
}

export interface AllureTestStepResult {
ignored: boolean;
ignored?: boolean;
hookType?: AllureTestStepMetadata['hookType'];
displayName: string;
start: number;
Expand Down
10 changes: 7 additions & 3 deletions src/environment/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,23 @@ function addSourceCode({ event }: TestEnvironmentCircusEvent) {
let code = '';
if (event.name === 'add_hook') {
const { hookType, fn } = event;
code = `${hookType}(${fn});`;
const functionCode = String(fn);

if (code.includes("during setup, this cannot be null (and it's fine to explode if it is)")) {
if (
functionCode.includes("during setup, this cannot be null (and it's fine to explode if it is)")
) {
code = '';
realm.runtimeContext
.getCurrentMetadata()
.set('displayName', 'Reset mocks, modules and timers (Jest)');
} else {
code = `${hookType}(${autoIndent(functionCode)});`;
}
}

if (event.name === 'add_test') {
const { testName, fn } = event;
code = `test(${JSON.stringify(testName)}, ${fn});`;
code = `test(${JSON.stringify(testName)}, ${autoIndent(String(fn))});`;
}

if (code) {
Expand Down
9 changes: 8 additions & 1 deletion src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { bunyamin, threadGroups } from 'bunyamin';
import { bunyamin, type BunyaminLogRecordFields, isDebug, threadGroups } from 'bunyamin';

export const log = bunyamin.child({
cat: 'jest-allure2-reporter',
tid: 'jest-allure2-reporter',
});

const nofields: BunyaminLogRecordFields = {};
const noop = () => nofields;

export const optimizeForTracing = isDebug('jest-allure2-reporter')
? <T extends (...arguments_: any[]) => BunyaminLogRecordFields>(function_: T): T => function_
: () => noop;

threadGroups.add({
id: 'jest-allure2-reporter',
displayName: 'jest-allure2-reporter',
Expand Down
10 changes: 2 additions & 8 deletions src/options/default/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as sourceCode from '../../source-code';
import type { ReporterConfig } from '../types';
import * as common from '../common';
import * as custom from '../custom';
import * as helpers from '../helpers';
import * as sourceCode from '../source-code';

import { categories } from './categories';
import { testRun } from './testRun';
Expand All @@ -24,19 +24,13 @@ export function defaultOptions(): ReporterConfig {
enabled: true,
plugins: sourceCode,
}),
markdown: {
enabled: true,
keepSource: true,
remarkPlugins: ['remark-gfm'],
rehypePlugins: ['rehype-highlight'],
},
helpers: custom.helpers(helpers)!,
testRun: custom.testCase(testRun),
testFile: custom.testCase(testFile),
testCase: custom.testCase(testCase),
testStep: custom.testStep(testStep),
categories: common.constant(categories),
environment: () => ({}),
executor: ({ $ }) => $.getExecutorInfo(true),
executor: () => ({}),
};
}
2 changes: 1 addition & 1 deletion src/options/default/testRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { compose2 } from '../common';
export const testRun: TestCaseCustomizer<TestRunExtractorContext> = {
ignored: true,
historyId: ({ testRunMetadata, result }) => testRunMetadata.historyId ?? result.fullName,
fullName: async ({ $, testRunMetadata }) =>
fullName: ({ $, testRunMetadata }) =>
testRunMetadata.fullName ?? $.manifest(['name'], 'untitled project'),
displayName: ({ testRunMetadata }) => testRunMetadata.displayName ?? '(test run)',
description: ({ testRunMetadata }) => testRunMetadata.description?.join('\n\n') ?? '',
Expand Down
6 changes: 0 additions & 6 deletions src/options/extendOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ export function extendOptions(
contentHandler: custom?.attachments?.contentHandler ?? base.attachments.contentHandler,
fileHandler: custom?.attachments?.fileHandler ?? base.attachments.fileHandler,
},
markdown: {
enabled: custom?.markdown?.enabled ?? base.markdown.enabled,
keepSource: custom?.markdown?.keepSource ?? base.markdown.keepSource,
remarkPlugins: [...base.markdown.remarkPlugins, ...(custom?.markdown?.remarkPlugins ?? [])],
rehypePlugins: [...base.markdown.rehypePlugins, ...(custom?.markdown?.rehypePlugins ?? [])],
},
sourceCode: custom?.sourceCode
? mergeSourceCodeConfigs(base.sourceCode, customizers.sourceCode(custom.sourceCode))
: base.sourceCode,
Expand Down
26 changes: 0 additions & 26 deletions src/options/helpers/executor/index.ts

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2cc5357

Please sign in to comment.