-
Notifications
You must be signed in to change notification settings - Fork 175
✅ Introduce a new way to mock values in unit tests #4129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
574d5e9
✅ cleanup lazyLoadRecorder.spec.ts
BenoitZugmeyer f9cd842
✅ make sure we don't initialize things two times
BenoitZugmeyer 95f66d3
✅ introduce a new way to mock values
BenoitZugmeyer a339f5d
✅ replace all *Impl arguments with mockable
BenoitZugmeyer 2f64528
✅ use mockable to mock location
BenoitZugmeyer 16fd8e1
🤖 update AGENTS.md
BenoitZugmeyer 0ed26a5
👌 apply codex suggestion
BenoitZugmeyer 0a1eb6d
👌 use mockable for taskQueue
BenoitZugmeyer 0ce309a
👌 format nitpick
BenoitZugmeyer fcdf0fb
📝 adjust documentation
BenoitZugmeyer 2094c2e
✅ use mockable in `waitPageActivityEnd.spec.ts`
BenoitZugmeyer b98ddf1
fix typecheck
BenoitZugmeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| declare const __BUILD_ENV__SDK_VERSION__: string | ||
|
|
||
| export const mockableReplacements = new Map<unknown, unknown>() | ||
|
|
||
| /** | ||
| * Wraps a value to make it mockable in tests. In production builds, this is a no-op | ||
| * that returns the value as-is. In test builds, it checks if a mock replacement has | ||
| * been registered and returns that instead. | ||
| * | ||
| * @example | ||
| * // In source file: | ||
| * import { mockable } from '../tools/mockable' | ||
| * export function formatNavigationEntry(): string { | ||
| * const navigationEntry = mockable(getNavigationEntry)() | ||
| * ... | ||
| * } | ||
| * | ||
| * // In test file: | ||
| * import { replaceMockable } from '@datadog/browser-core/test' | ||
| * it('...', () => { | ||
| * replaceMockable(getNavigationEntry, () => FAKE_NAVIGATION_ENTRY) | ||
| * expect(formatNavigationEntry()).toEqual(...) | ||
| * }) | ||
| */ | ||
| export function mockable<T>(value: T): T { | ||
| // In test builds, return a wrapper that checks for mocks at call time | ||
| if (__BUILD_ENV__SDK_VERSION__ === 'test' && mockableReplacements.has(value)) { | ||
| return mockableReplacements.get(value)! as T | ||
| } | ||
| // In production, return the value as-is | ||
| return value | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 praise: Yeah! 🤖