/* eslint-disable @typescript-eslint/no-empty-interface */ import type {AggregatedResult, Config, TestCaseResult, TestResult} from '@jest/reporters'; import JestMetadataReporter from 'jest-metadata/reporter'; declare module 'jest-allure2-reporter' { // region Options export interface ReporterOptions { /** * Extend the base configuration with custom options. * You can apply multiple extensions in a chain. */ extends?: MaybeArray; /** * Overwrite the results directory if it already exists. * @default true */ overwrite?: boolean; /** * Specify where to output test result files. * Please note that the results directory is not a ready-to-view Allure report. * You'll need to generate the report using the `allure` CLI. * * @default 'allure-results' */ resultsDir?: string; /** * Inject Allure's global variables into the test environment. * Those who don't want to pollute the global scope can disable this option * and import the `jest-allure2-reporter/api` module in their test files. * * @default true */ injectGlobals?: boolean; /** * Configure how external attachments are attached to the report. */ attachments?: AttachmentsOptions; /** * Tweak the way source code and docblocks are extracted from test files. */ sourceCode?: SourceCodeProcessorOptions; /** * Configures the defect categories for the report. * * By default, the report will have the following categories: * `Product defects`, `Test defects` based on the test case status: * `failed` and `broken` respectively. */ categories?: CategoriesCustomizer; /** * Configures the environment information that will be reported. */ environment?: EnvironmentCustomizer; /** * Configures the executor information that will be reported. */ executor?: ExecutorCustomizer; /** * Customize extractor helpers object to use later in the customizers. */ helpers?: HelpersCustomizer; /** * Customize how to report test runs (sessions) as pseudo-test cases. * This is normally used to report broken global setup and teardown hooks, * and to provide additional information about the test run. */ testRun?: TestCaseCustomizer; /** * Customize how to report test files as pseudo-test cases. * This is normally used to report broken test files, so that you can be aware of them, * but advanced users may find other use cases. */ testFile?: TestCaseCustomizer; /** * Customize how test cases are reported: names, descriptions, labels, status, etc. */ testCase?: TestCaseCustomizer; /** * Customize how individual test steps are reported. */ testStep?: TestStepCustomizer; /** * Custom AllureWriter implementation or configuration. * * @example './my-writer.js' * @example MyWriterClass * @example new MyWriter({ url: 'mongodb://...' }) * @example ['./database-writer.js', { connectionString: 'mongodb://...' }] * @example [MyWriterClass, { batchSize: 100 }] */ writer?: string | Function | object | [string | Function, any]; } export interface AttachmentsOptions { /** * Defines a subdirectory within the {@link ReporterOptions#resultsDir} where attachments will be stored. * Use absolute path if you want to store attachments outside the {@link ReporterOptions#resultsDir} directory. * @default 'attachments' */ subDir?: string; /** * Specifies default strategy for attaching files to the report by their path. * - `copy` - copy the file to {@link AttachmentsOptions#subDir} * - `move` - move the file to {@link AttachmentsOptions#subDir} * - `ref` - use the file path as is * @default 'ref' * @see {@link AllureRuntime#createFileAttachment} */ fileHandler?: BuiltinFileAttachmentHandler | string; /** * Specifies default strategy for attaching dynamic content to the report. * Uses simple file writing by default. * @default 'write' * @see {@link AllureRuntime#createContentAttachment} */ contentHandler?: BuiltinContentAttachmentHandler | string; } /** @see {@link AttachmentsOptions#fileHandler} */ export type BuiltinFileAttachmentHandler = 'copy' | 'move' | 'ref'; /** @see {@link AttachmentsOptions#contentHandler} */ export type BuiltinContentAttachmentHandler = 'write'; export interface SourceCodeProcessorOptions { enabled?: boolean; plugins?: Record; } // endregion // region Customizers export type SourceCodePluginCustomizer = PropertyExtractor>; export interface SourceCodePlugin { readonly name: string; extractDocblock?(context: Readonly): MaybePromise; extractSourceCode?(location: Readonly, includeComments: boolean): MaybePromise; } export interface DocblockExtractionContext extends AllureTestItemSourceLocation { transformedCode?: string; } export interface TestCaseCustomizer { /** * Extractor to generate a unique identifier for the test case file. * Do not use it unless you need predictable JSON paths for some demo purposes. */ uuid?: PropertyCustomizer; /** * Extractor to omit test cases from the report. */ ignored?: PropertyCustomizer; /** * Test case ID extractor to fine-tune Allure's history feature. * @example ({ package, file, test }) => `${package.name}:${file.path}:${test.fullName}` * @example ({ test }) => `${test.identifier}:${test.title}` * @see https://wix-incubator.github.io/jest-allure2-reporter/docs/config/history/#test-case-id */ historyId?: PropertyCustomizer; /** * Extractor for the default test or step name. * @default ({ test }) => test.title */ displayName?: PropertyCustomizer; /** * Extractor for the full test case name. * @default ({ test }) => test.fullName */ fullName?: PropertyCustomizer; /** * Extractor for the test case start timestamp. */ start?: PropertyCustomizer; /** * Extractor for the test case stop timestamp. */ stop?: PropertyCustomizer; /** * Extractor for the test case description. * @example ({ testCaseMetadata }) => '```js\n' + testCaseMetadata.transformedCode + '\n```' */ description?: PropertyCustomizer; /** * Extractor for the test case description in HTML format. * @example ({ testCaseMetadata }) => '
' + testCaseMetadata.transformedCode + '
' */ descriptionHtml?: PropertyCustomizer; /** * Extractor for the test case stage. */ stage?: PropertyCustomizer; /** * Extractor for the test case status. * @see https://wix-incubator.github.io/jest-allure2-reporter/docs/config/statuses/ * @example ({ value }) => value === 'broken' ? 'failed' : value */ status?: PropertyCustomizer; /** * Extractor for the test case status details. */ statusDetails?: PropertyCustomizer>; /** * Customize Allure labels for the test case. * * @example * { * suite: ({ file }) => file.path, * subSuite: ({ test }) => test.ancestorTitles[0], * } */ labels?: LabelsCustomizer; /** * Resolve issue links for the test case. * * @example * { * issue: ({ value }) => ({ * type: 'issue', * name: value.name ?? `Open ${value.url} in JIRA`, * url: `https://jira.company.com/${value.url}`, * }), * } */ links?: LinksCustomizer; /** * Customize step or test case attachments. */ attachments?: AttachmentsCustomizer; /** * Customize step or test case parameters. */ parameters?: ParametersCustomizer; } /** * Global customizations for how test steps are reported, e.g. * beforeAll, beforeEach, afterEach, afterAll hooks and custom steps. */ export interface TestStepCustomizer { /** * Extractor to omit test steps from the report. */ ignored?: PropertyCustomizer; /** * Extractor for the step name. * @example ({ value }) => value.replace(/(before|after)(Each|All)/, (_, p1, p2) => p1 + ' ' + p2.toLowerCase()) */ displayName?: PropertyCustomizer; /** * Extractor for the test step start timestamp. */ start?: PropertyCustomizer; /** * Extractor for the test step stop timestamp. */ stop?: PropertyCustomizer; /** * Extractor for the test step stage. * @see https://wix-incubator.github.io/jest-allure2-reporter/docs/config/statuses/ * @example ({ value }) => value === 'running' ? 'pending' : value */ stage?: PropertyCustomizer; /** * Extractor for the test step status. * @see https://wix-incubator.github.io/jest-allure2-reporter/docs/config/statuses/ * @example ({ value }) => value === 'broken' ? 'failed' : value */ status?: PropertyCustomizer; /** * Extractor for the test step status details. */ statusDetails?: PropertyCustomizer>; /** * Customize step or test step attachments. */ attachments?: AttachmentsCustomizer; /** * Customize step or test step parameters. */ parameters?: ParametersCustomizer; } export type CategoriesCustomizer = PropertyCustomizer; export type EnvironmentCustomizer = PropertyCustomizer>; export type ExecutorCustomizer = PropertyCustomizer; export type HelpersCustomizer = | PropertyExtractor> | HelperCustomizersMap; export type HelperCustomizersMap = { [K in keyof Helpers]?: KeyedHelperCustomizer; }; export type KeyedHelperCustomizer = PropertyExtractor>; export type AttachmentsCustomizer = PropertyCustomizer; export type LabelsCustomizer = | PropertyCustomizer | Record>; export type KeyedLabelCustomizer = | MaybeNullish> | KeyedLabelExtractor export type KeyedLabelExtractor = PropertyExtractor< Context, MaybeNullish>, MaybePromise>> >; export type LinksCustomizer = | PropertyCustomizer | Record>; export type KeyedLinkCustomizer = | MaybeNullish | KeyedLinkExtractor; export type KeyedLinkExtractor = PropertyExtractor< Context, MaybeNullish>, MaybePromise>> >; export type ParametersCustomizer = | PropertyCustomizer | Record>; export type KeyedParameterCustomizer = PropertyCustomizer< Context, Parameter, Primitive | Partial >; export type PropertyCustomizer = Result | PropertyExtractor, MaybePromise>; // endregion // region Extractors export type PropertyExtractor< Context, Value, Result = Value, > = (context: PropertyExtractorContext) => Result; export type PropertyExtractorContext = Readonly; export type PromisedProperties = { readonly [K in keyof T]: MaybePromise; }; export interface GlobalExtractorContext { $: Helpers; globalConfig: Config.GlobalConfig; reporterConfig: unknown; } export interface TestItemExtractorContext extends GlobalExtractorContext { result: Partial>; testRunMetadata: AllureTestRunMetadata; } export interface TestRunExtractorContext extends TestItemExtractorContext { aggregatedResult: AggregatedResult; } export interface TestFileExtractorContext extends TestItemExtractorContext { filePath: string[]; testFile: TestResult; testFileMetadata: AllureTestFileMetadata; } export interface TestCaseExtractorContext extends TestItemExtractorContext { filePath: string[]; testCase: TestCaseResult; testCaseMetadata: AllureTestCaseMetadata; testFileMetadata: AllureTestFileMetadata; } export interface TestStepExtractorContext extends TestItemExtractorContext { aggregatedResult?: AggregatedResult; filePath?: string[]; testCase?: TestCaseResult; testCaseMetadata?: AllureTestCaseMetadata; testFile?: TestResult; testFileMetadata?: AllureTestFileMetadata; testStepMetadata: AllureTestStepMetadata; } export interface Helpers extends HelpersAugmentation { /** * Provides an optimized way to navigate through the test file content. * Accepts a file path in a string or a split array format. * @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; /** * Extracts the source code of the current test case or step. * @param location - the location of the source code to extract. * @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.sourceLocation) */ extractSourceCode(location: AllureTestItemSourceLocation | undefined, includeComments?: boolean): Promise; /** * 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. * * @example * ({ $ }) => $.manifest('', m => m.version) * @example * ({ $ }) => $.manifest('jest', jest => jest.version) * @example * ({ $ }) => (await $.manifest()).version * @example * ({ $ }) => (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; } export interface FileNavigator { getContent(): string; getLines(): string[]; getLineCount(): number; getPosition(): [number, number, number]; jump(lineNumber: number): boolean; jumpToPosition(position: number): boolean; moveUp(countOfLines?: number): boolean; moveDown(countOfLines?: number): boolean; readLine(lineNumber?: number): string; } export interface ManifestHelper { (packageName?: string): Promise | undefined>; (extractor: string[] | ManifestHelperExtractor): Promise; (extractor: string[] | ManifestHelperExtractor, defaultValue: T): Promise; (packageName: string, extractor: MaybeArray | ManifestHelperExtractor): Promise; (packageName: string, extractor: MaybeArray | ManifestHelperExtractor, defaultValue: T): Promise; } export type ManifestHelperExtractor = (manifest: Record) => T; /** * Represents the result of extracting source code from a file. * Can be used for reporting in HTML and Markdown formats. */ export interface ExtractSourceCodeHelperResult { /** * The extracted source code. */ code?: string; /** * The programming language of the source code, usually required for syntax highlighting. */ language?: string; /** * The name of the file from which the source code was extracted. * Code preview components may use this property to display the file name. */ fileName?: string; /** * The starting line number of the extracted source code. * Line numbers are 1-based, meaning the first line starts at 1. * Code preview components may use this property to highlight the code. */ startLine?: number; /** * The starting column number of the extracted source code. * Column numbers are 1-based, meaning the first column starts at 1. * Code preview components may use this property to put the cursor in the right place. */ startColumn?: number; /** * The ending line number of the extracted source code. * Line numbers are 1-based, meaning the first line starts at 1. * Code preview components may use this property to highlight the code. */ endLine?: number; /** * The ending column number of the extracted source code. * Column numbers are 1-based, meaning the first column starts at 1. */ endColumn?: number; } export interface StripAnsiHelper { /** * 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!' }) */ (textOrObject: R): R; } // endregion // region Custom Metadata export interface AllureTestItemMetadata { /** * File attachments to be added to the test case, test step or a test file. */ attachments?: Attachment[]; /** * Property path to the current step metadata object. * @see {steps} * @example ['steps', '0'] */ currentStep?: AllureTestStepPath; /** * Parsed docblock: comments and pragmas. */ docblock?: AllureTestItemDocblock; /** * Title of the test case or test step. */ displayName?: string; /** * Custom history ID to distinguish between tests and their retry attempts. */ historyId?: Primitive; /** * Key-value pairs to disambiguate test cases or to provide additional information. */ parameters?: Parameter[]; /** * Location (file, line, column) of the test case, test step or a hook. */ sourceLocation?: AllureTestItemSourceLocation; /** * Indicates test item execution progress. */ stage?: Stage; /** * Start timestamp in milliseconds. */ start?: number; /** * Test result: failed, broken, passed, skipped or unknown. */ status?: Status; /** * Extra information about the test result: message and stack trace. */ statusDetails?: StatusDetails; /** * Recursive data structure to represent test steps for more granular reporting. */ steps?: AllureNestedTestStepMetadata[]; /** * Stop timestamp in milliseconds. */ stop?: number; /** * Transformed code of the test case, test step or a hook. */ transformedCode?: string; } export type AllureTestStepPath = string[]; export type AllureNestedTestStepMetadata = Omit; /** @inheritDoc */ export interface AllureTestStepMetadata extends AllureTestItemMetadata { /** * Steps produced by Jest hooks will have this property set. * User-defined steps don't have this property. */ hookType?: 'beforeAll' | 'beforeEach' | 'afterEach' | 'afterAll'; } /** @inheritDoc */ export interface AllureTestCaseMetadata extends AllureTestItemMetadata { /** * Markdown description of the test case or test file. */ description?: string[]; /** * Raw HTML description of the test case or test file. */ descriptionHtml?: string[]; fullName?: string; labels?: Label[]; links?: Link[]; } /** @inheritDoc */ export interface AllureTestFileMetadata extends AllureTestCaseMetadata {} /** @inheritDoc */ export interface AllureTestRunMetadata extends AllureTestCaseMetadata { config: unknown; loadedFiles: string[]; sourceLocation?: never; transformedCode?: never; } export interface AllureTestItemSourceLocation { fileName?: string; lineNumber?: number; columnNumber?: number; } export interface AllureTestItemDocblock { comments?: string; pragmas?: Record; } // endregion // region Extensibility export interface HelpersAugmentation { // Use to extend Helpers } // endregion // region Allure Test Data export interface AllureTestCaseResult { uuid?: string; ignored?: boolean; historyId: Primitive; displayName: string; fullName: string; start: number; stop: number; description?: string; descriptionHtml?: string; stage: Stage; status: Status; statusDetails?: StatusDetails; steps?: AllureTestStepResult[]; labels?: Label[]; links?: Link[]; attachments?: Attachment[]; parameters?: Parameter[]; } export interface AllureTestStepResult { ignored?: boolean; hookType?: AllureTestStepMetadata['hookType']; displayName: string; start: number; stop: number; stage: Stage; status: Status; statusDetails?: StatusDetails; steps?: AllureTestStepResult[]; attachments?: Attachment[]; parameters?: Parameter[]; } // endregion //region Allure Vendor types export interface Attachment { name: string; type: string; source: string; } export interface Category { name?: string; messageRegex?: string | RegExp; traceRegex?: string | RegExp; matchedStatuses?: Status[]; flaky?: boolean; } export interface ExecutorInfo { name?: string; type?: | 'jenkins' | 'bamboo' | 'teamcity' | 'gitlab' | 'github' | 'circleci' | string; url?: string; buildOrder?: number; buildName?: string; buildUrl?: string; reportUrl?: string; reportName?: string; } export interface Label { name: LabelName; value: string; } export type LabelName = | KnownLabelName | string; export type KnownLabelName = | 'epic' | 'feature' | 'owner' | 'package' | 'parentSuite' | 'severity' | 'story' | 'subSuite' | 'suite' | 'tag' | 'testClass' | 'testMethod' | 'thread'; export interface Link { name?: string; url: string; type?: LinkType; } export type LinkType = KnownLinkType | string; export type KnownLinkType = 'issue' | 'tms'; export interface Parameter { name: string; value: Primitive; excluded?: boolean; mode?: ParameterMode; } export type ParameterMode = 'hidden' | 'masked' | 'default'; export type Severity = 'blocker' | 'critical' | 'normal' | 'minor' | 'trivial'; export type Stage = 'scheduled' | 'running' | 'finished' | 'pending' | 'interrupted'; export type Status = 'failed' | 'broken' | 'passed' | 'skipped' | 'unknown'; export interface StatusDetails { message?: string; trace?: string; } //endregion export type Primitive = string | number | boolean | null | undefined; export type MaybePromise = T | Promise; export type MaybeArray = T | T[]; export type MaybeNullish = T | null | undefined; export type MaybeFunction = T | ((...args: any[]) => T); export type MaybeWithOptions = T | [T, unknown?]; } export default class JestAllure2Reporter extends JestMetadataReporter { constructor(globalConfig: Config.GlobalConfig, options: ReporterOptions); }