Skip to content

Commit ecad277

Browse files
epszawdelatrie
andauthored
Fix cypress issues (#1228)
Co-authored-by: Maksim Stepanov <17935127+delatrie@users.noreply.github.com>
1 parent a0cf483 commit ecad277

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const setupScreenshotAttachmentStep = (originalName: string | undefined,
5151

5252
export const startCommandLogStep = (entry: CypressLogEntry) => {
5353
const currentLogEntry = getCurrentLogEntry();
54+
5455
if (typeof currentLogEntry !== "undefined" && shouldStopCurrentLogStep(currentLogEntry.log, entry)) {
5556
stopCommandLogStep(currentLogEntry.log.attributes.id);
5657
}
@@ -65,12 +66,14 @@ export const stopCommandLogStep = (entryId: string) => findAndStopStepWithSubste
6566
const pushLogEntry = (entry: CypressLogEntry) => {
6667
const id = entry.attributes.id;
6768
const stepDescriptor: LogStepDescriptor = { id, type: "log", log: entry };
69+
6870
pushStep(stepDescriptor);
6971

7072
// Some properties of some Command Log entries are undefined at the time the entry is stopped. An example is the
7173
// Yielded property of some queries. We defer converting them to Allure step parameters until the test/hook ends.
7274
setupStepFinalization(stepDescriptor, (data) => {
7375
data.parameters = getCommandLogStepParameters(entry);
76+
7477
if (stepDescriptor.attachmentName) {
7578
// Rename the step to match the attachment name. Once the names are the same, Allure will render the
7679
// attachment in the place of the step.
@@ -146,18 +149,27 @@ const getLogProps = (entry: CypressLogEntry) => {
146149
attributes: { consoleProps },
147150
} = entry;
148151
const isAssertionWithMessage = !!maybeGetAssertionLogMessage(entry);
152+
const { props, name } = consoleProps();
153+
154+
// accessing LocalStorage after the page reload can stick the test runner
155+
// to avoid the issue, we just need to log the command manually
156+
// the problem potentially can happen with other storage related commands, like `clearAllLocalStorage`, `clearAllSessionStorage`, `getAllLocalStorage`, `getAllSessionStorage`, `setLocalStorage`, `setSessionStorage`
157+
// but probably, we don't need to silent them all at this moment
158+
// the context: https://github.com/allure-framework/allure-js/issues/1222
159+
if (["clearLocalStorage"].includes(name)) {
160+
return [] as [string, unknown][];
161+
}
149162

150163
// For assertion logs, we interpolate the 'Message' property, which contains unformatted assertion description,
151164
// directly into the step's name.
152165
// No need to keep the exact same information in the step's parameters.
153-
return Object.entries(consoleProps().props).filter(
154-
([k, v]) => isDefined(v) && !(isAssertionWithMessage && k === "Message"),
155-
);
166+
return Object.entries(props).filter(([k, v]) => isDefined(v) && !(isAssertionWithMessage && k === "Message"));
156167
};
157168

158169
const maybeGetAssertionLogMessage = (entry: CypressLogEntry) => {
159170
if (isAssertLog(entry)) {
160171
const message = entry.attributes.consoleProps().props.Message;
172+
161173
if (message && typeof message === "string") {
162174
return message;
163175
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const onAfterScreenshot = (
1616
...[, { name: originalName, path }]: Parameters<Cypress.ScreenshotDefaultsOptions["onAfterScreenshot"]>
1717
) => {
1818
const name = originalName ?? getFileNameFromPath(path);
19+
1920
reportScreenshot(path, name);
2021
setupScreenshotAttachmentStep(originalName, name);
2122
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DEFAULT_RUNTIME_CONFIG, last, toReversed } from "../utils.js";
33

44
export const getAllureState = () => {
55
let state = Cypress.env("allure") as AllureSpecState;
6+
67
if (!state) {
78
state = {
89
config: DEFAULT_RUNTIME_CONFIG,
@@ -15,8 +16,10 @@ export const getAllureState = () => {
1516
stepsToFinalize: [],
1617
nextApiStepId: 0,
1718
};
19+
1820
Cypress.env("allure", state);
1921
}
22+
2023
return state;
2124
};
2225

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export const uint8ArrayToBase64 = (data: unknown) => {
3030

3131
export const getTestStartData = (test: CypressTest) => ({
3232
...getNamesAndLabels(Cypress.spec, test),
33-
start: test.wallClockStartedAt?.getTime() || Date.now(),
33+
start:
34+
typeof test.wallClockStartedAt === "string"
35+
? Date.parse(test.wallClockStartedAt)
36+
: test.wallClockStartedAt?.getTime?.() || Date.now(),
3437
});
3538

3639
export const getTestStopData = (test: CypressTest) => ({
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect, it } from "vitest";
2+
import { Stage, Status } from "allure-js-commons";
3+
import { runCypressInlineTest } from "../utils.js";
4+
5+
it("shouldn't break the flow when access storage after the page reload", async () => {
6+
const { tests } = await runCypressInlineTest({
7+
"cypress/e2e/sample.cy.js": () => `
8+
it("passed", () => {
9+
cy.visit("https://allurereport.org");
10+
cy.clearLocalStorage();
11+
cy.wait(200);
12+
cy.reload();
13+
cy.wait(200);
14+
});
15+
`,
16+
});
17+
18+
expect(tests).toHaveLength(1);
19+
expect(tests[0].status).toBe(Status.PASSED);
20+
expect(tests[0].stage).toBe(Stage.FINISHED);
21+
});

0 commit comments

Comments
 (0)