playwright-mutation-gate 0.3.2 → 0.3.3

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/README.md CHANGED
@@ -231,6 +231,9 @@ await page.route('**/*', async (route) => {
231
231
  });
232
232
  ```
233
233
 
234
+ The real preamble also skips binary responses (images, fonts, media) instead of
235
+ decoding them as text, and serves any response with no match untouched.
236
+
234
237
  ### A worked example
235
238
 
236
239
  A test that reads fine on review but checks the wrong layer:
package/dist/behavior.js CHANGED
@@ -130,16 +130,24 @@ function parseDestructuredFixtures(params) {
130
130
  }
131
131
  /**
132
132
  * Build the route-interception preamble injected at the top of the test body.
133
- * It fetches each response, replaces every occurrence of the asserted value in
134
- * the headers and body with a sentinel, and records via PMG_HIT_FILE whether
135
- * any occurrence was found. `await import('node:fs')` keeps the preamble
136
- * self-contained, with no top-level import that could clash with the spec.
133
+ * It fetches each text response, replaces every occurrence of the asserted value
134
+ * in the headers and body with a sentinel, and records via PMG_HIT_FILE whether
135
+ * any occurrence was found. Binary resources (images, media, fonts) are passed
136
+ * straight through: decoding them as text to search for a string would corrupt
137
+ * them on the way back out, and the asserted value never lives there. A response
138
+ * with no match is served untouched rather than round-tripped through `text()`.
139
+ * `await import('node:fs')` keeps the preamble self-contained, with no top-level
140
+ * import that could clash with the spec.
137
141
  */
138
142
  function buildPreamble(value) {
139
143
  const needle = JSON.stringify(value);
140
144
  const replace = JSON.stringify(REPLACE);
141
145
  return [
142
146
  ` await page.route('**/*', async (route) => {`,
147
+ ` const resourceType = route.request().resourceType();`,
148
+ ` if (resourceType === 'image' || resourceType === 'media' || resourceType === 'font') {`,
149
+ ` return route.continue();`,
150
+ ` }`,
143
151
  ` const resp = await route.fetch();`,
144
152
  ` const headers = resp.headers();`,
145
153
  ` const NEEDLE = ${needle};`,
@@ -157,7 +165,10 @@ function buildPreamble(value) {
157
165
  ` body = body.split(NEEDLE).join(REPLACE);`,
158
166
  ` hit = true;`,
159
167
  ` }`,
160
- ` if (hit && process.env.PMG_HIT_FILE) {`,
168
+ ` if (!hit) {`,
169
+ ` return route.fulfill({ response: resp });`,
170
+ ` }`,
171
+ ` if (process.env.PMG_HIT_FILE) {`,
161
172
  ` const fs = await import('node:fs');`,
162
173
  ` fs.appendFileSync(process.env.PMG_HIT_FILE, '1');`,
163
174
  ` }`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playwright-mutation-gate",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Mutation testing for Playwright assertions: invert each test's primary assertion and fail the run if the test stays green.",
5
5
  "keywords": [
6
6
  "playwright",