Skip to content

Commit

Permalink
back compatibility with old style jest runtime api (via #955)
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw authored May 21, 2024
1 parent a6a65d1 commit f4821eb
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/allure-jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Use your favorite node package manager to install required packages:

```shell
npm add -D allure-jest allure-js-commons
npm add -D allure-jest
```

If you're using `jest` for testing `node` add following line to your `jest.config.js` file:
Expand Down
20 changes: 9 additions & 11 deletions packages/allure-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import os from "node:os";
import { dirname, sep } from "node:path";
import process from "node:process";
import stripAnsi from "strip-ansi";
import * as allure from "allure-js-commons";
import {
ALLURE_TEST_RUNTIME_KEY,
AllureNodeReporterRuntime,
Expand All @@ -23,22 +24,14 @@ import {
TestRuntime,
getStatusFromError,
getSuitesLabels,
setGlobalTestRuntime,
} from "allure-js-commons/sdk/node";
import { AllureJestConfig } from "./model.js";
import { AllureJestConfig, AllureJestEnvironment } from "./model.js";
import { getTestId, getTestPath } from "./utils.js";

const { ALLURE_HOST_NAME, ALLURE_THREAD_NAME, JEST_WORKER_ID } = process.env;
const hostname = os.hostname();

export interface AllureJestEnvironment extends JestEnvironment {
handleAllureRuntimeMessage(payload: { currentTestName: string; message: RuntimeMessage }): void;
}

export interface LinkMatcher {
type: LinkType | string;
urlTemplate: string;
}

class AllureJestTestRuntime implements TestRuntime {
constructor(
private jestEnvironment: AllureJestEnvironment,
Expand Down Expand Up @@ -249,7 +242,12 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
this.testPath = context.testPath.replace(config.globalConfig.rootDir, "").replace(sep, "");

// @ts-ignore
new AllureJestTestRuntime(this as AllureJestEnvironment, this.global);
const testRuntime = new AllureJestTestRuntime(this as AllureJestEnvironment, this.global);

// @ts-ignore
this.global.allure = allure;

setGlobalTestRuntime(testRuntime);
}

setup() {
Expand Down
8 changes: 6 additions & 2 deletions packages/allure-jest/src/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { JestEnvironmentConfig } from "@jest/environment";
import { Config } from "allure-js-commons/sdk/node";
import { JestEnvironment, JestEnvironmentConfig } from "@jest/environment";
import { Config, RuntimeMessage } from "allure-js-commons/sdk/node";

export interface AllureJestEnvironment extends JestEnvironment {
handleAllureRuntimeMessage(payload: { currentTestName: string; message: RuntimeMessage }): void;
}

export interface AllureJestConfig extends JestEnvironmentConfig {
projectConfig: JestEnvironmentConfig["projectConfig"] & {
Expand Down
20 changes: 20 additions & 0 deletions packages/allure-jest/test/spec/runtime/legacy/attachments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, it } from "@jest/globals";
import { runJestInlineTest } from "../../../utils";

it("handles json attachment", async () => {
const { tests, attachments } = await runJestInlineTest(`
it("json", async () => {
await allure.attachment("Request body", JSON.stringify({ foo: "bar" }), "application/json");
});
`);

expect(tests).toHaveLength(1);
expect(tests[0].attachments).toHaveLength(1);

const [attachment] = tests[0].attachments;

expect(attachment.name).toBe("Request body");
expect(Buffer.from(attachments[attachment.source] as string, "base64").toString("utf8")).toBe(
JSON.stringify({ foo: "bar" }),
);
});
24 changes: 24 additions & 0 deletions packages/allure-jest/test/spec/runtime/legacy/description.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, it } from "@jest/globals";
import { runJestInlineTest } from "../../../utils";

it("sets description", async () => {
const { tests } = await runJestInlineTest(`
it("description", async () => {
await allure.description("foo");
})
`);

expect(tests).toHaveLength(1);
expect(tests[0].description).toBe("foo");
});

it("sets html description", async () => {
const { tests } = await runJestInlineTest(`
it("descriptionHtml", async () => {
await allure.descriptionHtml("foo");
})
`);

expect(tests).toHaveLength(1);
expect(tests[0].descriptionHtml).toBe("foo");
});
15 changes: 15 additions & 0 deletions packages/allure-jest/test/spec/runtime/legacy/historyId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from "@jest/globals";
import { runJestInlineTest } from "../../../utils";

describe("historyId", () => {
it("historyId", async () => {
const { tests } = await runJestInlineTest(`
it("historyId", async () => {
await allure.historyId("foo");
})
`);

expect(tests).toHaveLength(1);
expect(tests[0].historyId).toBe("foo");
});
});
39 changes: 39 additions & 0 deletions packages/allure-jest/test/spec/runtime/legacy/labels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, it } from "@jest/globals";
import { LabelName } from "allure-js-commons";
import { runJestInlineTest } from "../../../utils";

it("sets labels", async () => {
const { tests } = await runJestInlineTest(`
it("label", async () => {
await allure.label("foo", "bar");
await allure.allureId("foo");
await allure.epic("foo");
await allure.feature("foo");
await allure.layer("foo");
await allure.owner("foo");
await allure.parentSuite("foo");
await allure.subSuite("foo");
await allure.suite("foo");
await allure.severity("foo");
await allure.story("foo");
await allure.tag("foo");
await allure.labels({ name: "test", value: "testValue" }, { name: "test2", value: "testValue2" });
})
`);

expect(tests).toHaveLength(1);
expect(tests[0].labels).toContainEqual({ name: "foo", value: "bar" });
expect(tests[0].labels).toContainEqual({ name: LabelName.ALLURE_ID, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.EPIC, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.FEATURE, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.LAYER, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.OWNER, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.PARENT_SUITE, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.SUB_SUITE, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.SUITE, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.SEVERITY, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.STORY, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: LabelName.TAG, value: "foo" });
expect(tests[0].labels).toContainEqual({ name: "test", value: "testValue" });
expect(tests[0].labels).toContainEqual({ name: "test2", value: "testValue2" });
});
49 changes: 49 additions & 0 deletions packages/allure-jest/test/spec/runtime/legacy/links.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect, it } from "@jest/globals";
import { LinkType } from "allure-js-commons";
import { runJestInlineTest } from "../../../utils";

it("sets links", async () => {
const { tests } = await runJestInlineTest(`
it("link", async () => {
await allure.link("https://allurereport.org");
await allure.issue("1");
await allure.issue("https://example.org/issues/2");
await allure.tms("1");
await allure.tms("https://example.org/tasks/2");
await allure.links(...[{ url:"https://allurereport.org/1" }, { url:"https://allurereport.org/2" }]);
})
`);

expect(tests).toHaveLength(1);
expect(tests).toEqual([
expect.objectContaining({
links: [
{
url: "https://allurereport.org",
},
{
url: "https://example.org/issues/1",
type: LinkType.ISSUE,
},
{
url: "https://example.org/issues/2",
type: LinkType.ISSUE,
},
{
url: "https://example.org/tasks/1",
type: LinkType.TMS,
},
{
url: "https://example.org/tasks/2",
type: LinkType.TMS,
},
{
url: "https://allurereport.org/1",
},
{
url: "https://allurereport.org/2",
},
],
}),
]);
});
35 changes: 35 additions & 0 deletions packages/allure-jest/test/spec/runtime/legacy/parameters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect, it } from "@jest/globals";
import { runJestInlineTest } from "../../../utils";

it("sets parameters", async () => {
const { tests } = await runJestInlineTest(`
it("parameter", async () => {
await allure.parameter("param1", "paramValue1");
await allure.parameter("param2", "paramValue2", {excluded:true});
await allure.parameter("param3", "paramValue3", {mode:"masked", excluded:true});
await allure.parameter("param4", "paramValue4", {mode:"hidden"});
})
`);

expect(tests).toHaveLength(1);
expect(tests).toEqual(
expect.arrayContaining([
expect.objectContaining({
parameters: [
{ name: "param1", value: "paramValue1" },
{ excluded: true, name: "param2", value: "paramValue2" },
{ excluded: true, mode: "masked", name: "param3", value: "paramValue3" },
{ mode: "hidden", name: "param4", value: "paramValue4" },
],
}),
expect.objectContaining({
parameters: [
{ name: "param1", value: "paramValue1" },
{ excluded: true, name: "param2", value: "paramValue2" },
{ excluded: true, mode: "masked", name: "param3", value: "paramValue3" },
{ mode: "hidden", name: "param4", value: "paramValue4" },
],
}),
]),
);
});
Loading

0 comments on commit f4821eb

Please sign in to comment.