Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Commit

Permalink
fix(contenttype): fixed ContentType export and ability to attach HTML…
Browse files Browse the repository at this point in the history
… docs

`ContentType` is exported as a value isntead of a type again. Attaching an HTML documents should
work as long as the `ContentType.HTMl` is used as the attachmetn type

#155
  • Loading branch information
ryparker committed Feb 10, 2021
1 parent 041d665 commit cbf2c5f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 32 deletions.
11 changes: 8 additions & 3 deletions __tests__/allure-api/attachment.test.js
Expand Up @@ -12,10 +12,15 @@ test('allure.attachment()', () => {
function: () => console.log('Ok it works.')
}, null, 2),
ContentType.JSON);
// Allure.attachment('JPEG-attachment', ContentType.JPEG, '');
// Allure.attachment('PNG-attachment', ContentType.PNG, '');
// allure.attachment('SVG attachment', ContentType.SVG, '');
allure.attachment('JPEG-attachment', '', ContentType.JPEG);
allure.attachment('PNG-attachment', '', ContentType.PNG);
allure.attachment('SVG attachment', '', ContentType.SVG);
allure.attachment('HTML attachment', '<div><p>This is an HTML doc</p></div', ContentType.HTML);

expect(1 + 2).toBe(3);
});

test('HTML is available on ContentType enum', () => {
expect(ContentType.HTML).toStrictEqual('text/html');
});

10 changes: 9 additions & 1 deletion package.json
Expand Up @@ -29,6 +29,14 @@
},
"extends": [
"plugin:jest/recommended"
],
"overrides": [
{
"files": "src/jest-allure-interface.ts",
"rules": {
"@typescript-eslint/no-implicit-any-catch": 1
}
}
]
},
"husky": {
Expand Down Expand Up @@ -124,4 +132,4 @@
"collect",
"analyze"
]
}
}
8 changes: 5 additions & 3 deletions src/allure-reporter.ts
Expand Up @@ -11,15 +11,15 @@ import {
Stage,
Status
} from 'allure-js-commons';
import JestAllureInterface from './jest-allure-interface';
import JestAllureInterface, {ContentType} from './jest-allure-interface';
import {createHash} from 'crypto';
import defaultCategories from './category-definitions';
import {parseWithComments} from 'jest-docblock';
import stripAnsi = require('strip-ansi');
import _ = require('lodash');
import prettier = require('prettier/standalone');
import parser = require('prettier/parser-typescript');
import type {ContentType} from './types';

import type * as jest from '@jest/types';

export default class AllureReporter {
Expand Down Expand Up @@ -228,7 +228,9 @@ export default class AllureReporter {
}

writeAttachment(content: Buffer | string, type: ContentType): string {
return this.allureRuntime.writeAttachment(content, type);
// Because Allure-JS-Commons does not support HTML we can workaround by providing the file extension.
const fileExtension = type === ContentType.HTML ? 'html' : undefined;
return this.allureRuntime.writeAttachment(content, {contentType: type, fileExtension});
}

pushStep(step: AllureStep): void {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -7,7 +7,7 @@ declare global {
export {default} from './allure-node-environment';

export * from 'allure-js-commons';
export type {ContentType} from './types';
export {ContentType} from './jest-allure-interface';
export type {default as StepWrapper} from './step-wrapper';
export type {default as JestAllureInterface} from './jest-allure-interface';

49 changes: 33 additions & 16 deletions src/jest-allure-interface.ts
Expand Up @@ -9,11 +9,32 @@ import {
Severity,
Status,
StepInterface,
isPromise
} from 'allure-js-commons';
isPromise} from 'allure-js-commons';

import StepWrapper from './step-wrapper';
import type AllureReporter from './allure-reporter';
import type {ContentType} from './types';

/**
* Supported content types for Allure attachments.
*
* _This enum was copied and extended from allure-js-commons_
*/
export enum ContentType {
// Allure-js-commons ContentTypes:
TEXT = 'text/plain',
XML = 'application/xml',
CSV = 'text/csv',
TSV = 'text/tab-separated-values',
CSS = 'text/css',
URI = 'text/uri-list',
SVG = 'image/svg+xml',
PNG = 'image/png',
JSON = 'application/json',
WEBM = 'video/webm',
JPEG = 'image/jpeg',
// Custom extensions:
HTML = 'text/html'
}

export default class JestAllureInterface extends Allure {
public jiraUrl: string;
Expand Down Expand Up @@ -91,7 +112,7 @@ export default class JestAllureInterface extends Allure {
return new StepWrapper(this.reporter, allureStep);
}

public step<T>(name: string, body: (step: StepInterface) => any): any {
public async step<T>(name: string, body: (step: StepInterface) => any): Promise<any> {
const wrappedStep = this.startStep(name);
let result;
try {
Expand All @@ -104,19 +125,15 @@ export default class JestAllureInterface extends Allure {
if (isPromise(result)) {
const promise = result as Promise<any>;

return promise
.then(a => {
wrappedStep.endStep();
return a;
})
.catch(error => {
wrappedStep.endStep();
throw error;
});
try {
const resolved = await promise;
wrappedStep.endStep();
return resolved;
} catch (error: any) {
wrappedStep.endStep();
throw error;
}
}

wrappedStep.endStep();
return result;
}

public logStep(
Expand Down
2 changes: 1 addition & 1 deletion src/step-wrapper.ts
@@ -1,5 +1,5 @@
import type {AllureStep, StepInterface, Stage} from 'allure-js-commons';
import type {ContentType} from './types';
import type {ContentType} from './jest-allure-interface';
import {Status} from 'allure-js-commons';
import type AllureReporter from './allure-reporter';

Expand Down
7 changes: 0 additions & 7 deletions src/types.ts

This file was deleted.

0 comments on commit cbf2c5f

Please sign in to comment.