Skip to content

✅ Introduce a new way to mock values in unit tests#4129

Open
BenoitZugmeyer wants to merge 12 commits intomainfrom
benoit/mock
Open

✅ Introduce a new way to mock values in unit tests#4129
BenoitZugmeyer wants to merge 12 commits intomainfrom
benoit/mock

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

Fix all issues with Cursor

⚠️ Warnings

❄️ 2 New flaky tests detected

cookie getCurrentSite caches the result from Safari 12.1.2 (Mac OS 10.14.6) (Datadog) (Fix with Cursor)
Expected spy cookie to have been called 2 times. It was called 0 times.
<Jasmine>
webpack:///packages/core/src/browser/cookie.spec.ts:46:43 <- /tmp/_karma_webpack_722687/commons.js:48873:49
<Jasmine>
cookie getCurrentSite returns the eTLD+1 for foo.bar.baz.example.com from Safari 12.1.2 (Mac OS 10.14.6) (Datadog) (Fix with Cursor)
Expected 'foo.bar.baz.example.com' to be 'example.com'.
<Jasmine>
webpack:///packages/core/src/browser/cookie.spec.ts:20:61 <- /tmp/_karma_webpack_722687/commons.js:48853:108
<Jasmine>

ℹ️ Info

🧪 All tests passed

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

View detailed report

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: b98ddf1 | 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 +114 B +0.07%
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.16 KiB -103 B -0.08%
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 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
@BenoitZugmeyer BenoitZugmeyer marked this pull request as ready for review February 12, 2026 09:06
@BenoitZugmeyer BenoitZugmeyer requested a review from a team as a code owner February 12, 2026 09:06
@BenoitZugmeyer
Copy link
Member Author

/to-staging

@gh-worker-devflow-routing-ef8351
Copy link

gh-worker-devflow-routing-ef8351 bot commented Feb 12, 2026

View all feedbacks in Devflow UI.

2026-02-12 09:11:06 UTC ℹ️ Start processing command /to-staging


2026-02-12 09:11:12 UTC ℹ️ Branch Integration: starting soon, merge expected in approximately 14m (p90)

Commit 518ac81fe5 will soon be integrated into staging-07.


2026-02-12 09:22:05 UTC ℹ️ Branch Integration: this commit was successfully integrated

Commit 518ac81fe5 has been merged into staging-07 in merge commit 378f13e872.

Check out the triggered DDCI request.

If you need to revert this integration, you can use the following command: /code revert-integration -b staging-07

Copy link

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

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: 518ac81fe5

ℹ️ 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".

gh-worker-dd-mergequeue-cf854d bot added a commit that referenced this pull request Feb 12, 2026
Integrated commit sha: 518ac81

Co-authored-by: BenoitZugmeyer <benoit.zugmeyer@datadoghq.com>
strategy = createPostStartStrategy(initConfiguration, startLogsResult)
return startLogsResult
},
startTelemetryImpl
Copy link
Collaborator

Choose a reason for hiding this comment

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

👏 praise: ‏Nice! It was just argument drilling!

Comment on lines +74 to +79
### Unit Tests

- Spec files co-located with implementation: `feature.ts` → `feature.spec.ts`
- Test framework: Jasmine + Karma. Spec files co-located with implementation: `feature.ts` → `feature.spec.ts`
- Focus tests with `fit()` / `fdescribe()`, skip with `xit()` / `xdescribe()`
- Use `registerCleanupTask()` for cleanup, NOT `afterEach()`
- Test framework: Jasmine + Karma
- Mock values/functions: wrap with `mockable()` in source, use `replaceMockable()` or `replaceMockableWithSpy()` in tests (auto-cleanup)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👏 praise: ‏Yeah! 🤖

This change was generated by Claude to test coding agents capabilities
to use the new mocking pattern. We used the following prompt:

> Dear claude. In @packages/rum-core/src/domain/waitPageActivityEnd.ts
and its spec, we have the `doWaitPageActivityEnd` function for testing
purpose, because we don't want to run `createPageActivityObservable` in
tests. Let's remove this extra function and use mocks instead instead.
@BenoitZugmeyer
Copy link
Member Author

/to-staging

@gh-worker-devflow-routing-ef8351
Copy link

gh-worker-devflow-routing-ef8351 bot commented Feb 12, 2026

View all feedbacks in Devflow UI.

2026-02-12 14:42:39 UTC ℹ️ Start processing command /to-staging


2026-02-12 14:42:49 UTC ℹ️ Branch Integration: starting soon, merge expected in approximately 14m (p90)

Commit b98ddf140b will soon be integrated into staging-07.


2026-02-12 14:53:42 UTC ℹ️ Branch Integration: this commit was successfully integrated

Commit b98ddf140b has been merged into staging-07 in merge commit 687815cd88.

Check out the triggered DDCI request.

If you need to revert this integration, you can use the following command: /code revert-integration -b staging-07

gh-worker-dd-mergequeue-cf854d bot added a commit that referenced this pull request Feb 12, 2026
Integrated commit sha: b98ddf1

Co-authored-by: BenoitZugmeyer <benoit.zugmeyer@datadoghq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants