Skip to content

Commit 7f2606a

Browse files
authored
feat: custom file handlers (#24)
1 parent e238639 commit 7f2606a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1926
-1220
lines changed

api.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './dist/api.js';
1+
export * from './dist/api/index.js';

e2e/configs/default.js

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const jestAllure2ReporterOptions = {
4040
testMethod: ({ testCase }) => testCase.fullName,
4141
owner: ({ value }) => value ?? 'Unknown',
4242
},
43+
links: {
44+
issue: ({ value }) => ({ ...value, url: `https://youtrack.jetbrains.com/issue/${value.url}/` }),
45+
},
4346
},
4447
};
4548

e2e/src/programmatic/grouping/client/auth/LoginScreen.test.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import LoginHelper from '../../../../utils/LoginHelper';
2-
31
/**
42
* Client tests for login screen
53
* @owner Security Team
@@ -8,6 +6,9 @@ import LoginHelper from '../../../../utils/LoginHelper';
86
* @tag smoke
97
*/
108

9+
import LoginHelper from '../../../../utils/LoginHelper';
10+
import {allure} from "../../../../../../src/api";
11+
1112
$Tag('client');
1213
$Epic('Authentication');
1314
$Feature('Login');
@@ -22,12 +23,18 @@ describe('Login screen', () => {
2223
$Story('Validation');
2324
describe('Form Submission', () => {
2425
it('should show error on invalid e-mail format', async () => {
25-
/** @owner Samantha Jones */
26+
/**
27+
* @owner Samantha Jones
28+
* @issue IDEA-235211
29+
*/
2630

2731
await LoginHelper.typeEmail('someone#example.com');
2832
await LoginHelper.typePassword('123456');
29-
expect(LoginHelper.snapshotForm()).toContain('someone#example.com');
30-
expect(LoginHelper.getValidationSummary()).toBe('fixtures/invalid-email.xml');
33+
await allure.step('Hello', () => {
34+
expect(LoginHelper.snapshotForm()).toContain('someone#example.com');
35+
expect(LoginHelper.getValidationSummary()).toBe('fixtures/invalid-email.xml');
36+
allure.status('passed', { message: 'All is good' });
37+
});
3138
});
3239

3340
it('should show error on short or invalid password format', () => {
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
$Tag('server');
2-
$Epic('Authentication');
3-
$Feature('Login');
1+
/**
2+
* Server login controller tests.
3+
* ------------------------------
4+
* @tag server
5+
* @epic Authentication
6+
* @feature Login
7+
*/
48
describe('POST /login', () => {
5-
$Story('Validation');
9+
beforeEach(() => {
10+
/** Environment setup */
11+
// This hook should set up the environment for each test case.
12+
});
13+
614
it('should return 401 if user is not found', () => {
7-
// ...
15+
/** @story Validation */
816
});
917

10-
$Story('Validation');
1118
it('should return 401 if password is incorrect', () => {
12-
// ...
19+
/** @story Validation */
1320
});
1421

15-
$Story('Happy path');
1622
it('should return 200 and user details if login is successful', () => {
17-
// ...
23+
/** @story Happy path */
1824
});
1925
});

e2e/src/utils/LoginHelper.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Step, Attachment, FileAttachment } from 'jest-allure2-reporter/api';
2+
13
class LoginHelper {
24
#email?: string;
35
#password?: string;

index.d.ts

+106-47
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,35 @@ declare module 'jest-allure2-reporter' {
117117
*/
118118
subDir?: string;
119119
/**
120-
* Specifies strategy for attaching files to the report by their path.
120+
* Specifies default strategy for attaching files to the report by their path.
121121
* - `copy` - copy the file to {@link AttachmentsOptions#subDir}
122122
* - `move` - move the file to {@link AttachmentsOptions#subDir}
123123
* - `ref` - use the file path as is
124124
* @default 'ref'
125125
* @see {@link AllureRuntime#createFileAttachment}
126126
*/
127-
fileHandler?: BuiltinFileHandler;
127+
fileHandler?: BuiltinFileAttachmentHandler | string;
128+
/**
129+
* Specifies default strategy for attaching dynamic content to the report.
130+
* Uses simple file writing by default.
131+
*/
132+
contentHandler?: BuiltinContentAttachmentHandler | string;
128133
};
129134

130135
/** @see {@link AttachmentsOptions#fileHandler} */
131-
export type BuiltinFileHandler = 'copy' | 'move' | 'ref';
136+
export type BuiltinFileAttachmentHandler = 'copy' | 'move' | 'ref';
137+
138+
/** @see {@link AttachmentsOptions#contentHandler} */
139+
export type BuiltinContentAttachmentHandler = 'write';
132140

133141
/**
134142
* Global customizations for how test cases are reported
135143
*/
136144
export interface TestCaseCustomizer {
145+
/**
146+
* Extractor to omit test file cases from the report.
147+
*/
148+
hidden: TestCaseExtractor<boolean>;
137149
/**
138150
* Test case ID extractor to fine-tune Allure's history feature.
139151
* @example ({ package, file, test }) => `${package.name}:${file.path}:${test.fullName}`
@@ -228,9 +240,9 @@ declare module 'jest-allure2-reporter' {
228240
*/
229241
export interface TestFileCustomizer {
230242
/**
231-
* Extractor to omit test cases from the report.
243+
* Extractor to omit test file cases from the report.
232244
*/
233-
ignored: TestFileExtractor<boolean>;
245+
hidden: TestFileExtractor<boolean>;
234246
/**
235247
* Test file ID extractor to fine-tune Allure's history feature.
236248
* @default ({ filePath }) => filePath.join('/')
@@ -331,6 +343,10 @@ declare module 'jest-allure2-reporter' {
331343
export type ResolvedTestStepCustomizer = Required<TestStepCustomizer>;
332344

333345
export interface TestStepCustomizer {
346+
/**
347+
* Extractor to omit test steps from the report.
348+
*/
349+
hidden: TestStepExtractor<boolean>;
334350
/**
335351
* Extractor for the step name.
336352
* @example ({ value }) => value.replace(/(before|after)(Each|All)/, (_, p1, p2) => p1 + ' ' + p2.toLowerCase())
@@ -410,96 +426,145 @@ declare module 'jest-allure2-reporter' {
410426
value: T | undefined;
411427
}
412428

413-
export interface GlobalExtractorContext<T = unknown>
429+
export interface GlobalExtractorContext<T = any>
414430
extends ExtractorContext<T>,
415431
GlobalExtractorContextAugmentation {
416432
globalConfig: Config.GlobalConfig;
417433
config: ReporterConfig;
418434
}
419435

420-
export interface TestFileExtractorContext<T = unknown>
436+
export interface TestFileExtractorContext<T = any>
421437
extends GlobalExtractorContext<T>,
422438
TestFileExtractorContextAugmentation {
423439
filePath: string[];
424440
testFile: TestResult;
425-
testFileMetadata: AllureTestCaseMetadata;
441+
testFileDocblock?: DocblockContext;
442+
testFileMetadata: AllureTestFileMetadata;
426443
}
427444

428-
export interface TestCaseExtractorContext<T = unknown>
445+
export interface TestCaseExtractorContext<T = any>
429446
extends TestFileExtractorContext<T>,
430447
TestCaseExtractorContextAugmentation {
431448
testCase: TestCaseResult;
449+
testCaseDocblock?: DocblockContext;
432450
testCaseMetadata: AllureTestCaseMetadata;
433451
}
434452

435-
export interface TestStepExtractorContext<T = unknown>
453+
export interface TestStepExtractorContext<T = any>
436454
extends TestCaseExtractorContext<T>,
437455
TestStepExtractorContextAugmentation {
456+
testStepDocblock?: DocblockContext;
438457
testStepMetadata: AllureTestStepMetadata;
439458
}
440459

441-
export interface AllureTestStepMetadata {
442-
steps?: AllureTestStepMetadata[];
443-
hidden?: boolean;
460+
export interface AllureTestItemSourceLocation {
461+
fileName?: string;
462+
lineNumber?: number;
463+
columnNumber?: number;
464+
}
465+
466+
export type AllureTestStepPath = string[];
467+
468+
export interface AllureTestItemMetadata {
444469
/**
445-
* Source code of the test case, test step or a hook.
470+
* File attachments to be added to the test case, test step or a test file.
446471
*/
447-
code?: string[];
448-
449-
name?: string;
450-
status?: Status;
451-
statusDetails?: StatusDetails;
452-
stage?: Stage;
453472
attachments?: Attachment[];
473+
/**
474+
* Property path to the current step metadata object.
475+
* @see {steps}
476+
* @example ['steps', '0']
477+
*/
478+
currentStep?: AllureTestStepPath;
479+
/**
480+
* Source code of the test case, test step or a hook.
481+
*/
482+
sourceCode?: string;
483+
/**
484+
* Location (file, line, column) of the test case, test step or a hook.
485+
*/
486+
sourceLocation?: AllureTestItemSourceLocation;
487+
/**
488+
* Markdown description of the test case or test file, or plain text description of a test step.
489+
*/
490+
description?: string[];
491+
/**
492+
* Key-value pairs to disambiguate test cases or to provide additional information.
493+
*/
454494
parameters?: Parameter[];
495+
/**
496+
* Indicates test item execution progress.
497+
*/
498+
stage?: Stage;
499+
/**
500+
* Start timestamp in milliseconds.
501+
*/
455502
start?: number;
456-
stop?: number;
457-
}
458-
459-
export interface AllureTestCaseMetadata extends AllureTestStepMetadata {
460503
/**
461-
* Pointer to the child step that is currently being added or executed.
462-
* @example ['steps', '0', 'steps', '0']
463-
* @internal
504+
* Test result: failed, broken, passed, skipped or unknown.
464505
*/
465-
currentStep?: string[];
506+
status?: Status;
466507
/**
467-
* Jest worker ID.
468-
* @internal Used to generate unique thread names.
469-
* @see {import('@noomorph/allure-js-commons').LabelName.THREAD}
508+
* Extra information about the test result: message and stack trace.
470509
*/
471-
workerId?: string;
510+
statusDetails?: StatusDetails;
472511
/**
473-
* Only steps can have names.
512+
* Recursive data structure to represent test steps for more granular reporting.
474513
*/
475-
name?: never;
476-
description?: string[];
514+
steps?: Omit<AllureTestStepMetadata, 'currentStep'>[];
515+
/**
516+
* Stop timestamp in milliseconds.
517+
*/
518+
stop?: number;
519+
}
520+
521+
export interface AllureTestStepMetadata extends AllureTestItemMetadata {
522+
/**
523+
* Steps produced by Jest hooks will have this property set.
524+
* User-defined steps don't have this property.
525+
*/
526+
hookType?: 'beforeAll' | 'beforeEach' | 'afterEach' | 'afterAll';
527+
}
528+
529+
export interface AllureTestCaseMetadata extends AllureTestItemMetadata {
477530
descriptionHtml?: string[];
478531
labels?: Label[];
479532
links?: Link[];
480533
}
481534

482535
export interface AllureTestFileMetadata extends AllureTestCaseMetadata {
483-
currentStep?: never;
536+
code?: never;
537+
steps?: never;
538+
workerId?: string;
539+
}
540+
541+
export interface AllureGlobalMetadata {
542+
config: Pick<ReporterConfig, 'resultsDir' | 'overwrite' | 'attachments' | 'injectGlobals'>;
543+
}
544+
545+
export interface DocblockContext {
546+
comments: string;
547+
pragmas: Record<string, string[]>;
548+
raw: string;
484549
}
485550

486551
export interface GlobalExtractorContextAugmentation {
487-
detectLanguage?(filePath: string, contents: string): string | undefined;
552+
detectLanguage?(contents: string, filePath?: string): string | undefined;
488553
processMarkdown?(markdown: string): Promise<string>;
489554

490-
// This should be extended by plugins
555+
// This may be extended by plugins
491556
}
492557

493558
export interface TestFileExtractorContextAugmentation {
494-
// This should be extended by plugins
559+
// This may be extended by plugins
495560
}
496561

497562
export interface TestCaseExtractorContextAugmentation {
498-
// This should be extended by plugins
563+
// This may be extended by plugins
499564
}
500565

501566
export interface TestStepExtractorContextAugmentation {
502-
// This should be extended by plugins
567+
// This may be extended by plugins
503568
}
504569

505570
export type PluginDeclaration =
@@ -527,11 +592,6 @@ declare module 'jest-allure2-reporter' {
527592
/** Method to extend global context. */
528593
globalContext?(context: GlobalExtractorContext): void | Promise<void>;
529594

530-
/** Method to affect test file metadata before it is created. */
531-
beforeTestFileContext?(
532-
context: Omit<TestFileExtractorContext, 'testFileMetadata'>,
533-
): void | Promise<void>;
534-
535595
/** Method to extend test file context. */
536596
testFileContext?(context: TestFileExtractorContext): void | Promise<void>;
537597

@@ -544,7 +604,6 @@ declare module 'jest-allure2-reporter' {
544604

545605
export type PluginHookName =
546606
| 'globalContext'
547-
| 'beforeTestFileContext'
548607
| 'testFileContext'
549608
| 'testCaseContext'
550609
| 'testStepContext';

0 commit comments

Comments
 (0)