req-race 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/bin/req-race.js +84 -44
- package/config.schema.json +16 -0
- package/dist/browser/req-race.global.js +1 -0
- package/dist/cjs/browser.cjs +843 -0
- package/dist/cjs/cypress.cjs +618 -0
- package/dist/cjs/index.cjs +1064 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/playwright.cjs +257 -0
- package/dist/cjs/puppeteer.cjs +257 -0
- package/dist/esm/browser.js +813 -0
- package/dist/esm/cypress.js +591 -0
- package/dist/esm/index.js +1002 -0
- package/dist/esm/playwright.js +221 -0
- package/dist/esm/puppeteer.js +221 -0
- package/dist/types/browser.d.cts +9 -0
- package/dist/types/browser.d.ts +9 -0
- package/dist/types/chaos.d.cts +25 -0
- package/dist/types/chaos.d.ts +25 -0
- package/dist/types/config.d.cts +3 -0
- package/dist/types/config.d.ts +3 -0
- package/dist/types/cypress.d.cts +1 -0
- package/dist/types/cypress.d.ts +1 -0
- package/dist/types/index.d.cts +7 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/playwright.d.cts +82 -0
- package/dist/types/playwright.d.ts +82 -0
- package/dist/types/prng.d.cts +1 -0
- package/dist/types/prng.d.ts +1 -0
- package/dist/types/puppeteer.d.cts +2 -0
- package/dist/types/puppeteer.d.ts +2 -0
- package/dist/types/reporters.d.cts +33 -0
- package/dist/types/reporters.d.ts +33 -0
- package/dist/types/safe-fetch.d.cts +24 -0
- package/dist/types/safe-fetch.d.ts +24 -0
- package/dist/types/types.d.cts +215 -0
- package/dist/types/types.d.ts +215 -0
- package/package.json +87 -13
- package/src/index.d.ts +0 -56
- package/src/index.js +0 -373
- package/src/playwright.d.ts +0 -30
- package/src/playwright.js +0 -113
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/playwright.ts
|
|
21
|
+
var playwright_exports = {};
|
|
22
|
+
__export(playwright_exports, {
|
|
23
|
+
assertHandlesOffline: () => assertHandlesOffline,
|
|
24
|
+
assertHandlesServerError: () => assertHandlesServerError,
|
|
25
|
+
assertNoDoubleSubmit: () => assertNoDoubleSubmit,
|
|
26
|
+
assertNoLoadingHang: () => assertNoLoadingHang,
|
|
27
|
+
assertNoOutOfOrderSequence: () => assertNoOutOfOrderSequence,
|
|
28
|
+
assertNoRetryStorm: () => assertNoRetryStorm,
|
|
29
|
+
assertNoStaleAfterNavigation: () => assertNoStaleAfterNavigation,
|
|
30
|
+
assertNoStaleRace: () => assertNoStaleRace,
|
|
31
|
+
assertNoUnhandledRejections: () => assertNoUnhandledRejections,
|
|
32
|
+
withChaos: () => withChaos
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(playwright_exports);
|
|
35
|
+
|
|
36
|
+
// src/types.ts
|
|
37
|
+
var RaceAssertionError = class extends Error {
|
|
38
|
+
code = "ERR_RACE_ASSERTION";
|
|
39
|
+
constructor(message, options) {
|
|
40
|
+
super(message, options);
|
|
41
|
+
this.name = "RaceAssertionError";
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/playwright.ts
|
|
46
|
+
function globToRegex(pattern) {
|
|
47
|
+
if (pattern instanceof RegExp) return pattern;
|
|
48
|
+
const escaped = String(pattern).replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
|
49
|
+
return new RegExp(escaped);
|
|
50
|
+
}
|
|
51
|
+
async function assertNoStaleRace(page, options) {
|
|
52
|
+
const { url, trigger, selector, expectedText, delayMs = 800 } = options;
|
|
53
|
+
let count = 0;
|
|
54
|
+
const handler = async (routeArg) => {
|
|
55
|
+
const route = routeArg;
|
|
56
|
+
count++;
|
|
57
|
+
const current = count;
|
|
58
|
+
const response = await route.fetch();
|
|
59
|
+
const body = await response.body();
|
|
60
|
+
if (current === 1) {
|
|
61
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
62
|
+
}
|
|
63
|
+
await route.fulfill({ response, body });
|
|
64
|
+
};
|
|
65
|
+
await page.route(url, handler);
|
|
66
|
+
try {
|
|
67
|
+
await trigger();
|
|
68
|
+
await page.waitForTimeout(delayMs + 150);
|
|
69
|
+
const actual = await page.locator(selector).first().textContent();
|
|
70
|
+
if (!actual || !actual.includes(expectedText)) {
|
|
71
|
+
throw new RaceAssertionError(
|
|
72
|
+
`Stale fetch race detected! Expected "${selector}" to include "${expectedText}", got "${actual ?? ""}".`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
} finally {
|
|
76
|
+
await page.unroute(url, handler);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function assertNoDoubleSubmit(page, options) {
|
|
80
|
+
const { button, url, maxRequests = 1, windowMs = 600 } = options;
|
|
81
|
+
const urlRegex = globToRegex(url);
|
|
82
|
+
let mutationCount = 0;
|
|
83
|
+
const onRequest = (reqArg) => {
|
|
84
|
+
const req = reqArg;
|
|
85
|
+
const method = req.method().toUpperCase();
|
|
86
|
+
if (["POST", "PUT", "PATCH", "DELETE"].includes(method) && urlRegex.test(req.url())) {
|
|
87
|
+
mutationCount++;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
page.on("request", onRequest);
|
|
91
|
+
try {
|
|
92
|
+
const locator = page.locator(button).first();
|
|
93
|
+
await locator.click();
|
|
94
|
+
await locator.click({ force: true }).catch(() => {
|
|
95
|
+
});
|
|
96
|
+
await page.waitForTimeout(windowMs);
|
|
97
|
+
if (mutationCount > maxRequests) {
|
|
98
|
+
throw new RaceAssertionError(
|
|
99
|
+
`Double-submit bug detected! Clicking "${button}" twice fired ${mutationCount} mutations (max: ${maxRequests}).`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return { mutationCount };
|
|
103
|
+
} finally {
|
|
104
|
+
page.off("request", onRequest);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function assertNoLoadingHang(page, options) {
|
|
108
|
+
const { url, trigger, loadingSelector, timeoutMs = 1500 } = options;
|
|
109
|
+
const handler = async (routeArg) => {
|
|
110
|
+
const route = routeArg;
|
|
111
|
+
await route.abort("timedout");
|
|
112
|
+
};
|
|
113
|
+
await page.route(url, handler);
|
|
114
|
+
try {
|
|
115
|
+
await trigger();
|
|
116
|
+
await page.waitForTimeout(timeoutMs);
|
|
117
|
+
const stillVisible = await page.locator(loadingSelector).first().isVisible().catch(() => false);
|
|
118
|
+
if (stillVisible) {
|
|
119
|
+
throw new RaceAssertionError(
|
|
120
|
+
`Infinite loading hang detected! "${loadingSelector}" still visible after ${timeoutMs}ms.`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
} finally {
|
|
124
|
+
await page.unroute(url, handler);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async function assertHandlesServerError(page, options) {
|
|
128
|
+
const { url, trigger, errorSelector, status = 503, expectNoSuccessSelector } = options;
|
|
129
|
+
const handler = async (routeArg) => {
|
|
130
|
+
const route = routeArg;
|
|
131
|
+
await route.fulfill({ status, body: JSON.stringify({ error: "Server Error" }) });
|
|
132
|
+
};
|
|
133
|
+
await page.route(url, handler);
|
|
134
|
+
try {
|
|
135
|
+
await trigger();
|
|
136
|
+
await page.waitForTimeout(200);
|
|
137
|
+
const hasErr = await page.locator(errorSelector).first().isVisible().catch(() => false);
|
|
138
|
+
if (!hasErr) {
|
|
139
|
+
throw new RaceAssertionError(`Expected error UI "${errorSelector}" on HTTP ${status}.`);
|
|
140
|
+
}
|
|
141
|
+
if (expectNoSuccessSelector) {
|
|
142
|
+
const hasSuccess = await page.locator(expectNoSuccessSelector).first().isVisible().catch(() => false);
|
|
143
|
+
if (hasSuccess) {
|
|
144
|
+
throw new RaceAssertionError(`Success UI "${expectNoSuccessSelector}" appeared despite HTTP ${status}.`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
} finally {
|
|
148
|
+
await page.unroute(url, handler);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async function assertHandlesOffline(page, options) {
|
|
152
|
+
const handler = async (routeArg) => {
|
|
153
|
+
const route = routeArg;
|
|
154
|
+
await route.abort("internetdisconnected");
|
|
155
|
+
};
|
|
156
|
+
await page.route(options.url, handler);
|
|
157
|
+
try {
|
|
158
|
+
await options.trigger();
|
|
159
|
+
await page.waitForTimeout(200);
|
|
160
|
+
const visible = await page.locator(options.errorSelector).first().isVisible().catch(() => false);
|
|
161
|
+
if (!visible) {
|
|
162
|
+
throw new RaceAssertionError(`Expected offline error UI "${options.errorSelector}".`);
|
|
163
|
+
}
|
|
164
|
+
} finally {
|
|
165
|
+
await page.unroute(options.url, handler);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async function assertNoRetryStorm(page, options) {
|
|
169
|
+
let count = 0;
|
|
170
|
+
const handler = async (routeArg) => {
|
|
171
|
+
count++;
|
|
172
|
+
const route = routeArg;
|
|
173
|
+
await route.fulfill({ status: options.status ?? 503, body: "" });
|
|
174
|
+
};
|
|
175
|
+
await page.route(options.url, handler);
|
|
176
|
+
try {
|
|
177
|
+
await options.trigger();
|
|
178
|
+
await page.waitForTimeout(options.windowMs ?? 1e3);
|
|
179
|
+
if (count > options.maxRequests) {
|
|
180
|
+
throw new RaceAssertionError(`Retry storm detected: ${count} requests exceeded max ${options.maxRequests}.`);
|
|
181
|
+
}
|
|
182
|
+
return { count };
|
|
183
|
+
} finally {
|
|
184
|
+
await page.unroute(options.url, handler);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async function assertNoStaleAfterNavigation(page, options) {
|
|
188
|
+
const delayMs = options.delayMs ?? 500;
|
|
189
|
+
const handler = async (routeArg) => {
|
|
190
|
+
const route = routeArg;
|
|
191
|
+
const res = await route.fetch();
|
|
192
|
+
const body = await res.body();
|
|
193
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
194
|
+
await route.fulfill({ response: res, body });
|
|
195
|
+
};
|
|
196
|
+
await page.route(options.url, handler);
|
|
197
|
+
try {
|
|
198
|
+
await options.trigger();
|
|
199
|
+
await options.navigateTo();
|
|
200
|
+
await page.waitForTimeout(delayMs + 150);
|
|
201
|
+
const leaked = await page.locator(options.forbiddenSelector).first().isVisible().catch(() => false);
|
|
202
|
+
if (leaked) {
|
|
203
|
+
throw new RaceAssertionError(`Stale response updated DOM "${options.forbiddenSelector}" after navigation.`);
|
|
204
|
+
}
|
|
205
|
+
} finally {
|
|
206
|
+
await page.unroute(options.url, handler);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async function assertNoUnhandledRejections(page, options) {
|
|
210
|
+
const errors = [];
|
|
211
|
+
const onErr = (err) => errors.push(String(err));
|
|
212
|
+
page.on("pageerror", onErr);
|
|
213
|
+
try {
|
|
214
|
+
await options.trigger();
|
|
215
|
+
await page.waitForTimeout(options.windowMs ?? 300);
|
|
216
|
+
if (errors.length > 0) {
|
|
217
|
+
throw new RaceAssertionError(`Unhandled pageerror(s): ${errors.join("; ")}`);
|
|
218
|
+
}
|
|
219
|
+
} finally {
|
|
220
|
+
page.off("pageerror", onErr);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
async function assertNoOutOfOrderSequence(page, options) {
|
|
224
|
+
return assertNoStaleRace(page, {
|
|
225
|
+
url: options.url,
|
|
226
|
+
trigger: options.trigger,
|
|
227
|
+
selector: options.selector,
|
|
228
|
+
expectedText: options.expectedFinalText,
|
|
229
|
+
delayMs: options.delayMs ?? 600
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async function withChaos(page, options = {}) {
|
|
233
|
+
const stats = await page.evaluate((opts) => {
|
|
234
|
+
const r = globalThis.ReqRace;
|
|
235
|
+
return r ? r.enable(opts).getStats() : null;
|
|
236
|
+
}, options);
|
|
237
|
+
return {
|
|
238
|
+
stats,
|
|
239
|
+
dispose: async () => {
|
|
240
|
+
await page.evaluate(() => {
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
246
|
+
0 && (module.exports = {
|
|
247
|
+
assertHandlesOffline,
|
|
248
|
+
assertHandlesServerError,
|
|
249
|
+
assertNoDoubleSubmit,
|
|
250
|
+
assertNoLoadingHang,
|
|
251
|
+
assertNoOutOfOrderSequence,
|
|
252
|
+
assertNoRetryStorm,
|
|
253
|
+
assertNoStaleAfterNavigation,
|
|
254
|
+
assertNoStaleRace,
|
|
255
|
+
assertNoUnhandledRejections,
|
|
256
|
+
withChaos
|
|
257
|
+
});
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/puppeteer.ts
|
|
21
|
+
var puppeteer_exports = {};
|
|
22
|
+
__export(puppeteer_exports, {
|
|
23
|
+
assertHandlesOffline: () => assertHandlesOffline,
|
|
24
|
+
assertHandlesServerError: () => assertHandlesServerError,
|
|
25
|
+
assertNoDoubleSubmit: () => assertNoDoubleSubmit,
|
|
26
|
+
assertNoLoadingHang: () => assertNoLoadingHang,
|
|
27
|
+
assertNoOutOfOrderSequence: () => assertNoOutOfOrderSequence,
|
|
28
|
+
assertNoRetryStorm: () => assertNoRetryStorm,
|
|
29
|
+
assertNoStaleAfterNavigation: () => assertNoStaleAfterNavigation,
|
|
30
|
+
assertNoStaleRace: () => assertNoStaleRace,
|
|
31
|
+
assertNoUnhandledRejections: () => assertNoUnhandledRejections,
|
|
32
|
+
withChaos: () => withChaos
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(puppeteer_exports);
|
|
35
|
+
|
|
36
|
+
// src/types.ts
|
|
37
|
+
var RaceAssertionError = class extends Error {
|
|
38
|
+
code = "ERR_RACE_ASSERTION";
|
|
39
|
+
constructor(message, options) {
|
|
40
|
+
super(message, options);
|
|
41
|
+
this.name = "RaceAssertionError";
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/playwright.ts
|
|
46
|
+
function globToRegex(pattern) {
|
|
47
|
+
if (pattern instanceof RegExp) return pattern;
|
|
48
|
+
const escaped = String(pattern).replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
|
49
|
+
return new RegExp(escaped);
|
|
50
|
+
}
|
|
51
|
+
async function assertNoStaleRace(page, options) {
|
|
52
|
+
const { url, trigger, selector, expectedText, delayMs = 800 } = options;
|
|
53
|
+
let count = 0;
|
|
54
|
+
const handler = async (routeArg) => {
|
|
55
|
+
const route = routeArg;
|
|
56
|
+
count++;
|
|
57
|
+
const current = count;
|
|
58
|
+
const response = await route.fetch();
|
|
59
|
+
const body = await response.body();
|
|
60
|
+
if (current === 1) {
|
|
61
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
62
|
+
}
|
|
63
|
+
await route.fulfill({ response, body });
|
|
64
|
+
};
|
|
65
|
+
await page.route(url, handler);
|
|
66
|
+
try {
|
|
67
|
+
await trigger();
|
|
68
|
+
await page.waitForTimeout(delayMs + 150);
|
|
69
|
+
const actual = await page.locator(selector).first().textContent();
|
|
70
|
+
if (!actual || !actual.includes(expectedText)) {
|
|
71
|
+
throw new RaceAssertionError(
|
|
72
|
+
`Stale fetch race detected! Expected "${selector}" to include "${expectedText}", got "${actual ?? ""}".`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
} finally {
|
|
76
|
+
await page.unroute(url, handler);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function assertNoDoubleSubmit(page, options) {
|
|
80
|
+
const { button, url, maxRequests = 1, windowMs = 600 } = options;
|
|
81
|
+
const urlRegex = globToRegex(url);
|
|
82
|
+
let mutationCount = 0;
|
|
83
|
+
const onRequest = (reqArg) => {
|
|
84
|
+
const req = reqArg;
|
|
85
|
+
const method = req.method().toUpperCase();
|
|
86
|
+
if (["POST", "PUT", "PATCH", "DELETE"].includes(method) && urlRegex.test(req.url())) {
|
|
87
|
+
mutationCount++;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
page.on("request", onRequest);
|
|
91
|
+
try {
|
|
92
|
+
const locator = page.locator(button).first();
|
|
93
|
+
await locator.click();
|
|
94
|
+
await locator.click({ force: true }).catch(() => {
|
|
95
|
+
});
|
|
96
|
+
await page.waitForTimeout(windowMs);
|
|
97
|
+
if (mutationCount > maxRequests) {
|
|
98
|
+
throw new RaceAssertionError(
|
|
99
|
+
`Double-submit bug detected! Clicking "${button}" twice fired ${mutationCount} mutations (max: ${maxRequests}).`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return { mutationCount };
|
|
103
|
+
} finally {
|
|
104
|
+
page.off("request", onRequest);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function assertNoLoadingHang(page, options) {
|
|
108
|
+
const { url, trigger, loadingSelector, timeoutMs = 1500 } = options;
|
|
109
|
+
const handler = async (routeArg) => {
|
|
110
|
+
const route = routeArg;
|
|
111
|
+
await route.abort("timedout");
|
|
112
|
+
};
|
|
113
|
+
await page.route(url, handler);
|
|
114
|
+
try {
|
|
115
|
+
await trigger();
|
|
116
|
+
await page.waitForTimeout(timeoutMs);
|
|
117
|
+
const stillVisible = await page.locator(loadingSelector).first().isVisible().catch(() => false);
|
|
118
|
+
if (stillVisible) {
|
|
119
|
+
throw new RaceAssertionError(
|
|
120
|
+
`Infinite loading hang detected! "${loadingSelector}" still visible after ${timeoutMs}ms.`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
} finally {
|
|
124
|
+
await page.unroute(url, handler);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async function assertHandlesServerError(page, options) {
|
|
128
|
+
const { url, trigger, errorSelector, status = 503, expectNoSuccessSelector } = options;
|
|
129
|
+
const handler = async (routeArg) => {
|
|
130
|
+
const route = routeArg;
|
|
131
|
+
await route.fulfill({ status, body: JSON.stringify({ error: "Server Error" }) });
|
|
132
|
+
};
|
|
133
|
+
await page.route(url, handler);
|
|
134
|
+
try {
|
|
135
|
+
await trigger();
|
|
136
|
+
await page.waitForTimeout(200);
|
|
137
|
+
const hasErr = await page.locator(errorSelector).first().isVisible().catch(() => false);
|
|
138
|
+
if (!hasErr) {
|
|
139
|
+
throw new RaceAssertionError(`Expected error UI "${errorSelector}" on HTTP ${status}.`);
|
|
140
|
+
}
|
|
141
|
+
if (expectNoSuccessSelector) {
|
|
142
|
+
const hasSuccess = await page.locator(expectNoSuccessSelector).first().isVisible().catch(() => false);
|
|
143
|
+
if (hasSuccess) {
|
|
144
|
+
throw new RaceAssertionError(`Success UI "${expectNoSuccessSelector}" appeared despite HTTP ${status}.`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
} finally {
|
|
148
|
+
await page.unroute(url, handler);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async function assertHandlesOffline(page, options) {
|
|
152
|
+
const handler = async (routeArg) => {
|
|
153
|
+
const route = routeArg;
|
|
154
|
+
await route.abort("internetdisconnected");
|
|
155
|
+
};
|
|
156
|
+
await page.route(options.url, handler);
|
|
157
|
+
try {
|
|
158
|
+
await options.trigger();
|
|
159
|
+
await page.waitForTimeout(200);
|
|
160
|
+
const visible = await page.locator(options.errorSelector).first().isVisible().catch(() => false);
|
|
161
|
+
if (!visible) {
|
|
162
|
+
throw new RaceAssertionError(`Expected offline error UI "${options.errorSelector}".`);
|
|
163
|
+
}
|
|
164
|
+
} finally {
|
|
165
|
+
await page.unroute(options.url, handler);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async function assertNoRetryStorm(page, options) {
|
|
169
|
+
let count = 0;
|
|
170
|
+
const handler = async (routeArg) => {
|
|
171
|
+
count++;
|
|
172
|
+
const route = routeArg;
|
|
173
|
+
await route.fulfill({ status: options.status ?? 503, body: "" });
|
|
174
|
+
};
|
|
175
|
+
await page.route(options.url, handler);
|
|
176
|
+
try {
|
|
177
|
+
await options.trigger();
|
|
178
|
+
await page.waitForTimeout(options.windowMs ?? 1e3);
|
|
179
|
+
if (count > options.maxRequests) {
|
|
180
|
+
throw new RaceAssertionError(`Retry storm detected: ${count} requests exceeded max ${options.maxRequests}.`);
|
|
181
|
+
}
|
|
182
|
+
return { count };
|
|
183
|
+
} finally {
|
|
184
|
+
await page.unroute(options.url, handler);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async function assertNoStaleAfterNavigation(page, options) {
|
|
188
|
+
const delayMs = options.delayMs ?? 500;
|
|
189
|
+
const handler = async (routeArg) => {
|
|
190
|
+
const route = routeArg;
|
|
191
|
+
const res = await route.fetch();
|
|
192
|
+
const body = await res.body();
|
|
193
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
194
|
+
await route.fulfill({ response: res, body });
|
|
195
|
+
};
|
|
196
|
+
await page.route(options.url, handler);
|
|
197
|
+
try {
|
|
198
|
+
await options.trigger();
|
|
199
|
+
await options.navigateTo();
|
|
200
|
+
await page.waitForTimeout(delayMs + 150);
|
|
201
|
+
const leaked = await page.locator(options.forbiddenSelector).first().isVisible().catch(() => false);
|
|
202
|
+
if (leaked) {
|
|
203
|
+
throw new RaceAssertionError(`Stale response updated DOM "${options.forbiddenSelector}" after navigation.`);
|
|
204
|
+
}
|
|
205
|
+
} finally {
|
|
206
|
+
await page.unroute(options.url, handler);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async function assertNoUnhandledRejections(page, options) {
|
|
210
|
+
const errors = [];
|
|
211
|
+
const onErr = (err) => errors.push(String(err));
|
|
212
|
+
page.on("pageerror", onErr);
|
|
213
|
+
try {
|
|
214
|
+
await options.trigger();
|
|
215
|
+
await page.waitForTimeout(options.windowMs ?? 300);
|
|
216
|
+
if (errors.length > 0) {
|
|
217
|
+
throw new RaceAssertionError(`Unhandled pageerror(s): ${errors.join("; ")}`);
|
|
218
|
+
}
|
|
219
|
+
} finally {
|
|
220
|
+
page.off("pageerror", onErr);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
async function assertNoOutOfOrderSequence(page, options) {
|
|
224
|
+
return assertNoStaleRace(page, {
|
|
225
|
+
url: options.url,
|
|
226
|
+
trigger: options.trigger,
|
|
227
|
+
selector: options.selector,
|
|
228
|
+
expectedText: options.expectedFinalText,
|
|
229
|
+
delayMs: options.delayMs ?? 600
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async function withChaos(page, options = {}) {
|
|
233
|
+
const stats = await page.evaluate((opts) => {
|
|
234
|
+
const r = globalThis.ReqRace;
|
|
235
|
+
return r ? r.enable(opts).getStats() : null;
|
|
236
|
+
}, options);
|
|
237
|
+
return {
|
|
238
|
+
stats,
|
|
239
|
+
dispose: async () => {
|
|
240
|
+
await page.evaluate(() => {
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
246
|
+
0 && (module.exports = {
|
|
247
|
+
assertHandlesOffline,
|
|
248
|
+
assertHandlesServerError,
|
|
249
|
+
assertNoDoubleSubmit,
|
|
250
|
+
assertNoLoadingHang,
|
|
251
|
+
assertNoOutOfOrderSequence,
|
|
252
|
+
assertNoRetryStorm,
|
|
253
|
+
assertNoStaleAfterNavigation,
|
|
254
|
+
assertNoStaleRace,
|
|
255
|
+
assertNoUnhandledRejections,
|
|
256
|
+
withChaos
|
|
257
|
+
});
|