-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Extension methods for MockBuilder #5417
Comments
There are at least this two options to achieve it:
What do you think? At the moment I solved it with a workaround:
And then Im using the CustomMockBuilder instead of the MockBuilder. But I would prefer a solution with extension methods when it would be possible :P |
THank you for the info. That makes sense. I'll add its support during next weeks. |
Do you have updates on this? |
Hi @AE1NS, happy Sunday! Would something like that work for you? declare module 'ng-mocks' {
interface IMockBuilder {
customMock(value: string): this;
}
}
MockBuilder.extend('customMock', (builder, [value]: Parameters<IMockBuilder['customMock']>) => {
// here can be any custom logic around `builder`.
builder.mock(TargetService, {
echo: () => value,
});
}); |
feat(MockBuilder): can be extended with custom methods #5417
Wow looks great! Thank you! Btw: Little typo in the documentation inside the example code comment: |
Hi there. If you mean Extension - that's expected. I needed to add a new interface to pass type checks. Please, let me know how it works for you. |
Just tested it, works like expected! I am talking about the typo in the comment |
Ah. I see. Thank you for spotting that. Could you submit a PR? You can edit the page right on GitHub. |
Thank you! I'm going to deploy it later today. |
Discussed in #5404
Originally posted by AE1NS April 6, 2023
I would love to write some reusable extension methods for the MockBuilder. But as MockBuilder is a function and not a class, I'm struggling a bit how to achieve this.
Thought I could use something like this:
.d.ts:
.ts:
test.spec.ts:
The type is recognized correctly but it will not run, as 'MockBuilder.prototype' cant work on a function. Can you tell me if can extend it on another way externally?
The text was updated successfully, but these errors were encountered: