-
-
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
Added support for naming mocked functions #4586
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4586 +/- ##
==========================================
- Coverage 55.68% 55.67% -0.01%
==========================================
Files 186 186
Lines 6348 6356 +8
Branches 3 3
==========================================
+ Hits 3535 3539 +4
- Misses 2812 2816 +4
Partials 1 1
Continue to review full report at Codecov.
|
@gricard You can add an integration test, there you can get both stdout and stderr. Lots of examples in there |
Thanks, @SimenB. |
Interesting... AppVeyor build failed, and it looks like it's because the snapshot tests failed due to slight differences in the characters used. Should I not have committed the snapshots created on my Mac? That appears to be the problem. Output differences between macOS / Windows.
|
…ts vary by platform, apparently
Well, if the test output varies by platform, then I'll do regex matching instead |
You can do |
docs/en/MockFunctionAPI.md
Outdated
@@ -138,13 +138,32 @@ console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn()); | |||
> 'first call', 'second call', 'default', 'default' | |||
``` | |||
|
|||
### `mockFn.mockReturnThis()` |
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.
why is this removed?
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.
Looks like an accident on my part. I'll fix it.
test('first test', () => { | ||
mockFn(); | ||
expect(mockFn).toHaveBeenCalled(); | ||
expect(mockFn.mock.calls.length).toBe(1); |
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.
any particular reason why you do this instead of expect(mockFn).toHaveBeenCalledTimes(1)
?
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.
No reason. I just copied what was in another test suite in there that I based mine on.
@@ -0,0 +1,70 @@ | |||
/** |
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.
Can you add a test for failing expect(mockWithName).not.toHaveBeenCalled()
and expect(mockWithName).toHaveBeenCalledTimes(5)
to assert on the name for them as well? I see no reason for them not to work as you expect, but having the assertions would be nice
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.
So, a test where the assertion expect(mockWithName).not.toHaveBeenCalled()
will fail, and then check for that error?
And a test for expect(mockWithName).toHaveBeenCalledTimes(5)
when it's not called 5 times?
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.
Ok, I've added more tests to cover those.
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.
Yup. Thanks!
I quite like it. Thanks for contributing this change to Jets. However, I'd prefer to not add an argument to |
Whatever floats your boat. |
…fn() and only use jest.fn().mockName() to set it
Remove obsolete unit tests
Hm. Well, I made the changes, but circleci test-node-8 is failing because of an unrelated test failure.
|
Restarted it, and CI is happy now |
Sweet. Thanks, dude. |
Question: Is the name cleared by calling |
Ooh. Yeah. That makes sense. I didn't think of that. I can change it so that clears out the name also. |
I think it should |
I took a look at the docs for all of those. I think only mockReset() makes sense, in terms of clearing out the name. mockRestore() is specific to the mocked implementation. |
Yep, agreed. :) |
It looks like mockReset() already does this, because it clears out the mock entirely with its config (where the mock name is stored). I've added a unit test to verify this, and updated the documentation for mockReset(). |
For funsies, can you add a test that verifies that (I always have to check the docs for the purpose of the 3 different resets, so having super explicit tests is a good thing I think). It should be good to merge either way, this is awesome! Gonna start using it as soon as it's released 🙂 |
All set, @SimenB |
And thanks! I really like Jest and this will help me out a lot as well. Hopefully I can contribute some more to the project. :) |
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.
LGTM (pending CI). Thanks for sticking around for our nit-picking
Alright, I think this is almost good to go, I would just like to request one more change (sorry :( ). Could we split the function up into |
… getMockName() instead of overloading mockName() function to return it when no arguments are passed
All set |
Thanks for sticking with us, this is a great PR :) |
Thanks, dude! |
.mockImplementation(scalar => 42 + scalar) | ||
.mockName('add42'); | ||
|
||
const myMockFn2 = jest.fn(scalar => 42 + scalar, 'add42'); |
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 wrong now
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.
Damn. I thought I caught all the doc references. Can I still commit changes for this PR, or is that done since it's merged?
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.
New PR is needed
@@ -217,6 +217,19 @@ const otherObj = { | |||
}; | |||
``` | |||
|
|||
## Mock Names |
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.
should it include a "availble in jest 21.4" or something?
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
This patch adds the ability to specify a name for a just.fn() mock, so if the test fails, you will see that name in the error output instead of "just.fn()", which should help make some test results clearer. It provides a few different ways to specify the mock name.
Motivation
I switched a frontend product over to Jest from Karma/Mocha/Chai a few months ago, and I love it. But there's a couple things that I wanted to see in the output, like specified mock names, and I did not see any existing means to add that to my tests.
Test plan
Unit tests and integration tests have been added.