Commit 182cbdb
authored
refactor(playwright): extract browser-side evaluate callbacks (#8060)
Playwright's `page.evaluate(fn)` stringifies the callback with
`fn.toString()` and ships the source into the chromium worker, where it
runs with no access to the Node.js module graph. That is normally
fine, but it breaks the moment the calling file is instrumented by
NYC: the serialized body references NYC's counter globals
(`cov_xxxx.f[0]++`, etc.) which do not exist in the browser, so the
evaluate fails at runtime with `ReferenceError: cov_xxxx is not
defined`. The symptom is silent because Playwright surfaces the browser
error asynchronously — tests just start flapping.
Three coordinated pieces close the foot-gun for the whole codebase:
1. `packages/datadog-instrumentations/src/playwright-browser-scripts.js`
is a new file that holds the two inline callbacks (`detectRum` and
`stopRumSession`) that used to live in `playwright.js`. The file is
required by the instrumentation and the two function values are
passed through to `page.evaluate(...)` instead of anonymous arrows.
2. `nyc.config.js` adds `**/*-browser-scripts.js` to the exclusion
list, so NYC never instruments these files. Renaming the file
pattern requires updating both this glob and the eslint rule
below — the in-source comment calls that out.
3. `eslint.config.mjs` adds a targeted `no-restricted-syntax` rule that
errors when any `.evaluate(<inline function>)` call appears anywhere
in the repo, pointing the author at the `*-browser-scripts.js`
convention. Without the lint rule this class of bug would creep
right back in the next time someone inlines a callback for
convenience.
Behavioral parity with the old code is intentional — both helpers
return the exact same shapes they did when they were inline. Only the
call sites change.1 parent 734ceff commit 182cbdb
4 files changed
Lines changed: 44 additions & 17 deletions
File tree
- packages/datadog-instrumentations/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
463 | 463 | | |
464 | 464 | | |
465 | 465 | | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
466 | 477 | | |
467 | 478 | | |
468 | 479 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
Lines changed: 27 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
55 | 58 | | |
56 | 59 | | |
57 | 60 | | |
| |||
1117 | 1120 | | |
1118 | 1121 | | |
1119 | 1122 | | |
1120 | | - | |
1121 | | - | |
1122 | | - | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | | - | |
| 1123 | + | |
1130 | 1124 | | |
1131 | 1125 | | |
1132 | 1126 | | |
| |||
1209 | 1203 | | |
1210 | 1204 | | |
1211 | 1205 | | |
1212 | | - | |
1213 | | - | |
1214 | | - | |
1215 | | - | |
1216 | | - | |
1217 | | - | |
1218 | | - | |
| 1206 | + | |
1219 | 1207 | | |
1220 | 1208 | | |
1221 | 1209 | | |
| |||
0 commit comments