-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
test: add comprehensive test coverage for utils.ts #32907
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
base: next
Are you sure you want to change the base?
Conversation
- Add 13 test cases for groupBy and invariant functions - Cover edge cases, type narrowing, and lazy evaluation - Achieve 100% coverage for utils.ts utility functions Tests include: - groupBy: various key types, empty arrays, index handling - invariant: lazy messages, type assertions, edge cases
📝 WalkthroughWalkthroughAdds comprehensive unit tests for Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10-15 minutes
✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
code/core/src/common/utils/utils.test.ts (1)
45-55: Consider verifying the grouping result.While this test correctly verifies that indices are passed to the key selector, it doesn't assert the actual grouping result. Consider also verifying that the returned object matches expectations for completeness.
Apply this diff to also verify the grouping result:
it('should pass index to key selector', () => { const items = ['a', 'b', 'c', 'd']; const indices: number[] = []; - groupBy(items, (_item, index) => { + const result = groupBy(items, (_item, index) => { indices.push(index); return index % 2 === 0 ? 'even' : 'odd'; }); expect(indices).toEqual([0, 1, 2, 3]); + expect(result).toEqual({ + even: ['a', 'c'], + odd: ['b', 'd'], + }); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
code/core/src/common/utils/utils.test.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
code/**/*.{test,spec}.{ts,tsx}
📄 CodeRabbit inference engine (.cursorrules)
code/**/*.{test,spec}.{ts,tsx}: Place all test files under the code/ directory
Name test files as *.test.ts, *.test.tsx, *.spec.ts, or *.spec.tsx
Files:
code/core/src/common/utils/utils.test.ts
**/*.test.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/spy-mocking.mdc)
**/*.test.{ts,tsx,js,jsx}: Use vi.mock() with the spy: true option for all package and file mocks in Vitest tests
Place all mocks at the top of the test file before any test cases
Use vi.mocked() to type and access mocked functions
Implement mock behaviors in beforeEach blocks
Mock all required dependencies that the test subject uses
Mock implementations should be placed in beforeEach blocks
Each mock implementation should return a Promise for async functions
Mock implementations should match the expected return type of the original function
Use vi.mocked() to access and implement mock behaviors
Mock all required properties and methods that the test subject uses
Avoid direct function mocking without vi.mocked()
Avoid mock implementations outside of beforeEach blocks
Avoid mocking without the spy: true option
Avoid inline mock implementations within test cases
Avoid mocking only a subset of required dependencies
Mock at the highest level of abstraction needed
Keep mock implementations simple and focused
Use type-safe mocking with vi.mocked()
Document complex mock behaviors
Group related mocks together
Files:
code/core/src/common/utils/utils.test.ts
**/*.{js,jsx,json,html,ts,tsx,mjs}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.{js,jsx,json,html,ts,tsx,mjs}: Run Prettier formatting on changed files before committing
Run ESLint on changed files and fix all errors/warnings before committing (useyarn lint:js:cmd <file>)
Files:
code/core/src/common/utils/utils.test.ts
**/*.@(test|spec).{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.@(test|spec).{ts,tsx,js,jsx}: Unit tests should import and execute the functions under test rather than only asserting on syntax patterns
Mock external dependencies in tests usingvi.mock()(e.g., filesystem, loggers)
Files:
code/core/src/common/utils/utils.test.ts
**/*.{ts,tsx,js,jsx,mjs}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Export functions from modules when they need to be unit-tested
Files:
code/core/src/common/utils/utils.test.ts
code/**/*.{ts,tsx,js,jsx,mjs}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
In application code, use Storybook loggers instead of
console.*(client code:storybook/internal/client-logger; server code:storybook/internal/node-logger)
Files:
code/core/src/common/utils/utils.test.ts
{code/**,scripts/**}/**/*.{ts,tsx,js,jsx,mjs}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Do not use
console.log,console.warn, orconsole.errordirectly unless in isolated files where importing loggers would significantly increase bundle size
Files:
code/core/src/common/utils/utils.test.ts
🧠 Learnings (2)
📚 Learning: 2025-09-17T08:11:47.197Z
Learnt from: CR
Repo: storybookjs/storybook PR: 0
File: .cursor/rules/spy-mocking.mdc:0-0
Timestamp: 2025-09-17T08:11:47.197Z
Learning: Applies to **/*.test.{ts,tsx,js,jsx} : Group related mocks together
Applied to files:
code/core/src/common/utils/utils.test.ts
📚 Learning: 2025-10-13T13:33:14.659Z
Learnt from: CR
Repo: storybookjs/storybook PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-13T13:33:14.659Z
Learning: Applies to **/*.@(test|spec).{ts,tsx,js,jsx} : Unit tests should import and execute the functions under test rather than only asserting on syntax patterns
Applied to files:
code/core/src/common/utils/utils.test.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: normal
- GitHub Check: Core Unit Tests, windows-latest
🔇 Additional comments (3)
code/core/src/common/utils/utils.test.ts (3)
1-3: LGTM! Clean imports for testing pure utilities.The imports are minimal and appropriate. No mocks are needed since
groupByandinvariantare pure utility functions without external dependencies.
5-106: Excellent test coverage for groupBy!The test suite is comprehensive, covering basic functionality, edge cases (empty arrays, single items), different key types (string, numeric, symbol), and index-aware key selectors. The tests are well-structured and effectively validate the function's behavior.
108-224: Excellent test coverage for invariant!The test suite thoroughly validates the invariant function, including:
- Truthy/falsy condition handling
- Default error messages
- Lazy message evaluation optimization (critical for performance)
- Type narrowing behavior
- Complex real-world scenarios
The lazy evaluation tests (lines 159-182) are particularly valuable as they verify the performance optimization benefit.
|
View your CI Pipeline Execution ↗ for commit 8f367da
☁️ Nx Cloud last updated this comment at |
What I did
Added comprehensive test coverage for the utility functions in
code/core/src/common/utils/utils.tswhich was recently added but lacked test coverage.This PR introduces 13 well-structured test cases covering all functionality and edge cases for the
groupByandinvariantutility functions.Changes Made:
code/core/src/common/utils/utils.test.tswith comprehensive test coveragegroupByfunctioninvariantfunctionChecklist for Contributors
Testing
Documentation
Summary by CodeRabbit
Release Notes
Note: This release contains internal quality improvements with no user-facing changes.