Skip to content

Commit

Permalink
Merge 21b622f into be12c69
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn authored Jun 10, 2024
2 parents be12c69 + 21b622f commit 703b4ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
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;
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 };

0 comments on commit 703b4ec

Please sign in to comment.