-
Notifications
You must be signed in to change notification settings - Fork 312
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]: Exclude ɵfac ɵprov properties from auto mock #2908
Comments
Would it work if you do like this
or like this
In general, |
Yes, both options work. But I would like to somehow unify the behavior when the service is mocked from the project, and from the library. If you mock the service from the project, then everything works fine, but for the service from the library, you have to write some extra stuff. This increases the learning curve of new employees. Therefore, if jest does not offer this capability and it is not applicable at the jest-preset-angular level, I will attempt to submit a feature request to jest. |
You can also create testing utilities which provides the reusable ways to mock things, document and that would also help to instruct new employees too :) |
No, I can't. That would be too easy. The fact is that a call to the jest.mock function is parsed statically. And there is no way to pass dynamic parameters there, such as utility functions or constants from outside. The only way to duplicate this factory function in each test in each jest.mock call. |
Currently, In Vitest, they have an API like this https://vitest.dev/api/vi#vi-hoisted which I think can allow to pass things from outside which Jest doesn't have. Maybe you can look at Vitest API to come up with a nice proposal for Jest since Jest and Vitest are kind of identical |
If It should be something like: jest.onGenerateMock((moduleName, moduleMock) => {
// some modifications for moduleMock (as described in example)
return moduleMock;
}); |
Ye, it should be possible to add to the list of hoisting methods |
Pull request is merged. Waiting for release... |
🚀 Feature Proposal
Exclude
ɵfac
ɵprov
properties from jest auto mockMotivation
Currently, I'm trying to test a component that depends on an external Angular library. This library provides a
SomeService
that is provided in root.Here is my component:
Here is my test:
Currently I receive error:
Cannot read property 'someMethod' of undefined
.This happens because of jest auto mocks, mocking ɵfac and ɵprov as well. Is it possible to exclude them?
Example
I've done some research. Here is my working patch:
However, it would not be beneficial to repeat it in each test and mock. Perhaps it could be placed there?
The text was updated successfully, but these errors were encountered: