-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
docs(JestObjectAPI.md): incorrect specification to the method jest.replaceProperty #14388
Conversation
✅ Deploy Preview for jestjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
Not sure. That can be a bug in code as well. Hm.. Perhaps not (see this test). @michal-kocarek Would you remember why |
docs/JestObjectAPI.md
Outdated
@@ -669,7 +669,7 @@ Creates a mock function similar to `jest.fn` but also tracks calls to `object[me | |||
|
|||
:::note | |||
|
|||
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `jest.replaceProperty(object, methodName, jest.fn(() => customImplementation));` | |||
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)`. |
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.
This is how it sounded before #13496
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)`. | |
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation)`. |
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.
This behavior still working on "jest": "^29.6.2"
:
object[methodName] = jest.fn(() => customImplementation)
Let me show how the things are working in my example:
class Math {
total;
constructor() {}
sum(num1, num2) {
return num1 + num2;
}
}
example.test.js
math["sum"] = (num1, num2) => num1 * num2; // works well
jest.spyOn(math, "sum").mockImplementation((num1, num2) => num1 * num2); // works well
jest.replaceProperty(math, "total", 20); // works well
jest.replaceProperty(math, "sum", jest.fn((num1, num2) => num1 * num2)); // Cannot replace the `sum` property because it is a function. Use `jest.spyOn(object, 'sum')` instead.
I think maybe we just have a mismatch in the docs 🤔 |
Yes, it would be enough to change the sentence as it was before #13496. Just like in the suggestion which I left. Could you do that, please? |
Of course! Done ✅ |
Perfect! Could you make the same change in all applicable versions in the |
Yes! Done too ✅ |
We have a fail on step |
That’s right. The fail is not related, #14371 should fix that. |
I merged with main and passed in all steps on CI. Maybe good to merge? |
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.
Thanks!
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
The issue is correlated with the documentation that propose the use of
jest.replaceProperty
to mock amethod
inside a classIf you follow this recommendation your test will output:
Test plan