-
Notifications
You must be signed in to change notification settings - Fork 632
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
Feature: a way replace inner function in spy
mocking
#6234
Comments
How exactly would this look like in terms of Deno.test("test", () => {
let cnt = 0;
function addOne() {
cnt++;
return cnt;
}
function addTwo() {
cnt += 2;
return cnt;
}
function throws() {
throw new Error("error");
}
const fn = spy(addOne);
assertEquals(fn(), 1);
fn.mockImplementation(addTwo);
assertEquals(fn(), 3);
assertEquals(fn(), 5);
fn.mockImplementation(throws as () => number);
assertThrows(fn, "error");
}); |
LGTM
|
cc @KyleJune What do you think of this kind of addition? |
Yes, I checked the code. Just add a setter here. Lines 653 to 656 in 2ad1564
|
That original property you linked is on the methodSpy but there is a similar one on functionSpy. The purpose of it is to be able to access the original value of the property. Making it writable and giving the user access to override it would basically turn spys into stubs. It would also make the property confusing since the name implies it would give you the original function being spied on while it actually would be giving you the stub function. I don't think original should be made writable. If you want to make it possible to turn a spy into a stub or to replace the replacement function of a stub, I would start with changing it so that the fake property on stubs is writable and updating the stub code so that it will call that property instead of the fake const. That would make it possible to swap the fake version of the function with a new fake that you would like the stub to use going forward. Lines 1106 to 1109 in 2ad1564
Then for making it possible to convert a spy, I'd suggest adding a fake property to spies that defaults to undefined. Then if the user sets it, make the spy call through to the fake instead of the original if the fake property is set. That would also make it possible to detect if a spy has been converted into a stub. Oh also instead of just making it a writable, I'd actually change it so that the fake variable is declared with let instead of const. Then the fake property descriptor would have a getter that returns the vale and a setter that overwrites it. That way we could have the setter throw an error if the user tries to set it to anything other than undefined or a function. I don't like the idea of there being 2 different ways to change the implementation used behind the mock. I'd suggest choosing to either make fake property a getter/setter, or add a function like replaceStub. Personally, I'd prefer the fake property getter/setter approach. Edit: Also worth noting if this change is made, I think stub could be refactored to just call through to spy, then setting the fake property to be the stub function before returning it. That would remove some code duplication. |
@KyleJune I agree with you. Let's not make So the API would probably look like: const fn = spy(addOne);
assertEquals(fn(), 1);
fn.fake = addTwo;
assertEquals(fn(), 3);
assertEquals(fn(), 5);
fn.fake = throws;
assertThrows(fn); |
How about |
I think fake would be better because stub already has a fake property and it would be a breaking change to rename it vs. changing it to be writable via a setter. And it would be confusing if the property name was different for spy and stub. |
Is your feature request related to a problem? Please describe.
In an event emitter, I want to replace the handler to throw an error, to verify if the emitter can handle error
Describe the solution you'd like
Like
node:test
, you can replace the mock implementation withDescribe alternatives you've considered
make the
fn
inspy(fn)
a wrapper, which call an external function variable.The text was updated successfully, but these errors were encountered: