Skip to content

Commit 332e9a5

Browse files
committed
fix mr issues
1 parent 18f11b2 commit 332e9a5

File tree

45 files changed

+576
-175
lines changed

Some content is hidden

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

45 files changed

+576
-175
lines changed
Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, it } from "vitest";
22
import { runCodeceptJsInlineTest } from "../../../utils.js";
33

4-
it("writes globals payload from runtime API calls", async () => {
4+
it("writes globals payload from scenario body", async () => {
55
const { globals, attachments } = await runCodeceptJsInlineTest({
66
"login.test.js": `
77
const { globalAttachment, globalError } = require("allure-js-commons");
@@ -17,20 +17,66 @@ it("writes globals payload from runtime API calls", async () => {
1717
const globalsEntries = Object.entries(globals ?? {});
1818
expect(globalsEntries).toHaveLength(1);
1919

20-
const [globalsFileName, globalInfo] = globalsEntries[0];
21-
expect(globalsFileName).toMatch(/.+-globals\.json/);
22-
expect(globalInfo.errors).toEqual([
23-
{
24-
message: "global setup failed",
25-
trace: "stack",
26-
},
27-
]);
20+
const [, globalInfo] = globalsEntries[0];
21+
expect(globalInfo.errors).toEqual(
22+
expect.arrayContaining([
23+
{
24+
message: "global setup failed",
25+
trace: "stack",
26+
},
27+
]),
28+
);
29+
30+
const scenarioAttachment = globalInfo.attachments.find((a) => a.name === "global-log");
31+
expect(scenarioAttachment?.type).toBe("text/plain");
32+
const encodedScenarioAttachment = attachments[scenarioAttachment!.source] as string;
33+
expect(Buffer.from(encodedScenarioAttachment, "base64").toString("utf-8")).toBe("hello");
34+
});
35+
36+
it("writes globals payload from suite hooks", async () => {
37+
const { globals, attachments } = await runCodeceptJsInlineTest({
38+
"login.test.js": `
39+
const { globalAttachment, globalError } = require("allure-js-commons");
40+
41+
Feature("sample-feature");
42+
BeforeSuite(async () => {
43+
await globalAttachment("before-suite-log", "before", { contentType: "text/plain" });
44+
});
45+
46+
AfterSuite(async () => {
47+
await globalError({ message: "after suite error" });
48+
});
49+
50+
Scenario("sample-scenario", async () => {});
51+
`,
52+
});
53+
54+
const globalsEntries = Object.entries(globals ?? {});
55+
expect(globalsEntries).toHaveLength(1);
56+
57+
const [, globalInfo] = globalsEntries[0];
58+
expect(globalInfo.errors).toEqual(expect.arrayContaining([{ message: "after suite error" }]));
2859
expect(globalInfo.attachments).toHaveLength(1);
2960

30-
const [globalAttachmentRef] = globalInfo.attachments;
31-
expect(globalAttachmentRef.name).toBe("global-log");
32-
expect(globalAttachmentRef.type).toBe("text/plain");
61+
const beforeSuiteAttachment = globalInfo.attachments.find((a) => a.name === "before-suite-log");
62+
expect(beforeSuiteAttachment?.type).toBe("text/plain");
63+
const encodedBeforeSuiteAttachment = attachments[beforeSuiteAttachment!.source] as string;
64+
expect(Buffer.from(encodedBeforeSuiteAttachment, "base64").toString("utf-8")).toBe("before");
65+
});
66+
67+
it("does not collect globals from module scope", async () => {
68+
const { globals, stdout } = await runCodeceptJsInlineTest({
69+
"login.test.js": `
70+
const { globalError } = require("allure-js-commons");
3371
34-
const encodedAttachment = attachments[globalAttachmentRef.source] as string;
35-
expect(Buffer.from(encodedAttachment, "base64").toString("utf-8")).toBe("hello");
72+
void globalError({ message: "module scope error" });
73+
74+
Feature("sample-feature");
75+
Scenario("sample-scenario", async () => {});
76+
`,
77+
});
78+
79+
const globalsEntries = Object.entries(globals ?? {});
80+
expect(globalsEntries).toHaveLength(0);
81+
expect(stdout.join("\n")).toContain("no test runtime is found");
3682
});

packages/allure-cucumberjs/src/reporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,13 @@ export default class AllureCucumberReporter extends Formatter {
574574
return;
575575
}
576576

577-
this.allureRuntime.applyRuntimeMessages(undefined, globalMessages);
577+
this.allureRuntime.applyGlobalRuntimeMessages(globalMessages);
578578
}
579579

580580
private onTestRunFinished() {
581581
this.allureRuntime.writeCategoriesDefinitions();
582582
this.allureRuntime.writeEnvironmentInfo();
583-
this.allureRuntime.writeGlobalInfo();
583+
this.allureRuntime.writeGlobals();
584584
}
585585

586586
private exceptionToError(message?: string, exception?: messages.Exception): Error | undefined {
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
const { Before } = require("@cucumber/cucumber");
1+
const { Given } = require("@cucumber/cucumber");
22
const { globalAttachment, globalError } = require("allure-js-commons");
33

4-
let isSent = false;
5-
6-
Before(async () => {
7-
if (isSent) {
8-
return;
9-
}
10-
11-
isSent = true;
4+
Given("a passed step", async () => {
125
await globalAttachment("global-log", "hello", { contentType: "text/plain" });
136
await globalError({ message: "global setup failed", trace: "stack" });
147
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { Before, After } = require("@cucumber/cucumber");
2+
const { globalAttachment, globalError } = require("allure-js-commons");
3+
4+
void globalError({ message: "module scope error" });
5+
6+
Before(async () => {
7+
await globalAttachment("before-log", "before", { contentType: "text/plain" });
8+
});
9+
10+
After(async () => {
11+
await globalError({ message: "after hook error" });
12+
});

packages/allure-cucumberjs/test/spec/runtime/modern/globals.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect, it } from "vitest";
22
import { runCucumberInlineTest } from "../../../utils.js";
33

44
it("writes globals payload from runtime API calls", async () => {
5-
const { globals, attachments } = await runCucumberInlineTest(["hooks"], ["hooks", "runtime/modern/globals"], {
5+
const { globals, attachments } = await runCucumberInlineTest(["hooks"], ["runtime/modern/globals"], {
66
parallel: false,
77
});
88

@@ -26,3 +26,32 @@ it("writes globals payload from runtime API calls", async () => {
2626
const encodedAttachment = attachments[globalAttachmentRef.source] as string;
2727
expect(Buffer.from(encodedAttachment, "base64").toString("utf-8")).toBe("hello");
2828
});
29+
30+
it("supports globals from hooks", async () => {
31+
const { globals, attachments } = await runCucumberInlineTest(["hooks"], ["hooks", "runtime/modern/globalsContexts"], {
32+
parallel: false,
33+
});
34+
35+
const globalsEntries = Object.entries(globals ?? {});
36+
expect(globalsEntries).toHaveLength(1);
37+
38+
const [, globalInfo] = globalsEntries[0];
39+
expect(globalInfo.errors).toEqual(expect.arrayContaining([{ message: "after hook error" }]));
40+
41+
const beforeAttachmentRef = globalInfo.attachments.find((a) => a.name === "before-log");
42+
expect(beforeAttachmentRef?.type).toBe("text/plain");
43+
const encodedAttachment = attachments[beforeAttachmentRef!.source] as string;
44+
expect(Buffer.from(encodedAttachment, "base64").toString("utf-8")).toBe("before");
45+
});
46+
47+
it("does not collect globals from support module scope", async () => {
48+
const { globals } = await runCucumberInlineTest(["hooks"], ["hooks", "runtime/modern/globalsContexts"], {
49+
parallel: false,
50+
});
51+
52+
const globalsEntries = Object.entries(globals ?? {});
53+
expect(globalsEntries).toHaveLength(1);
54+
55+
const [, globalInfo] = globalsEntries[0];
56+
expect(globalInfo.errors).not.toEqual(expect.arrayContaining([{ message: "module scope error" }]));
57+
});

packages/allure-cypress/src/browser/runtime.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ class AllureCypressTestRuntime implements TestRuntime {
146146
});
147147
}
148148

149+
globalAttachmentFromPath(name: string, path: string, options: Omit<AttachmentOptions, "encoding">) {
150+
return this.#enqueueMessageAsync({
151+
type: "global_attachment_path",
152+
data: {
153+
name,
154+
path,
155+
contentType: options.contentType,
156+
fileExtension: options.fileExtension,
157+
},
158+
});
159+
}
160+
149161
globalError(details: StatusDetails) {
150162
return this.#enqueueMessageAsync({
151163
type: "global_error",

packages/allure-cypress/src/reporter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class AllureCypress {
139139
this.#endAllSpecs();
140140
this.allureRuntime.writeEnvironmentInfo();
141141
this.allureRuntime.writeCategoriesDefinitions();
142-
this.allureRuntime.writeGlobalInfo();
142+
this.allureRuntime.writeGlobals();
143143
};
144144

145145
endSpec = (specAbsolutePath: string, cypressVideoPath?: string) => {
@@ -464,6 +464,10 @@ export class AllureCypress {
464464
if (!rootUuid && !isGlobalRuntimeMessage(message)) {
465465
return;
466466
}
467+
if (!rootUuid) {
468+
this.allureRuntime.applyGlobalRuntimeMessages([message]);
469+
return;
470+
}
467471
this.allureRuntime.applyRuntimeMessages(rootUuid, [message]);
468472
};
469473

packages/allure-jasmine/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export default class AllureJasmineReporter implements jasmine.CustomReporter {
8484
if (!rootUuid && !isGlobalRuntimeMessage(message)) {
8585
return;
8686
}
87+
if (!rootUuid) {
88+
this.allureRuntime.applyGlobalRuntimeMessages([message]);
89+
return;
90+
}
8791
this.allureRuntime.applyRuntimeMessages(rootUuid, [message]);
8892
}
8993

@@ -204,7 +208,7 @@ export default class AllureJasmineReporter implements jasmine.CustomReporter {
204208
jasmineDone(): void {
205209
this.allureRuntime.writeEnvironmentInfo();
206210
this.allureRuntime.writeCategoriesDefinitions();
207-
this.allureRuntime.writeGlobalInfo();
211+
this.allureRuntime.writeGlobals();
208212
// write global container (or any remaining scopes)
209213
this.scopesStack.forEach((scopeUuid) => {
210214
this.allureRuntime.writeScope(scopeUuid);

packages/allure-jasmine/test/spec/runtime/modern/globals.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,41 @@ it("writes globals payload from runtime API calls", async () => {
3737
const encodedAttachment = attachments[globalAttachmentRef.source] as string;
3838
expect(Buffer.from(encodedAttachment, "base64").toString("utf-8")).toBe("hello");
3939
});
40+
41+
it("collects globals from setup-only file without tests", async () => {
42+
const { globals, attachments } = await runJasmineInlineTest({
43+
"spec/test/sample.spec.js": `
44+
const { writeFileSync } = require("node:fs");
45+
const { join } = require("node:path");
46+
const { globalAttachment, globalAttachmentPath, globalError } = require("allure-js-commons");
47+
48+
const filePath = join(process.cwd(), "setup-only.log");
49+
writeFileSync(filePath, "from-path", "utf8");
50+
51+
void globalAttachment("setup-inline-log", "from-inline", { contentType: "text/plain" });
52+
void globalAttachmentPath("setup-file-log", filePath, { contentType: "text/plain" });
53+
void globalError({ message: "setup-only error", trace: "setup stack" });
54+
`,
55+
});
56+
57+
const globalsEntries = Object.entries(globals ?? {});
58+
expect(globalsEntries).toHaveLength(1);
59+
60+
const [, globalInfo] = globalsEntries[0];
61+
expect(globalInfo.errors).toEqual(
62+
expect.arrayContaining([
63+
{
64+
message: "setup-only error",
65+
trace: "setup stack",
66+
},
67+
]),
68+
);
69+
70+
const inlineRef = globalInfo.attachments.find((a) => a.name === "setup-inline-log");
71+
const pathRef = globalInfo.attachments.find((a) => a.name === "setup-file-log");
72+
73+
expect(inlineRef?.type).toBe("text/plain");
74+
expect(pathRef?.type).toBe("text/plain");
75+
expect(Buffer.from(attachments[inlineRef!.source] as string, "base64").toString("utf-8")).toBe("from-inline");
76+
expect(Buffer.from(attachments[pathRef!.source] as string, "base64").toString("utf-8")).toBe("from-path");
77+
});

packages/allure-jest/src/environmentFactory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
7979
if (!executableUuid && !isGlobalRuntimeMessage(message)) {
8080
return;
8181
}
82-
82+
if (!executableUuid) {
83+
this.runtime.applyGlobalRuntimeMessages([message]);
84+
return;
85+
}
8386
this.runtime.applyRuntimeMessages(executableUuid, [message]);
8487
}
8588

@@ -362,7 +365,7 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
362365
#handleRunFinish() {
363366
this.runtime.writeEnvironmentInfo();
364367
this.runtime.writeCategoriesDefinitions();
365-
this.runtime.writeGlobalInfo();
368+
this.runtime.writeGlobals();
366369
}
367370

368371
#currentExecutable() {

0 commit comments

Comments
 (0)