Skip to content
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

fix: mock property support symbol #62

Merged
merged 1 commit into from
Dec 21, 2024
Merged

fix: mock property support symbol #62

merged 1 commit into from
Dec 21, 2024

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Dec 21, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced mocking capabilities with support for Symbol and number properties.
    • Expanded test coverage for various asynchronous behaviors and error handling scenarios.
  • Bug Fixes

    • Improved error handling in tests for file reading functions and HTTP requests.
  • Documentation

    • Updated test descriptions to reflect new functionalities and broader scope.
  • Refactor

    • Updated type annotations for property parameters to improve type safety.

Copy link

coderabbitai bot commented Dec 21, 2024

Walkthrough

The pull request introduces modifications to a mocking library, focusing on enhancing type flexibility and removing the dedicated TypeScript declaration file. The changes primarily involve updating function signatures to support PropertyKey instead of just string, allowing for more versatile property mocking. The dependency on @cnpmjs/muk-prop has been upgraded to version ^1.1.0, and the src/index.d.ts file containing the original mocking namespace has been completely removed.

Changes

File Change Summary
package.json Upgraded @cnpmjs/muk-prop dependency from ^1.0.0 to ^1.1.0
src/index.ts Updated multiple function signatures to use PropertyKey instead of string for property parameters
src/index.d.ts Completely removed TypeScript declaration file for mm namespace
test/mm.test.ts Updated test suite to reflect new mocking capabilities and expanded test coverage

Poem

🐰 Mock, mock, with symbols bright,
Keys that dance in type's pure light!
From strings to symbols, we now roam,
Our mocking framework finds its home!
Flexibility leaps with rabbit might! 🚀

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

pkg-pr-new bot commented Dec 21, 2024

Open in Stackblitz

npm i https://pkg.pr.new/node-modules/mm@62

commit: d3b4893

Copy link

codecov bot commented Dec 21, 2024

Codecov Report

Attention: Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.

Project coverage is 95.52%. Comparing base (f1eadcc) to head (d3b4893).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/index.ts 94.11% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master      #62   +/-   ##
=======================================
  Coverage   95.52%   95.52%           
=======================================
  Files           1        1           
  Lines         648      648           
  Branches      123      123           
=======================================
  Hits          619      619           
  Misses         29       29           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@coderabbitai coderabbitai bot left a 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)
src/index.ts (1)

248-248: Line 248 not covered by tests.
No existing test calls 'spy' on a non-function property, leaving this line untested. Consider adding a test to improve coverage.

Below is a diff snippet that adds a new test block to cover this scenario:

+describe('spy non-function property coverage', () => {
+  it('should throw error when property is not a function', () => {
+    const obj = { prop: 123 };
+    (function() {
+      mm.spy(obj, 'prop');
+    }).should.throw(Error, { message: 'spy target prop is not a function' });
+  });
+});
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 248-248: src/index.ts#L248
Added line #L248 was not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 13a540c and d3b4893.

📒 Files selected for processing (4)
  • package.json (1 hunks)
  • src/index.d.ts (0 hunks)
  • src/index.ts (14 hunks)
  • test/mm.test.ts (2 hunks)
💤 Files with no reviewable changes (1)
  • src/index.d.ts
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/index.ts

[warning] 248-248: src/index.ts#L248
Added line #L248 was not covered by tests

🔇 Additional comments (21)
test/mm.test.ts (4)

766-782: Tests for environment variable mocking are robust.
The revised tests successfully validate that environment variables can be mocked and restored. This ensures consistency in environment-dependent behaviors.


784-790: Symbol property mocking validated successfully.
The test case effectively ensures that symbol property mocking functions as expected, reflecting the newly added support for PropertyKey.


792-796: Number property mocking validated successfully.
This test scenario confirms that numeric property mocking is supported, aligned with the expanded PropertyKey usage.


847-847: Comprehensive coverage for complex property mocking.
This test case demonstrates mocking functions that themselves contain additional properties, extending coverage to more intricate mocking scenarios. Implementation details seem correct.

package.json (1)

22-22: Dependency version upgraded to ^1.1.0.
The updated version may include critical fixes or improvements. Confirm there are no breaking changes that could affect existing functionality.

✅ Verification successful

Version upgrade from ^1.0.0 to ^1.1.0 is safe to proceed

