Skip to content

Commit

Permalink
add ability to get current allure api for mocha when config is provid…
Browse files Browse the repository at this point in the history
…ed (fixes #668, via #730)
  • Loading branch information
epszaw authored Aug 2, 2023
1 parent 628768d commit 38366df
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 60 deletions.
4 changes: 3 additions & 1 deletion packages/allure-decorators/test/fixtures/specs/baseTest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { decorate } from "../../../";
import { allure, MochaAllure } from "allure-mocha/runtime";
import { allureGetter, MochaAllure } from "allure-mocha/runtime";

export class BaseTest {
public static readonly TEST_URL = "https://custom.domain.com";

public before() {
const allure = allureGetter();

decorate<MochaAllure>(allure);
}
}
24 changes: 14 additions & 10 deletions packages/allure-mocha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

This project implements Allure integration with Mocha framework.

**Allure API doesn't work in parallel mode**! If you want to use the functionality, please switch back to single thread mode!
**Allure API doesn't work in parallel mode**! If you want to use the functionality, please switch
back to single thread mode!

## Installation

Expand Down Expand Up @@ -40,33 +41,34 @@ If you want to provide extra information, such as steps and attachments, import
into your code:

```javascript
const allureMocha = require("allure-mocha/runtime");
const { allure } = require("allure-mocha/runtime");

it("is a test", () => {
allureMocha.allure.epic("Some info");
allure.epic("Some info");
});
```

### Parameters usage

```ts
import allureMocha from "allure-mocha/runtime";
const { allure } = require("allure-mocha/runtime");

it("is a test", () => {
allureMocha.allure.parameter("parameterName", "parameterValue");
allure.parameter("parameterName", "parameterValue");
});
```

Also addParameter takes an third optional parameter with the hidden and excluded options:
`mode: "hidden" | "masked"` - `masked` hide parameter value to secure sensitive data, and `hidden` entirely hide parameter from report
`mode: "hidden" | "masked"` - `masked` hide parameter value to secure sensitive data, and `hidden`
entirely hide parameter from report

`excluded: true` - excludes parameter from the history

```ts
import allureMocha from "allure-mocha/runtime";
import { allure } from "allure-mocha/runtime";

it("is a test", () => {
allureMocha.allure.parameter("parameterName", "parameterValue", {
allure.parameter("parameterName", "parameterValue", {
mode: "hidden",
excluded: true,
});
Expand All @@ -75,8 +77,10 @@ it("is a test", () => {

## Decorators Support

To make tests more readable and avoid explicit API calls, you can use a special extension - [ts-test-decorators](https://github.com/sskorol/ts-test-decorators).
To make tests more readable and avoid explicit API calls, you can use a special
extension - [ts-test-decorators](https://github.com/sskorol/ts-test-decorators).

## Examples

[mocha-allure-example](https://github.com/vovsemenv/mocha-allure-example) - minimal setup for using mocha with allure
[mocha-allure-example](https://github.com/vovsemenv/mocha-allure-example) - minimal setup for using
mocha with allure
2 changes: 1 addition & 1 deletion packages/allure-mocha/runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { MochaAllure } from "./dist/MochaAllure";
export { MochaAllureReporter } from "./dist/MochaAllureReporter";
export { allure } from "./dist/MochaAllureReporter";
export { allure, allureGetter } from "./dist/MochaAllureReporter";
6 changes: 6 additions & 0 deletions packages/allure-mocha/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Object.defineProperty(module.exports, "allure", {
return _MochaAllureReporter.allure;
},
});
Object.defineProperty(module.exports, "allureGetter", {
enumerable: true,
get: function () {
return _MochaAllureReporter.allureGetter;
},
});
Object.defineProperty(module.exports, "MochaAllureReporter", {
enumerable: true,
get: function () {
Expand Down
60 changes: 60 additions & 0 deletions packages/allure-mocha/src/MochaAllureGateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { MochaAllure } from "./MochaAllure";

const METHODS_TO_WRAP: (keyof MochaAllure)[] = [
"epic",
"feature",
"story",
"suite",
"parentSuite",
"subSuite",
"label",
"parameter",
"link",
"issue",
"tms",
"description",
"descriptionHtml",
"owner",
"severity",
"layer",
"id",
"tag",
"writeEnvironmentInfo",
"writeCategoriesDefinitions",
"attachment",
"testAttachment",
"logStep",
"step",
];

export class MochaAllureGateway {
allureGetter: () => MochaAllure;

constructor(allureGetter: () => MochaAllure) {
this.allureGetter = allureGetter;

METHODS_TO_WRAP.forEach((method) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this[method] = this.wrapMethod(method);
});
}

private wrapMethod(methodName: keyof MochaAllure) {
return (...args: any[]) => {
const allure = this.allureGetter();

if (!allure) {
// eslint-disable-next-line no-console
console.error(
`MochaAllure: "${methodName}" can't be used in parallel mode! To use Allure Runtime API, please, switch back to single thread mode.`,
);
return;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
allure[methodName](...args);
};
}
}
15 changes: 10 additions & 5 deletions packages/allure-mocha/src/MochaAllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AllureConfig, AllureRuntime } from "allure-js-commons";
import * as Mocha from "mocha";
import { AllureReporter } from "./AllureReporter";
import { MochaAllure } from "./MochaAllure";
import { ParallelMochaAllure } from "./ParallelMochaAllure";
import { MochaAllureGateway } from "./MochaAllureGateway";

const {
EVENT_SUITE_BEGIN,
Expand All @@ -16,9 +16,13 @@ const {
EVENT_HOOK_END,
} = Mocha.Runner.constants;

// eslint-disable-next-line
let mochaAllure: MochaAllure;

export const allureGetter = () => mochaAllure;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export let allure: MochaAllure = new ParallelMochaAllure();
export const allure: MochaAllure = new MochaAllureGateway(allureGetter);

type ParallelRunner = Mocha.Runner & {
linkPartialObjects?: (val: boolean) => ParallelRunner;
Expand All @@ -35,10 +39,11 @@ export class MochaAllureReporter extends Mocha.reporters.Base {
...opts.reporterOptions,
resultsDir,
};
const runtime = new AllureRuntime(allureConfig);

this.coreReporter = new AllureReporter(new AllureRuntime(allureConfig));
this.coreReporter = new AllureReporter(runtime);

allure = this.coreReporter.getImplementation();
mochaAllure = this.coreReporter.getImplementation();

if (runner.linkPartialObjects) {
runner.linkPartialObjects(true);
Expand Down
43 changes: 0 additions & 43 deletions packages/allure-mocha/src/ParallelMochaAllure.ts

This file was deleted.

0 comments on commit 38366df

Please sign in to comment.