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

Test instructions

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: 91.30%
Overall Coverage: 77.27% (+0.00%)

View detailed report

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: c9c4f10 | 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 167.98 KiB 168.07 KiB +98 B +0.06%
Rum Profiler 4.32 KiB 4.32 KiB 0 B 0.00%
Rum Recorder 24.54 KiB 24.54 KiB 0 B 0.00%
Logs 56.25 KiB 56.16 KiB -86 B -0.15%
Flagging 944 B 944 B 0 B 0.00%
Rum Slim 124.88 KiB 124.82 KiB -61 B -0.05%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance

Pending...

🧠 Memory Performance

Pending...

🔗 RealWorld

@BenoitZugmeyer BenoitZugmeyer force-pushed the benoit/mock branch 2 times, most recently from 98f56db to 2fbac02 Compare January 28, 2026 14:57
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.

2 participants