The verification shows:

  • Node.js version requirement (>= 18.19.0) matches in both package.json and the dependency
  • The package is used for basic mocking functionality (muk, isMocked, restore) which are core features
  • The minor version bump (1.0.0 to 1.1.0) follows semver, indicating only backward-compatible changes
  • The dependency has minimal usage in the codebase (only in src/index.ts)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify if there are known issues with @cnpmjs/muk-prop@^1.1.0
npm view @cnpmjs/muk-prop@^1.1.0 dependencies
npm view @cnpmjs/muk-prop@^1.1.0 engines

Length of output: 117


Script:

#!/bin/bash
# Check current Node.js version requirement in package.json
rg '"node":\s*"[^"]*"' package.json

# Find usage of muk-prop in the codebase
rg -l "@cnpmjs/muk-prop"

# Check the previous version of muk-prop in package.json from git history
git show HEAD~1:package.json | rg "muk-prop"

Length of output: 192


Script:

#!/bin/bash
# Check how muk-prop is used in the codebase
rg -A 5 "@cnpmjs/muk-prop" src/index.ts

Length of output: 235

src/index.ts (16)

13-17: Enhanced type safety for 'mock()'.
Switching the property parameter to PropertyKey allows both string and symbol-based property mocking. This seamlessly accommodates the new test cases in “mm.test.ts”.


Line range hint 18-41: Refined 'spyFunction' for robust function tracking.
Changing the function signature to accept PropertyKey broadens the spy to symbol-based method names. The proxy usage is well-structured.


35-35: Proper error handling for mocking async functions.
Throwing an error for mismatched async usage is prudent, preventing unexpected behavior.


42-42: Broadening 'isAsyncLikeFunction' to PropertyKey.
This supports a consistent approach to symbol-based property checks. Implementation remains straightforward.


Line range hint 93-128: '_mockError' expansion to handle symbol-based methods.
Allowing symbols for module method names aligns with the broader shift to PropertyKey. Timeout logic and callback invocation appear correct.


Line range hint 143-147: 'mockError' updated to accept string | symbol.
This change continues the consistent pattern across function signatures. Implementation has no apparent gaps.


Line range hint 157-161: 'errorOnce' alignment with symbol-based property keys.
The new parameter type is consistent. No functional regressions noted.


Line range hint 171-207: 'mockDatas' for multi-value returns, improved type coverage.
Broadening “method” to PropertyKey is consistent. The logic for asynchronous generations remains intact.


Line range hint 211-218: 'mockData' reusing 'mockDatas' internally is well-structured.
This reduces duplication and keeps the code DRY. Good approach.


Line range hint 220-229: 'dataWithAsyncDispose' supporting symbol-based mocking.
Implementation is concise and consistent with the rest of the code changes.


Line range hint 237-244: 'mockEmpty' to return null values with symbol-based method.
Retains the original intention for mocking empty results, with minimal overhead.


Line range hint 246-256: 'spy' can now handle symbols while verifying target is a function.
Implementation ensures function references are strictly validated.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 248-248: src/index.ts#L248
Added line #L248 was not covered by tests

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 23)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 22)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 20)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 18.19.0)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 18)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 23)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 22)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 18.19.0)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 20)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 18)

[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 243-243:
Expected @param names to be "mod, method". Got "mod,, method,"


Line range hint 265-274: 'syncError' also supports symbol-based methods.
No issues found—this approach parallels existing logic.


Line range hint 279-288: 'syncData' includes symbol property coverage.
Implementation continues the same pattern of broadening property keys.


Line range hint 291-299: 'syncEmpty' consistently extends 'syncData'.
No functional discrepancies noted.

🧰 Tools
🪛 GitHub Check: Node.js / Test (ubuntu-latest, 23)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 22)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 20)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 18.19.0)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 18)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 23)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 22)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 18.19.0)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 20)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"

🪛 GitHub Check: Node.js / Test (macos-latest, 18)

[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


[warning] 288-288:
Expected @param names to be "mod, method". Got "mod,, method,"


584-584: 'classMethod' supporting class prototypes with PropertyKey.
Implementation is straightforward, ensuring consistent mocking for symbol-based or numeric properties in class prototypes.

@fengmk2 fengmk2 merged commit a7950f3 into master Dec 21, 2024
22 of 23 checks passed
@fengmk2 fengmk2 deleted the support-symbol branch December 21, 2024 15:37
fengmk2 pushed a commit that referenced this pull request Dec 21, 2024
[skip ci]

## [4.0.1](v4.0.0...v4.0.1) (2024-12-21)

### Bug Fixes

* mock property support symbol ([#62](#62)) ([a7950f3](a7950f3))
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