-
-
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
Rename jest.mock to jest.stub #7592
Comments
We have |
Hmm I'm not sure if there stub would be that wrong because we would still use the factory to return jest.fn() or an intercepted module object. And I am not sure if we would define the assertions in the factory? |
I think you may be interpreting "pre-programmed expectations" too narrowly. I don't think it requires the expectations to be defined "before the call happens at runtime" or "above the call in the source code" or something like that. I believe it means that we "pre-program" the expectations when writing the test, so it can still be a mock if the expectation is below the usage of the mock: const mock = jest.fn();
mock(42); // happens somewhere in the SUT
expect(mock).toHaveBeenCalledWith(42); I'm happy to be corrected though if you can link me to some literature that is more explicit about this. |
The whole "mock"/"stub"/"spy" stuff was very off-putting at the very beginning when I started testing because it's so hard to find a right answer to which is which (or was at the time; MS article tries really well to fix it, which is great). I still see no added value to differentiate that. In Jest I see EDIT: |
FWIW |
@jeysal In general practice, mocks have sort of a verification method and you call it after defining the expectations (see @thymikee after reading your comment, now I also think using And I also think introducing alternative API and deprecating old ones with upcoming releases is a good way to go. Thanks for elaborating the idea. |
A bit related to #6180 if we wanna mess with the overall API anyways, /cc @mattphillips @rickhanlonii |
Sure, I am aware that a
and the approach of Jest
is IMO a very minor detail. What distinguishes them from stubs is that they perform assertions on the calls that have occurred on them, while stubs exist to provide canned return values. |
I'm using jest for quite some time now and I also didn't care about it. I just read the jest's official documentation and started writing my tests. But I recently gave a presentation about testing and in that tried to explain the terminology, and then of course, I suggested to use jest being my favourite test runner at the moment. But then @jeysal I totally understand your argument and it is a minor detail, but it's a detail and seems like it doesn't fit with the literature. And definitely, I'm not an expert on this, I just spent quite some time with testing and read some, and as you said my understanding can be wrong. I'm aware that I'm suggesting this change to the developers who created the most popular testing framework existing at the moment. It's up to them to follow the old terminology or set the new ones. The thing I care about is Javascript always had issues with standardisation. By now we are in much better shape but we still need to make progress. I was inspired by Denis Radin's question "Would you get on a plane if it uses Javascript?" in this talk Denis Radin: Applying NASA coding standards to JavaScript | JSConf EU 2017 |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 Feature Proposal
It would be better to rename jest.mock to jest.stub, because actually what it is doing is stubbing the objects. It doesn't have pre-programmed expectations like mocks have.
Motivation
I love jest and using it for a quite while now. But jest.mock doesn't do what mocks do and that naming bothers me a little bit. I did a lot of research about testing and noticed that we are often using the term
mock
wrong in js community.I started my research based on this article but almost all articles I've read confirm this statement: https://martinfowler.com/articles/mocksArentStubs.html
I know it's a major change and I don't know if you already considered this and wanted to go with the term mock because it's more often used, however I think renaming it would bring value by avoiding confusions like I had.
Example
Here some of the documents that I've came across.
Pitch
This is a change to an existing Jest feature.
The text was updated successfully, but these errors were encountered: