diff --git a/package-e2e/api.test.ts b/package-e2e/api.test.ts index d1e7cc3..feebaae 100644 --- a/package-e2e/api.test.ts +++ b/package-e2e/api.test.ts @@ -1,4 +1,5 @@ import { + IAllureRuntime, Attachment, FileAttachment, Step, @@ -37,6 +38,9 @@ $Severity('critical'); $Tag('e2e'); $TmsLink('TEST-456'); test('typings of jest-allure2-reporter/api', async () => { + assertType(allure); + assertType(allure.$bind()); + assertType(allure.$bind({ metadata: false, time: true })); allure.description('This is a _description_ generated in runtime'); allure.descriptionHtml('This is a description generated in runtime'); allure.status('failed'); diff --git a/src/runtime/AllureRuntime.ts b/src/runtime/AllureRuntime.ts index 2261515..676bab0 100644 --- a/src/runtime/AllureRuntime.ts +++ b/src/runtime/AllureRuntime.ts @@ -30,7 +30,11 @@ import { processMaybePromise } from '../utils/processMaybePromise'; import { wrapFunction } from '../utils/wrapFunction'; import { formatString } from '../utils/formatString'; -import type { IAllureRuntime, ParameterOptions } from './IAllureRuntime'; +import type { + AllureRuntimeBindOptions, + IAllureRuntime, + ParameterOptions, +} from './IAllureRuntime'; import type { IAttachmentsHandler } from './AttachmentsHandler'; export type AllureRuntimeConfig = { @@ -39,6 +43,10 @@ export type AllureRuntimeConfig = { nowProvider: () => number; }; +const constant = + (value: T) => + () => + value; const noop = (..._arguments: unknown[]) => void 0; export class AllureRuntime implements IAllureRuntime { @@ -57,11 +65,15 @@ export class AllureRuntime implements IAllureRuntime { this.#now = config.nowProvider; } - bind(config: Partial): AllureRuntime { + $bind(options?: AllureRuntimeBindOptions): AllureRuntime { + const { metadata = true, time = false } = options ?? {}; + return new AllureRuntime({ - attachmentsHandler: config.attachmentsHandler ?? this.#attachmentsHandler, - metadataProvider: config.metadataProvider ?? this.#metadataProvider, - nowProvider: config.nowProvider ?? this.#now, + attachmentsHandler: this.#attachmentsHandler, + metadataProvider: metadata + ? constant(this.#metadata) + : this.#metadataProvider, + nowProvider: time ? constant(this.#now()) : this.#now, }); } diff --git a/src/runtime/IAllureRuntime.ts b/src/runtime/IAllureRuntime.ts index 59c4e70..fe43ba3 100644 --- a/src/runtime/IAllureRuntime.ts +++ b/src/runtime/IAllureRuntime.ts @@ -9,6 +9,12 @@ import type { import type { Function_, MaybePromise } from '../utils/types'; export interface IAllureRuntime { + /** + * Advanced API for attaching metadata to the same step or test. + * Useful when your artifacts are delayed and are created asynchronously. + */ + $bind(options?: AllureRuntimeBindOptions): IAllureRuntime; + description(value: string): void; descriptionHtml(value: string): void; @@ -89,3 +95,10 @@ export type AttachmentOptions = { export type ParameterOrString = string | Omit; export type ParameterOptions = Pick; + +export type AllureRuntimeBindOptions = { + /** @default true */ + metadata?: boolean; + /** @default false */ + time?: boolean; +};