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

a way to skip mocks for a particular definition globally #271

Closed
satanTime opened this issue Jan 5, 2021 · 4 comments · Fixed by #275
Closed

a way to skip mocks for a particular definition globally #271

satanTime opened this issue Jan 5, 2021 · 4 comments · Fixed by #275
Assignees

Comments

@satanTime
Copy link
Member

similar to defaultMock, but should allow to specify what should be always kept / mocked with a way to change it later.

@satanTime satanTime mentioned this issue Jan 10, 2021
@satanTime satanTime self-assigned this Jan 10, 2021
@GerkinDev
Copy link

GerkinDev commented Jan 10, 2021

Would be super useful for global services or classes, like loggers, ErroHandler, Router, ActivatedRoute, etc etc. Totally into it ;)

The only problem I see is that it may be troublesome to have some implicit stuff happening. That's exactly why I never mock a component by providing its declaring module: I prefer this to be explicit, so I MUST keep track of dependencies & update my tests accordingly.

Maybe instanciating sub-factories in the user codebase might be more explicit ? Or config aliases ? Or register providers/tokens, but still need to say "Hey, I want the global mock from that token !" ? This option is, IMO, the one I would prefer. Just provide the token to some method, and poof, my mock is here.

@satanTime
Copy link
Member Author

Hi @GerkinDev,

totally agree, I would disallow all that stuff to keep tests' logic explicit, allowing only default mock behavior for observables.

Might you give me an example of "Hey, I want the global mock from that token !"? Not sure I got it correctly.


Back to the topic.

A guy in gitter gave a different interesting case.

He uses initTestEnvironment to provide a custom TranslateService for all tests.
I would go for ngMocks.defaultMock(TranslateService, ...), but he wants to use the original service, and any MockModule for a module that imports TranslateModule breaks this currently.
Therefore, there should be a solution to exclude TranslateModule and-or TranslateService globally during mocking process for all tests.

A possible way is MockBuilder(...).exclude(TranslateModule), but it should be done in every test.
I think, if we agreed in the team, that a mock TranslateModule would be provided via initTestEnvironment, then okay - we know it, and we could skip . exclude. Just to call once ngMocks.defaultExclude(TranslateModule) in test.ts.

Another case I found is to replace BrowserAnimationsModule with NoopAnimationsModule. Not sure if it makes sense :)

And if we have global exclude and global replace, then maybe we need global keep, for cases like CommonModule with ngIf, but from our own project.

But for global mock, I didn't found a case for it. if I mark a module to be kept globally, but, in the same time, one of its dependency should be still mocked.... not sure.... if you have a real example - I would be glad to discuss it further.


To recap.

The PR has:

  • defaultKeep
  • defaultExclude
  • defaultReplace
  • defaultWipe
  • defaultMock (not only for explicit mocks)

If you think that something should be added / changed / improved - just let me know and let's discuss it.

@satanTime
Copy link
Member Author

v11.4.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants