Skip to content

Conversation

@BenoitZugmeyer
Copy link
Member

@BenoitZugmeyer BenoitZugmeyer commented Jan 28, 2026

Motivation

This is a proposal to allow mocking values (mostly functions) within tests.

The current way to mock values used within a function is to pass them as argument of that function. For example:

// foo.ts
import { startBar } from 'bar'

function createFoo(startBarImpl = startBar) {
  const bar = startBarImpl()
}


// foo.spec.ts
it('create foo', () => {
  createFoo(mockStartBar)
})

However, forces to have unecessary extra arguments in functions. Impls also need to be propagated within subfunctions:

// foo.ts
function createFoo(startBarImpl = startBar) {
  const baz = trackBaz(startBarImpl)
}

// baz.ts
function trackBaz(startBarImpl = startBar) {
}

This PR introduce a new way to mock values that is similar to a lightweight dependency injection system. Values that can be mocked should be wrapped in a mocked() call when used, and tests can use mockValue() to replace the value or mockWithSpy() to replace it with a jasmine spy. The example above would be implemented as:

// foo.ts
import { startBar } from 'bar'

function createFoo() {
  const bar = mockable(startBar)()
}

// foo.spec.ts
it('create foo', () => {
  replaceMockable(startBar, mockStartBar)
  // or
  replaceMockableWithSpy(startBar).and.callFake(mockStartBar)
  createFoo()
})

Changes

The first 2 commits does a bit of spec cleaning.

Then, we introduce mockable capabilities and use it in tests, mostly to replace all *Impl arguments and location mocking.

Please review commit by commit.

Test instructions

If tests passes, it should be good.

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

@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Jan 28, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage
Patch Coverage: 90.91%
Overall Coverage: 77.27% (+0.01%)

View detailed report

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 518ac81 | Docs | Datadog PR Page | Was this helpful? Give us feedback!

@cit-pr-commenter-54b7da
Copy link

cit-pr-commenter-54b7da bot commented Jan 28, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 169.44 KiB 169.55 KiB +107 B +0.06%
Rum Profiler 4.31 KiB 4.31 KiB 0 B 0.00%
Rum Recorder 24.54 KiB 24.54 KiB 0 B 0.00%
Logs 56.72 KiB 56.62 KiB -104 B -0.18%
Flagging 944 B 944 B 0 B 0.00%
Rum Slim 126.26 KiB 126.18 KiB -84 B -0.06%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
RUM - add global context 0.004 0.0062 +55.00%
RUM - add action 0.0124 0.0194 +56.45%
RUM - add error 0.0121 0.0163 +34.71%
RUM - add timing 0.0025 0.0034 +36.00%
RUM - start view 0.0118 0.0217 +83.90%
RUM - start/stop session replay recording 0.0007 0.001 +42.86%
Logs - log message 0.0149 0.0192 +28.86%
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
RUM - add global context 27.32 KiB 27.49 KiB +172 B
RUM - add action 50.83 KiB 50.02 KiB -832 B
RUM - add timing 26.93 KiB 26.14 KiB -810 B
RUM - add error 57.36 KiB 55.71 KiB -1.65 KiB
RUM - start/stop session replay recording 26.81 KiB 25.92 KiB -910 B
RUM - start view 452.06 KiB 454.59 KiB +2.52 KiB
Logs - log message 45.88 KiB 46.69 KiB +829 B

🔗 RealWorld

@BenoitZugmeyer BenoitZugmeyer force-pushed the benoit/mock branch 3 times, most recently from 2fbac02 to 9182076 Compare January 29, 2026 20:46
This spec initialization is unnecessarily complex. This commit cleans
that spec and moves one higher level assertion in `recorderApi.spec.ts`
A few test cases is logsPublicApi and preStartRum run test
initialization multiple times (due to nested describe/beforeEach).

This commit cleans this up
@BenoitZugmeyer BenoitZugmeyer changed the title Benoit/mock ✅ Introduce a new way to mock values in unit tests Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant