Skip to content

Commit

Permalink
feat: add allure. method
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Dec 15, 2023
1 parent 9a865a1 commit b9e9b0f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions package-e2e/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
IAllureRuntime,
Attachment,
FileAttachment,
Step,
Expand Down Expand Up @@ -37,6 +38,9 @@ $Severity('critical');
$Tag('e2e');
$TmsLink('TEST-456');
test('typings of jest-allure2-reporter/api', async () => {
assertType<IAllureRuntime>(allure);
assertType<IAllureRuntime>(allure.$bind());
assertType<IAllureRuntime>(allure.$bind({ metadata: false, time: true }));
allure.description('This is a _description_ generated in runtime');
allure.descriptionHtml('This is a <i>description</i> generated in runtime');
allure.status('failed');
Expand Down
22 changes: 17 additions & 5 deletions src/runtime/AllureRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -39,6 +43,10 @@ export type AllureRuntimeConfig = {
nowProvider: () => number;
};

const constant =
<T>(value: T) =>
() =>
value;
const noop = (..._arguments: unknown[]) => void 0;

export class AllureRuntime implements IAllureRuntime {
Expand All @@ -57,11 +65,15 @@ export class AllureRuntime implements IAllureRuntime {
this.#now = config.nowProvider;
}

bind(config: Partial<AllureRuntimeConfig>): 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,
});
}

Expand Down
13 changes: 13 additions & 0 deletions src/runtime/IAllureRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,3 +95,10 @@ export type AttachmentOptions = {
export type ParameterOrString = string | Omit<Parameter, 'value'>;

export type ParameterOptions = Pick<Parameter, 'mode' | 'excluded'>;

export type AllureRuntimeBindOptions = {
/** @default true */
metadata?: boolean;
/** @default false */
time?: boolean;
};

0 comments on commit b9e9b0f

Please sign in to comment.