Skip to content

Commit df6a0e1

Browse files
committed
Ensure reload-behavior is actually a reload
This hopefully ensures that the state handling doesn't cause unintentional and hard-to-debug beavior in other cases.
1 parent 2dc1d8e commit df6a0e1

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

lib/plugin-event-handlers.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ interface StateBeforeRun {
8686
interface StateBeforeSpec {
8787
state: "before-spec";
8888
pretty: PrettyState;
89+
spec: Cypress.Spec;
8990
messages: {
9091
accumulation: messages.Envelope[];
9192
};
@@ -94,6 +95,7 @@ interface StateBeforeSpec {
9495
interface StateReceivedSpecEnvelopes {
9596
state: "received-envelopes";
9697
pretty: PrettyState;
98+
spec: Cypress.Spec;
9799
messages: {
98100
accumulation: messages.Envelope[];
99101
current: messages.Envelope[];
@@ -103,6 +105,7 @@ interface StateReceivedSpecEnvelopes {
103105
interface StateTestStarted {
104106
state: "test-started";
105107
pretty: PrettyState;
108+
spec: Cypress.Spec;
106109
messages: {
107110
accumulation: messages.Envelope[];
108111
current: messages.Envelope[];
@@ -113,6 +116,7 @@ interface StateTestStarted {
113116
interface StateStepStarted {
114117
state: "step-started";
115118
pretty: PrettyState;
119+
spec: Cypress.Spec;
116120
messages: {
117121
accumulation: messages.Envelope[];
118122
current: messages.Envelope[];
@@ -124,6 +128,7 @@ interface StateStepStarted {
124128
interface StateStepFinished {
125129
state: "step-finished";
126130
pretty: PrettyState;
131+
spec: Cypress.Spec;
127132
messages: {
128133
accumulation: messages.Envelope[];
129134
current: messages.Envelope[];
@@ -134,6 +139,7 @@ interface StateStepFinished {
134139
interface StateTestFinished {
135140
state: "test-finished";
136141
pretty: PrettyState;
142+
spec: Cypress.Spec;
137143
messages: {
138144
accumulation: messages.Envelope[];
139145
current: messages.Envelope[];
@@ -158,6 +164,7 @@ interface StateAfterRun {
158164
interface StateHasReloaded {
159165
state: "has-reloaded";
160166
pretty: PrettyState;
167+
spec: Cypress.Spec;
161168
messages: {
162169
accumulation: messages.Envelope[];
163170
current: messages.Envelope[];
@@ -167,6 +174,7 @@ interface StateHasReloaded {
167174
interface StateHasReloadedAndReceivedSpecEnvelopes {
168175
state: "has-reloaded-received-envelopes";
169176
pretty: PrettyState;
177+
spec: Cypress.Spec;
170178
specEnvelopes: messages.Envelope[];
171179
messages: {
172180
accumulation: messages.Envelope[];
@@ -439,23 +447,33 @@ export async function beforeSpecHandler(
439447
case "after-spec":
440448
state = {
441449
state: "before-spec",
450+
spec,
442451
pretty: state.pretty,
443452
messages: state.messages,
444453
};
445-
break;
446-
// This will be the case for reloads occuring in a before(), in which case we do nothing,
447-
// because "received-envelopes" would anyway be the next natural state.
448-
case "before-spec":
449-
break;
454+
return;
455+
}
456+
457+
// This will be the case for reloads occuring in a before(), in which case we do nothing,
458+
// because "received-envelopes" would anyway be the next natural state.
459+
if (state.state === "before-spec") {
460+
return;
461+
}
462+
463+
switch (state.state) {
450464
case "received-envelopes": // This will be the case for reloading occuring in a beforeEach().
451465
case "step-started": // This will be the case for reloading occuring in a step.
452466
case "test-finished": // This will be the case for reloading occuring in any after-ish hook (and possibly beforeEach).
453-
state = {
454-
state: "has-reloaded",
455-
pretty: state.pretty,
456-
messages: state.messages,
457-
};
458-
break;
467+
if (state.spec.relative === spec.relative) {
468+
state = {
469+
state: "has-reloaded",
470+
spec: spec,
471+
pretty: state.pretty,
472+
messages: state.messages,
473+
};
474+
return;
475+
}
476+
// eslint-disable-next-line no-fallthrough
459477
default:
460478
throw createStateError("beforeSpecHandler", state.state);
461479
}
@@ -589,6 +607,7 @@ export async function specEnvelopesHandler(
589607
case "has-reloaded":
590608
state = {
591609
state: "has-reloaded-received-envelopes",
610+
spec: state.spec,
592611
specEnvelopes: data.messages,
593612
pretty: state.pretty,
594613
messages: state.messages,
@@ -607,6 +626,7 @@ export async function specEnvelopesHandler(
607626

608627
state = {
609628
state: "received-envelopes",
629+
spec: state.spec,
610630
pretty: state.pretty,
611631
messages: {
612632
accumulation: state.messages.accumulation,
@@ -691,6 +711,7 @@ export async function testCaseStartedHandler(
691711

692712
state = {
693713
state: "test-started",
714+
spec: state.spec,
694715
pretty: state.pretty,
695716
messages: {
696717
accumulation: state.messages.accumulation,
@@ -727,6 +748,7 @@ export function testStepStartedHandler(
727748

728749
state = {
729750
state: "step-started",
751+
spec: state.spec,
730752
pretty: state.pretty,
731753
messages: {
732754
accumulation: state.messages.accumulation,
@@ -864,6 +886,7 @@ export async function testStepFinishedHandler(
864886

865887
state = {
866888
state: "step-finished",
889+
spec: state.spec,
867890
pretty: state.pretty,
868891
messages: {
869892
accumulation: state.messages.accumulation,
@@ -897,6 +920,7 @@ export function testCaseFinishedHandler(
897920

898921
state = {
899922
state: "test-finished",
923+
spec: state.spec,
900924
pretty: state.pretty,
901925
messages: {
902926
accumulation: state.messages.accumulation,

0 commit comments

Comments
 (0)