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

Remove accidental mutation #520

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/dull-mice-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@replayio/cypress": patch
---

Fixed an edge case that caused test steps to appear in the wrong order for flaky tests.
25 changes: 14 additions & 11 deletions packages/cypress/src/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { ReporterError, TestMetadataV2 } from "@replayio/test-utils";
import Debug from "debug";
import { AFTER_EACH_HOOK } from "./constants";
import type { StepEvent } from "./support";
import { Errors, assertCurrentTest, assertMatchingStep, isStepAssertionError } from "./error";
import type { StepEvent } from "./support";

type Test = TestMetadataV2.Test;
type UserActionEvent = TestMetadataV2.UserActionEvent;
Expand Down Expand Up @@ -259,20 +259,20 @@ function groupStepsByTest(tests: Test[], steps: StepEvent[]): Test[] {
});
});

tests.forEach(t => {
tests.forEach(test => {
// If a test fails in the beforeAll hook phase, Cypress will mark the first
// test as failed and the rest as unknown. For consistency, try to detect this
// first case and set it to unknown as well.
if (t.result === "failed" && t.events.main.length === 0) {
if (!steps.some(s => s.event === "test:start" && isTestForStep(t, s))) {
t.result = "unknown";
if (test.result === "failed" && test.events.main.length === 0) {
if (!steps.some(s => s.event === "test:start" && isTestForStep(test, s))) {
test.result = "unknown";
}
}

// Cypress doesn't always bubble up step errors to the test so if a test
// failed and it is missing an error, we find the last error and set that on
// the test
if (t.result === "failed" && t.error == null) {
if (test.result === "failed" && test.error == null) {
const phases: (keyof Test["events"])[] = [
"afterAll",
"afterEach",
Expand All @@ -281,10 +281,13 @@ function groupStepsByTest(tests: Test[], steps: StepEvent[]): Test[] {
"beforeAll",
];
for (const phase of phases) {
const stepWithError = t.events[phase].reverse().find(t => t.data.error);
if (stepWithError) {
t.error = stepWithError.data.error;
break;
Comment on lines -284 to -287
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.prototype.reverse mutates the events array in place. This is not what we want.

Furthermore it's kind of wasteful to call this method anyway. We could use Array.prototype.findLast except I'm not sure what our minimum Node target is so...

const events = test.events[phase];
for (let index = events.length - 1; index >= 0; index--) {
const event = events[index];
if (event.data.error) {
test.error = event.data.error;
break;
}
}
}
}
Expand All @@ -293,4 +296,4 @@ function groupStepsByTest(tests: Test[], steps: StepEvent[]): Test[] {
return tests;
}

export { groupStepsByTest, getTestsFromResults, sortSteps };
export { getTestsFromResults, groupStepsByTest, sortSteps };