Skip to content

♻️ [Core] Make mockable replacements available in production builds - #5160

Closed
BeltranBulbarellaDD wants to merge 1 commit into
beltran.bulbarella/shopify-regular-sdkfrom
beltran.bulbarella/shopify-pixel-2-runtime-mockable
Closed

BeltranBulbarellaDD wants to merge 1 commit into
beltran.bulbarella/shopify-regular-sdkfrom
beltran.bulbarella/shopify-pixel-2-runtime-mockable

Conversation

@BeltranBulbarellaDD

@BeltranBulbarellaDD BeltranBulbarellaDD commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

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(), but mockable() only honors replacements in test builds (__BUILD_ENV__SDK_VERSION__ === 'test'), and the only way to register one is the test-only replaceMockable helper 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/util
    • mockable() now returns the registered replacement in all builds, not only test builds.
    • New export replaceMockable(value, replacement) registers a replacement at runtime. Registering the same value again overwrites the previous replacement (last wins).
    • API Extractor report (api/util.api.md) updated.
  • @datadog/browser-core/test replaceMockable keeps 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

  • Runtime cost: one Map.has per mockable() 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.
  • No behavior change for existing consumers: mockable() only behaves differently once something calls replaceMockable, which was previously only possible from tests.

Open question: at runtime, registering the same value twice silently overwrites (covered by a unit test). Should replaceMockable warn via display.warn in that case? I kept last-wins for now, since throwing could break init().

Test instructions

Unit tests only. Every existing spec using replaceMockable now 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:check

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

🤖 Generated with Claude Code

@BeltranBulbarellaDD BeltranBulbarellaDD changed the title Make replaceMockable usable at runtime ♻️ [Core] Make mockable replacements available in production builds Oct 20, 2026
@datadog-prod-us1-4

datadog-prod-us1-4 Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 1 Pipeline job failed

Lint PR title | lint

View more details · View in GitHub Actions

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
• Patch Coverage: 66.67%
• Overall Coverage: 77.27% (-0.00%)

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 0253272 | Docs | View more details | Give us feedback!

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce N/A 188.52 KiB N/A N/A N/A
Rum 188.44 KiB 188.50 KiB +53 B +0.03% ✅
Rum-salesforce Profiler N/A 8.36 KiB N/A N/A N/A
Rum Profiler 8.35 KiB 8.36 KiB +6 B +0.07% ✅
Rum-salesforce Recorder N/A 32.04 KiB N/A N/A N/A
Rum Recorder 32.04 KiB 32.04 KiB +1 B +0.00% ✅
Logs 59.28 KiB 59.51 KiB +232 B +0.38% ✅
Rum Slim 145.95 KiB 146.17 KiB +233 B +0.16% ✅
Worker 22.96 KiB 22.96 KiB 0 B 0.00% ✅
Rum Shopify N/A 218.94 KiB N/A N/A N/A
Rum-shopify Profiler N/A 8.36 KiB N/A N/A N/A
Rum-shopify Recorder N/A 4.85 KiB N/A N/A N/A

@BeltranBulbarellaDD

Copy link
Copy Markdown
Contributor Author

@codex review pretty please

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T14:50:34.680426Z 0253272 Manual request
🔒 Security Review ✅ Completed 2026-10-21T14:49:21.521114Z 0253272 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 0253272e33

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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>()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 Badge 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 👍 / 👎.

@BeltranBulbarellaDD
BeltranBulbarellaDD removed this pull request from stack #5161 October 28, 2026 18:04
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 11, 2026
An error occurred while trying to automatically change base from beltran.bulbarella/shopify-regular-sdk to main November 18, 2026 16:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant