Repository navigation
♻️ [Core] Make mockable replacements available in production builds - #5160
BeltranBulbarellaDD wants to merge 1 commit into
Conversation
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 1 Pipeline job failed
ℹ️ InfoNo other issues found (see more)🧪 All tests passed 🎯 Code Coverage (details) Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: 0253272 | Docs | View more details | Give us feedback! |
Bundles Sizes Evolution
|
|
@codex review pretty please |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
🛡️ Codex Security Review · Automatically triggeredSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0253272e33
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
| // In production, return the value as-is | ||
| return value | ||
| return mockableReplacements.has(value) ? (mockableReplacements.get(value) as T) : value |
There was a problem hiding this comment.
Prevent primitive keys from aliasing unrelated replacements
issue: When value is a primitive—especially undefined—unrelated call sites share the same map key. This lookup now runs in production, so in an insecure context lacking both Cookie Store and Web Crypto, an integration that replaces the missing globalObject.cookieStore can make mockable(globalObject.crypto?.subtle) in canvasHash.ts return that CookieStore replacement and then fail when it calls .digest(). Existing production call sites pass optional globals, so runtime replacements should be limited to reference-valued originals or keyed by a call-site-specific token.
Useful? React with 👍 / 👎.
| * Exposed so test helpers (e.g. `replaceMockable`) can register and clean up mocks. | ||
| * Exposed so test helpers (e.g. `replaceMockable` from `@datadog/browser-core/test`) can clean up mocks. | ||
| */ | ||
| export const mockableReplacements = new Map<unknown, unknown>() |
There was a problem hiding this comment.
Share replacements across the ESM and CJS entry points
issue: The registry is module-local, but this package deliberately ships separate ESM and CJS implementations. In a mixed Node or Electron application where the SDK reaches mockable through the ESM condition and an integration calls require('@datadog/js-core/util').replaceMockable (or vice versa), each format owns a different Map, so registration silently has no effect. Store the registry in a shared location or otherwise ensure both supported module formats operate on the same state.
AGENTS.md reference: packages/js-core/AGENTS.md:L54-L63
Useful? React with 👍 / 👎.
| * @param value - The original value, as passed to `mockable()` | ||
| * @param replacement - The value `mockable(value)` returns from now on | ||
| */ | ||
| export function replaceMockable<T>(value: T, replacement: T): void { |
There was a problem hiding this comment.
Add the required public API example
issue: replaceMockable is a new public API, but its TSDoc contains no @example, contrary to the repository's public-API documentation requirements. Add a runtime example showing the import and emphasizing that callers must pass the same original reference used by mockable; the adjacent example only demonstrates the separate browser-core test helper.
Useful? React with 👍 / 👎.
Motivation
The Shopify app pixel plugin (upcoming PR in this stack) needs to swap in host-specific implementations of a few core functions, such as the session store and session manager. Those functions are already wrapped with
mockable(), butmockable()only honors replacements in test builds (__BUILD_ENV__SDK_VERSION__ === 'test'), and the only way to register one is the test-onlyreplaceMockablehelper in@datadog/browser-core/test.This PR makes replacements work in production builds so integrations can use the existing
mockable()wrappers instead of adding a new extension mechanism.Stacked on #5140.
Changes
@datadog/js-core/utilmockable()now returns the registered replacement in all builds, not only test builds.replaceMockable(value, replacement)registers a replacement at runtime. Registering the same value again overwrites the previous replacement (last wins).api/util.api.md) updated.@datadog/browser-core/testreplaceMockablekeeps its behavior (throws if the value is already mocked, cleans up after each test) and now delegates the registration to the js-core function.Impact
Map.haspermockable()call, against a map that is empty unless an integration registered a replacement. The most frequent call sites run per view, per navigation (locationChangeObservable), or per render of the Next.js router components, so the cost is negligible.mockable()only behaves differently once something callsreplaceMockable, which was previously only possible from tests.Open question: at runtime, registering the same value twice silently overwrites (covered by a unit test). Should
replaceMockablewarn viadisplay.warnin that case? I kept last-wins for now, since throwing could breakinit().Test instructions
Unit tests only. Every existing spec using
replaceMockablenow goes through the new path.yarn test:unit --spec packages/js-core/src/util/mockable.spec.ts --spec packages/browser-core/test/replaceMockable.spec.ts yarn test:unit yarn workspace @datadog/js-core build && yarn api:checkChecklist
🤖 Generated with Claude Code