Skip to content
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

suggestion: Add some way to provide implementation #312

Open
safareli opened this issue Sep 19, 2022 · 4 comments
Open

suggestion: Add some way to provide implementation #312

safareli opened this issue Sep 19, 2022 · 4 comments

Comments

@safareli
Copy link

When using strong-mock what i'm missing sometimes from jest is mockImplementation.

Would be great to be able to do something similar:

interface FS {
  write: (content: string, out: fs.WriteStream) => void;
}

const fs = mock<FS>();
const writeImplementation = () => {...}
when(() => fs.write("a", It.isAny())).useImplementation(writeImplementation);
when(() => fs.write("b", It.isAny())).useImplementation(writeImplementation);

declare const ws: WriteStream;
fs.write("a",ws);
fs.write("b",ws);

or

when(() => fs.write).useImplementation(writeImplementation);
when(() => fs.write("a", It.isAny()));
when(() => fs.write("b", It.isAny()));
@NiGhTTraX
Copy link
Owner

Hey @safareli, I documented why this is not implemented, and how you can work around it, in the FAQ.

I'm curious to know your use cases. I usually find that I can change the design, or the tests, before needing to combine argument matchers with fakes. One can also use It.willCapture to verify arguments outside of an expectation.


Your second example is interesting. If I understand it correctly, you mock the function and provide an implementation, and then set the argument expectations. It doesn't really work with the existing when API, since there's no thenReturn, but it could work with a different API.

@safareli
Copy link
Author

Both of my examples are trying to express same expectations but with different potential api.

My use case is basically what's in example. I want to test that write is called and I also want data to be written to the WriteStream. it wouldn't be quite doable with existing strong-mock api.

I've read the FAQ section but when using that approach we loose power of strong-mock with regards to asserting arguments shape.

For example to solve this particular use case i'm falling back to jest like this:.

const writeImplementation = () => {...}
const fs = {
  write: jest.fn(writeImplementation)
}


declare const ws: WriteStream;
fs.write("a",ws);
fs.write("b",ws);


expect(fs.write.calls[0][0]).toEqual("a")
expect(fs.write.calls[1][0]).toEqual("b")

@NiGhTTraX
Copy link
Owner

also want data to be written to the WriteStream

But why? :) Does the code under test need to observe that the data was written? If so, you could do this:

const out = It.willCapture<WriteStream>();
when(() => fs.write("a", out)).thenReturn();

fs.write("a", ws);

// This is the captured ws.
out?.value.write("...");

I've read the FAQ section but when using that approach we loose power of strong-mock with regards to asserting arguments shape

Correct, the workaround is not a full replacement for mockImplementation.

@safareli
Copy link
Author

That could work for simple tests.

But let's say you are building up more complicated environment for some function to test, it contains FS maybe some network services and then this function under test will invoke some things from those services. now question is when should we call out?.value.write("...");? answer is we don't know. if on other hand we had something like useImplementation we basically get a hook which can be used to call out?.value.write("...");.

@NiGhTTraX NiGhTTraX changed the title Add some way to provide implementation suggestion: Add some way to provide implementation Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants