@@ -53,6 +53,28 @@ Cypress.Commands.add('resetInterceptedApiAliases', () =>
53
53
Cypress . env ( 'interceptedAliases' , { } )
54
54
) ;
55
55
56
+ /**
57
+ * Sets the request interception flag in Cypress environment.
58
+ * This flag is used to track whether a request matching an intercept pattern was detected.
59
+ *
60
+ * @param {boolean } value - The value to set for the flag (true if request was intercepted, false otherwise)
61
+ * @example
62
+ * // Mark a request as intercepted
63
+ * setRequestIntercepted(true);
64
+ *
65
+ * // Reset the interception flag
66
+ * setRequestIntercepted(false);
67
+ */
68
+ const setRequestIntercepted = ( value ) =>
69
+ Cypress . env ( 'wasRequestIntercepted' , value ) ;
70
+
71
+ /**
72
+ * Gets the current value of the request interception flag from Cypress environment.
73
+ * This flag indicates whether a request matching an intercept pattern was detected.
74
+ * @returns {boolean } The current value of the request interception flag
75
+ */
76
+ const getRequestIntercepted = ( ) => Cypress . env ( 'wasRequestIntercepted' ) ;
77
+
56
78
/**
57
79
* Custom command to intercept API calls and wait for them to complete.
58
80
* This command will:
@@ -91,19 +113,26 @@ Cypress.Commands.add(
91
113
const aliasObjectKey = `${ method . toLowerCase ( ) } -${ alias } ` ;
92
114
// Check if this request is already registered
93
115
const isAlreadyRegistered = ! ! interceptedAliasesMap [ aliasObjectKey ] ;
94
-
116
+ // Setting wasRequestIntercepted flag to false initially
117
+ setRequestIntercepted ( false ) ;
95
118
// Register the intercept if not already done
96
119
if ( ! isAlreadyRegistered ) {
97
- cy . intercept ( method , urlPattern ) . as ( alias ) ;
120
+ cy . intercept ( method , urlPattern , ( ) => {
121
+ // Setting wasRequestIntercepted flag to true after request is intercepted
122
+ setRequestIntercepted ( true ) ;
123
+ } ) . as ( alias ) ;
98
124
cy . setInterceptedApiAlias ( aliasObjectKey , alias ) ;
99
125
}
100
126
101
127
// Execute the function that triggers the API call
102
128
triggerFn ( ) ;
103
129
104
130
// Wait for the intercepted request to complete
105
- cy . wait ( `@${ alias } ` ) . then ( ( interception ) => {
106
- onApiResponse ( interception ) ;
131
+ cy . then ( ( ) => {
132
+ const isRequestIntercepted = getRequestIntercepted ( ) ;
133
+ if ( isRequestIntercepted ) {
134
+ cy . wait ( `@${ alias } ` ) . then ( onApiResponse ) ;
135
+ }
107
136
} ) ;
108
137
} ) ;
109
138
}
0 commit comments