-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
test_runner: mock.mockImplementationOnce only works for the last call #47718
Comments
I think this is working as intended/documented. |
Hmm got it! Why don't put an internal counter to avoid having this: m.mock.mockImplementationOnce(() => 1, 0)
m.mock.mockImplementationOnce(() => 2, 1) to this m.mock.mockImplementationOnce(() => 1)
m.mock.mockImplementationOnce(() => 2) and even enable nested calls such as: m.mock
.mockImplementationOnce(() => 1)
.mockImplementationOnce(() => 2) Other mocking libraries such as Jest work like this so I think for dev experience would be best to follow the same idea. WDYT? |
I'm not opposed if someone can make it work well, but there are edge cases that will need to be addressed. I don't think it's as straightforward as incrementing a counter when const m = mock.method(...);
m(); // Original mock provided by mock.method()
m.mock.mockImplementationOnce(() => 1);
m.mock.mockImplementationOnce(() => 2);
m(); // Returns 1
m(); // Returns 2
m(); // Original mock provided by mock.method()
m(); // Original mock provided by mock.method()
// What should the next line do?
m.mock.mockImplementationOnce(() => 3);
m(); In this example, I would expect the final
Side note: To be clear, just because Jest does something a particular way does not mean we should copy it. |
Nice I got it!
Yes, I agree with you. I only mention them as I like how the API works and thought it'd be easier to follow the same pattern. I'm gonna try doing a PoC and open a draft PR for it so we could discuss along the way |
Side note: possible implementation is having a queue for the mocks, this way this example would work as expected
|
I think it's a valuable feature |
Version
v21.0.0-pre
Platform
Darwin MacBook-Pro-4.local 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64
Subsystem
No response
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
mock.mockImplementationOnce should be used to mock each individual call.
given
Each call result should be returned given the order configured by the mockImplementation Once
What do you see instead?
Only the last mock.mockImplementationOnce is applied.
Additional information
I'd also enable sequence calls like:
@nodejs/test_runner
The text was updated successfully, but these errors were encountered: