Skip to content

Commit

Permalink
mocha legacy runtime api (via #986)
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw authored Jun 3, 2024
1 parent c30e5a0 commit 7f70be3
Show file tree
Hide file tree
Showing 58 changed files with 1,011 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ export class ReporterRuntime {
this.writeAttachmentForItem(message.data, step ?? root);
};

private writeAttachmentForItem = (attachment: RawAttachment, item: StepResult | TestResult | FixtureResult) => {
writeAttachmentForItem = (attachment: RawAttachment, item: StepResult | TestResult | FixtureResult) => {
const attachmentFilename = this.buildAttachmentFileName(attachment);

this.writer.writeAttachment(
Expand Down
6 changes: 3 additions & 3 deletions packages/allure-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"types": "./dist/types/index.d.ts"
},
"./runtime": {
"import": "./dist/esm/runtime.js",
"require": "./dist/cjs/runtime.js",
"types": "./dist/types/runtime.d.ts"
"import": "./dist/esm/legacy.js",
"require": "./dist/cjs/legacy.js",
"types": "./dist/types/legacy.d.ts"
}
},
"main": "./dist/cjs/index.js",
Expand Down
6 changes: 5 additions & 1 deletion packages/allure-mocha/src/MochaAllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "allure-js-commons/sdk/reporter";
import { setGlobalTestRuntime } from "allure-js-commons/sdk/runtime";
import { MochaTestRuntime } from "./MochaTestRuntime";
import { setLegacyApiRuntime } from "./legacyUtils.js";
import { getInitialLabels, getSuitesOfMochaTest, resolveParallelModeSetupFile } from "./utils.js";

const {
Expand All @@ -34,12 +35,15 @@ export class MochaAllureReporter extends Mocha.reporters.Base {
super(runner, opts);

const { resultsDir = "allure-results", writer, ...restOptions }: Config = opts.reporterOptions || {};

this.runtime = new ReporterRuntime({
writer: writer || new FileSystemWriter({ resultsDir }),
...restOptions,
});
const testRuntime = new MochaTestRuntime(this.runtime);

setGlobalTestRuntime(new MochaTestRuntime(this.runtime));
setGlobalTestRuntime(testRuntime);
setLegacyApiRuntime(this.runtime);

if (opts.parallel) {
opts.require = [...(opts.require ?? []), resolveParallelModeSetupFile()];
Expand Down
159 changes: 159 additions & 0 deletions packages/allure-mocha/src/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import * as commons from "allure-js-commons";
import type { ContentType, ParameterOptions } from "allure-js-commons";
import { Stage, Status } from "allure-js-commons";
import type { Category } from "allure-js-commons/sdk";
import { getStatusFromError, isPromise } from "allure-js-commons/sdk";
import { serialize } from "allure-js-commons/sdk/reporter";
import { getLegacyApiRuntime } from "./legacyUtils.js";

interface StepInterface {
parameter(name: string, value: string): void;

name(name: string): void;
}

interface AttachmentOptions {
contentType: ContentType | string;
fileExtension?: string;
}

class LegacyAllureApi {
epic = (epic: string) => Promise.resolve(commons.epic(epic));
feature = (feature: string) => Promise.resolve(commons.feature(feature));
story = (story: string) => Promise.resolve(commons.story(story));
suite = (name: string) => Promise.resolve(commons.suite(name));
parentSuite = (name: string) => Promise.resolve(commons.parentSuite(name));
subSuite = (name: string) => Promise.resolve(commons.subSuite(name));
label = (name: string, value: string) => Promise.resolve(commons.label(name, value));
parameter = (name: string, value: any, options?: ParameterOptions) =>
Promise.resolve(commons.parameter(name, serialize(value) as string, options));
link = (url: string, name?: string, type?: string) => Promise.resolve(commons.link(url, type, name));
issue = (name: string, url: string) => Promise.resolve(commons.issue(url, name));
tms = (name: string, url: string) => Promise.resolve(commons.tms(url, name));
description = (markdown: string) => Promise.resolve(commons.description(markdown));
descriptionHtml = (html: string) => Promise.resolve(commons.descriptionHtml(html));
owner = (owner: string) => Promise.resolve(commons.owner(owner));
severity = (severity: string) => Promise.resolve(commons.severity(severity));
layer = (layer: string) => Promise.resolve(commons.layer(layer));
id = (allureId: string) => Promise.resolve(commons.allureId(allureId));
tag = (tag: string) => Promise.resolve(commons.tag(tag));
writeEnvironmentInfo = (info: Record<string, string>) => {
getLegacyApiRuntime()?.writer.writeEnvironmentInfo(info);
};
writeCategoriesDefinitions = (categories: Category[]) => {
getLegacyApiRuntime()?.writer.writeCategoriesDefinitions(categories);
};
attachment = (name: string, content: Buffer | string, options: ContentType | string | AttachmentOptions) =>
Promise.resolve(commons.attachment(name, content, typeof options === "string" ? options : options.contentType));
testAttachment = (name: string, content: Buffer | string, options: ContentType | string | AttachmentOptions) => {
const runtime = getLegacyApiRuntime();
const currentTest = runtime?.getCurrentTest();
if (currentTest) {
runtime?.writeAttachmentForItem(
{
name,
content: Buffer.from(content).toString("base64"),
contentType: typeof options === "string" ? options : options.contentType,
encoding: "base64",
fileExtension: typeof options === "string" ? undefined : options.fileExtension,
},
currentTest,
);
}
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
logStep = (name: string, status?: Status) => {
this.step(name, () => {
getLegacyApiRuntime()?.updateStep((s) => (s.status = status));
});
};
// It's sync-first. That's why we can't simply reuse commons.step.
step = <T>(name: string, body: (step: StepInterface) => T): T => {
const runtime = getLegacyApiRuntime();
runtime?.applyRuntimeMessages([
{
type: "step_start",
data: {
name,
start: Date.now(),
},
},
]);
try {
const result = body({
name: this.renameStep,
parameter: this.addStepParameter,
});
if (isPromise(result)) {
const promise = result as Promise<any>;
return promise
.then((v) => {
this.stopStepSuccess();
return v;
})
.catch((e) => {
this.stopStepWithError(e);
throw e;
}) as T;
}
this.stopStepSuccess();
return result;
} catch (e) {
this.stopStepWithError(e);
throw e;
}
};

private renameStep = (name: string) => {
getLegacyApiRuntime()?.applyRuntimeMessages([
{
type: "step_metadata",
data: { name },
},
]);
};

private addStepParameter = (name: string, value: string) => {
getLegacyApiRuntime()?.applyRuntimeMessages([
{
type: "step_metadata",
data: {
parameters: [{ name, value }],
},
},
]);
};

private stopStepSuccess = () => {
getLegacyApiRuntime()?.applyRuntimeMessages([
{
type: "step_stop",
data: {
status: Status.PASSED,
stage: Stage.FINISHED,
stop: Date.now(),
},
},
]);
};

private stopStepWithError = (error: unknown) => {
const { message, stack } = error as Error;
getLegacyApiRuntime()?.applyRuntimeMessages([
{
type: "step_stop",
data: {
status: getStatusFromError(error as Error),
stage: Stage.FINISHED,
stop: Date.now(),
statusDetails: {
message,
trace: stack,
},
},
},
]);
};
}

export const allure = new LegacyAllureApi();
8 changes: 8 additions & 0 deletions packages/allure-mocha/src/legacyUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ReporterRuntime } from "allure-js-commons/sdk/reporter";

const ALLURE_TEST_RUNTIME_KEY = "__allure_mocha_legacy_runtime__";

export const getLegacyApiRuntime = () => (globalThis as any)[ALLURE_TEST_RUNTIME_KEY] as ReporterRuntime;

export const setLegacyApiRuntime = (runtime: ReporterRuntime) =>
((globalThis as any)[ALLURE_TEST_RUNTIME_KEY] = runtime);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// cjs: const { it } = require("mocha");
// cjs: const { allure } = require("allure-mocha/runtime");
// cjs: const { Status } = require("allure-js-commons");
// esm: import { it } from "mocha";
// esm: import { allure } from "allure-mocha/runtime";
// esm: import { Status } from "allure-js-commons";

it("a test run with categories", () => {
allure.writeCategoriesDefinitions([
{
name: "foo",
description: "bar",
messageRegex: "broken",
matchedStatuses: [Status.BROKEN],
},
{
name: "baz",
description: "qux",
messageRegex: "failure",
matchedStatuses: [Status.FAILED],
},
]);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a description", () => {
allure.description("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a description in HTML", () => {
allure.descriptionHtml("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// cjs: const { it } = require("mocha");
// cjs: const { allure } = require("allure-mocha/runtime");
// esm: import { it } from "mocha";
// esm: import { allure } from "allure-mocha/runtime";

it("a test run with env info", () => {
allure.writeEnvironmentInfo({ foo: "bar", baz: "qux" });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { beforeEach, describe, it } from "mocha";
import { allure } from "allure-mocha/runtime";

describe("a suite with before", () => {
beforeEach("an initial name", () => {
allure.displayName("a new name");
});

it("a test affected by a renamed fixture", () => {});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with an epic", () => {
allure.epic("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a feature", () => {
allure.feature("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a story", () => {
allure.story("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a custom label", () => {
allure.label("foo", "bar");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a layer", () => {
allure.layer("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with an owner", () => {
allure.owner("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a blocker", () => {
allure.severity("blocker");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a critical test", () => {
allure.severity("critical");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a minor test", () => {
allure.severity("minor");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a normal test", () => {
allure.severity("normal");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a trivial test", () => {
allure.severity("trivial");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a parent suite", () => {
allure.parentSuite("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a sub-suite", () => {
allure.subSuite("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a suite", () => {
allure.suite("foo");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with tags", () => {
allure.tag("foo");
allure.tag("bar");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with an issue link", () => {
allure.issue("baz", "https://foo.bar");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { it } from "mocha";
import { allure } from "allure-mocha/runtime";

it("a test with a named link", () => {
allure.link("https://foo.bar", "baz");
});
Loading

0 comments on commit 7f70be3

Please sign in to comment.