Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cypress issues #1228

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/allure-cypress/src/browser/commandLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const setupScreenshotAttachmentStep = (originalName: string | undefined,

export const startCommandLogStep = (entry: CypressLogEntry) => {
const currentLogEntry = getCurrentLogEntry();

if (typeof currentLogEntry !== "undefined" && shouldStopCurrentLogStep(currentLogEntry.log, entry)) {
stopCommandLogStep(currentLogEntry.log.attributes.id);
}
Expand All @@ -65,12 +66,14 @@ export const stopCommandLogStep = (entryId: string) => findAndStopStepWithSubste
const pushLogEntry = (entry: CypressLogEntry) => {
const id = entry.attributes.id;
const stepDescriptor: LogStepDescriptor = { id, type: "log", log: entry };

pushStep(stepDescriptor);

// Some properties of some Command Log entries are undefined at the time the entry is stopped. An example is the
// Yielded property of some queries. We defer converting them to Allure step parameters until the test/hook ends.
setupStepFinalization(stepDescriptor, (data) => {
data.parameters = getCommandLogStepParameters(entry);

if (stepDescriptor.attachmentName) {
// Rename the step to match the attachment name. Once the names are the same, Allure will render the
// attachment in the place of the step.
Expand Down Expand Up @@ -146,18 +149,31 @@ const getLogProps = (entry: CypressLogEntry) => {
attributes: { consoleProps },
} = entry;
const isAssertionWithMessage = !!maybeGetAssertionLogMessage(entry);
const { props, name } = consoleProps();

// accessing LocalStorage after the page reload can stick the test runner
// to avoid the issue, we just need to log the command manually
// the problem potentially can happen with other storage related commands, like `clearAllLocalStorage`, `clearAllSessionStorage`, `getAllLocalStorage`, `getAllSessionStorage`, `setLocalStorage`, `setSessionStorage`
// but probably, we don't need to silent them all at this moment
// the context: https://github.com/allure-framework/allure-js/issues/1222
if (["clearLocalStorage"].includes(name)) {
return [
epszaw marked this conversation as resolved.
Show resolved Hide resolved
["name", "clearLocalStorage"],
["type", "command"],
["props", {}],
] as [string, unknown][];
}

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

const maybeGetAssertionLogMessage = (entry: CypressLogEntry) => {
if (isAssertLog(entry)) {
const message = entry.attributes.consoleProps().props.Message;

if (message && typeof message === "string") {
return message;
}
Expand Down
1 change: 1 addition & 0 deletions packages/allure-cypress/src/browser/events/cypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const onAfterScreenshot = (
...[, { name: originalName, path }]: Parameters<Cypress.ScreenshotDefaultsOptions["onAfterScreenshot"]>
) => {
const name = originalName ?? getFileNameFromPath(path);

reportScreenshot(path, name);
setupScreenshotAttachmentStep(originalName, name);
};
Expand Down
3 changes: 3 additions & 0 deletions packages/allure-cypress/src/browser/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DEFAULT_RUNTIME_CONFIG, last, toReversed } from "../utils.js";

export const getAllureState = () => {
let state = Cypress.env("allure") as AllureSpecState;

if (!state) {
state = {
config: DEFAULT_RUNTIME_CONFIG,
Expand All @@ -15,8 +16,10 @@ export const getAllureState = () => {
stepsToFinalize: [],
nextApiStepId: 0,
};

Cypress.env("allure", state);
}

return state;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/allure-cypress/src/browser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const uint8ArrayToBase64 = (data: unknown) => {

export const getTestStartData = (test: CypressTest) => ({
...getNamesAndLabels(Cypress.spec, test),
start: test.wallClockStartedAt?.getTime() || Date.now(),
start: test.wallClockStartedAt?.getTime?.() || Date.now(),
epszaw marked this conversation as resolved.
Show resolved Hide resolved
});

export const getTestStopData = (test: CypressTest) => ({
Expand Down
21 changes: 21 additions & 0 deletions packages/allure-cypress/test/spec/security.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect, it } from "vitest";
import { Stage, Status } from "allure-js-commons";
import { runCypressInlineTest } from "../utils.js";

it("shouldn't break the flow when access storage after the page reload", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": () => `
it("passed", () => {
cy.visit("https://allurereport.org");
cy.clearLocalStorage();
cy.wait(200);
cy.reload();
cy.wait(200);
});
`,
});

expect(tests).toHaveLength(1);
expect(tests[0].status).toBe(Status.PASSED);
expect(tests[0].stage).toBe(Stage.FINISHED);
});
Loading