-
Notifications
You must be signed in to change notification settings - Fork 237
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
[unbound-method] Allow custom assert function names #1033
Comments
I'm really on the fence about this one because this is actually dependent on more than just the name of the function, and I'm pretty sure the example you've given could be better structured using the mock types, e.g. something like this should work:
We want to be careful with this rule because the more we customize it the harder it becomes to maintain with the base rule, and I'm not sure if there's enough value here vs the alternatives to be worth it. |
Thanks for the response. I understand the concern. Unfortunately, I couldn't get your suggestion to work, because the property const expectCallbackToReturn(
mockedFunction: jest.MockedFunction<Class['method']>,
expectedReturnValue: string
) => {
const [callback] = mockedFunction.mock.calls[0];
expect(callback()).toBe(expectedReturnValue);
}; Though, this doesn't solve the |
or using the types from
|
I'm going to close this since I think the better option is to restructure the types per my comments and examples above, so there's not a strong reason to extend the rule further. |
Coming back reeaally late here... Many thanks for your help! ❤️ Finally, I used the following approach based on your suggestions, only slightly modified: const expectCallbackToReturn = (
mock: jest.MockedFunction<MyClass['myMethod']>['mock'],
expectedReturnValue: string
) => {
const [callback] = mock.calls[0];
expect(callback()).toBe(expectedReturnValue);
};
const mockedMyClass: jest.Mocked<MyClass> = { myMethod: jest.fn() };
expectCallbackToReturn(
mockedMyClass.myMethod.mock,
'expected-value'
); The reason why I didn't use This felt quite cumbersome and weird compared to all the other mocks. (Also, the call signature would have to be Anyways, many thanks again! I agree that using proper types is the better option.👍 |
Glad I could help!
Yeah that was the function signature used over at |
Thanks for the plugin! We're happily using it in multiple TypeScript projects.
Though, we recently came across the issue that the
unbound-method
rule also complains if we're passing a method into a custom expect function like this one:We wrote this custom expect function to reuse it across a test file, but writing a custom matcher seems like overhead because we need it only in a single file.
A possible solution could be if we could specify custom
assertFunctionNames
like forexpect-expect
. Then, we would simply allowexpect*
.The text was updated successfully, but these errors were encountered: