req-race 1.0.0 → 1.1.1
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/README.md +107 -107
- package/bin/req-race.js +143 -103
- 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 +127 -53
- 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
package/src/playwright.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Playwright helpers for req-race:
|
|
3
|
-
* - assertNoStaleRace: reverses response order of Request #1 behind Request #2
|
|
4
|
-
* - assertNoDoubleSubmit: rapid double-click mutation guard assertion
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Assert that rapid overlapping requests do not allow a delayed earlier response
|
|
9
|
-
* to overwrite the newer UI state.
|
|
10
|
-
* @param {import('playwright').Page} page
|
|
11
|
-
* @param {object} options
|
|
12
|
-
*/
|
|
13
|
-
export async function assertNoStaleRace(page, options) {
|
|
14
|
-
const { url, trigger, selector, expectedText, delayMs = 800 } = options;
|
|
15
|
-
|
|
16
|
-
let count = 0;
|
|
17
|
-
const handler = async (route) => {
|
|
18
|
-
count++;
|
|
19
|
-
const current = count;
|
|
20
|
-
const response = await route.fetch();
|
|
21
|
-
const body = await response.body();
|
|
22
|
-
|
|
23
|
-
if (current === 1) {
|
|
24
|
-
await new Promise((r) => setTimeout(r, delayMs));
|
|
25
|
-
}
|
|
26
|
-
await route.fulfill({ response, body });
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
await page.route(url, handler);
|
|
30
|
-
try {
|
|
31
|
-
await trigger();
|
|
32
|
-
await page.waitForTimeout(delayMs + 150);
|
|
33
|
-
const actual = await page.locator(selector).textContent();
|
|
34
|
-
if (!actual || !actual.includes(expectedText)) {
|
|
35
|
-
throw new Error(
|
|
36
|
-
`Stale fetch race detected! Expected "${selector}" to include "${expectedText}", but got "${actual}".`
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
} finally {
|
|
40
|
-
await page.unroute(url, handler);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function globToRegex(pattern) {
|
|
45
|
-
if (pattern instanceof RegExp) return pattern;
|
|
46
|
-
const escaped = String(pattern)
|
|
47
|
-
.replace(/[.+?^${}()|[\]\\]/g, "\\$&")
|
|
48
|
-
.replace(/\*/g, ".*");
|
|
49
|
-
return new RegExp(escaped);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Rapidly double-click a button and assert that at most `maxRequests` mutation
|
|
54
|
-
* requests are fired.
|
|
55
|
-
* @param {import('playwright').Page} page
|
|
56
|
-
* @param {object} options
|
|
57
|
-
*/
|
|
58
|
-
export async function assertNoDoubleSubmit(page, options) {
|
|
59
|
-
const { button, url, maxRequests = 1, windowMs = 600 } = options;
|
|
60
|
-
const urlRegex = globToRegex(url);
|
|
61
|
-
|
|
62
|
-
let mutationCount = 0;
|
|
63
|
-
const onRequest = (req) => {
|
|
64
|
-
const method = req.method().toUpperCase();
|
|
65
|
-
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
|
|
66
|
-
if (urlRegex.test(req.url())) mutationCount++;
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
page.on("request", onRequest);
|
|
71
|
-
try {
|
|
72
|
-
const locator = page.locator(button).first();
|
|
73
|
-
await locator.click();
|
|
74
|
-
await locator.click({ force: true }).catch(() => {});
|
|
75
|
-
await page.waitForTimeout(windowMs);
|
|
76
|
-
|
|
77
|
-
if (mutationCount > maxRequests) {
|
|
78
|
-
throw new Error(
|
|
79
|
-
`Double-submit bug detected! Clicking "${button}" twice fired ${mutationCount} mutation requests (max allowed: ${maxRequests}).`
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
return { mutationCount };
|
|
83
|
-
} finally {
|
|
84
|
-
page.off("request", onRequest);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Abort or stall matching requests and assert the UI does not remain stuck
|
|
90
|
-
* in a loading state after `timeoutMs`.
|
|
91
|
-
* @param {import('playwright').Page} page
|
|
92
|
-
* @param {object} options
|
|
93
|
-
*/
|
|
94
|
-
export async function assertNoLoadingHang(page, options) {
|
|
95
|
-
const { url, trigger, loadingSelector, timeoutMs = 1500 } = options;
|
|
96
|
-
const handler = async (route) => {
|
|
97
|
-
await route.abort("timedout");
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
await page.route(url, handler);
|
|
101
|
-
try {
|
|
102
|
-
await trigger();
|
|
103
|
-
await page.waitForTimeout(timeoutMs);
|
|
104
|
-
const stillVisible = await page.locator(loadingSelector).first().isVisible().catch(() => false);
|
|
105
|
-
if (stillVisible) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
`Infinite loading hang detected! "${loadingSelector}" is still visible ${timeoutMs}ms after request failure.`
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
} finally {
|
|
111
|
-
await page.unroute(url, handler);
|
|
112
|
-
}
|
|
113
|
-
}
|