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

ngMocks.globalReplace doesn't seem to replace in standalone component #3161

Closed
DW8Reaper opened this issue Jul 20, 2022 · 6 comments · Fixed by #3231
Closed

ngMocks.globalReplace doesn't seem to replace in standalone component #3161

DW8Reaper opened this issue Jul 20, 2022 · 6 comments · Fixed by #3231
Assignees
Labels
bug Something isn't working released v14.1.1

Comments

@DW8Reaper
Copy link

Description

I apologize that I don't have an easy reproduction added here but the basic issue is the following, when using standalone components (testing with NgMocks v14.1.0) any ngMocks.globalReplace calls don't seem to apply to those components.

So in our codebase we do a global setup that calls something like this:

ngMocks.globalReplace(TranslateModule, MockTranslateModule);

Then in our code we have a standalone component:

@Component({
  select: 'a-component',
  template: '',
  standalong: true,
  imports: [TranslateModule]
})

and the test setup for the standalone component

  beforeEach(() => {
    return MockBuilder(TheStandaloneComponent);
  });

However the test don't work because when the tests run it uses vanilla/automatic module mocks for the TranslateModule instead of using my global replacement.

Workaround

I can work around it by adding a replace directly in the test like this then it works again:

  beforeEach(() => {
    return MockBuilder(TheStandaloneComponent).replace(TranslateModule, MockTranslateModule);
  });
@satanTime
Copy link
Member

Hi @DW8Reaper, thanks for the report. I'll take a look where the problem is.

@DW8Reaper
Copy link
Author

Thanks @satanTime I don't know if this helps or further confuses the issue but I have found anther funky thing might be related/causing this. If we have code like this:

mockBuilder(SomeComponent, SomeModule)
      .mock(SomeService, aCustomMockObjectWeMake)

but the module SomeModule does not define or export SomeService (in fact no module in the test exports it). Previously it seems when we call .mock(SomeService, aCustomMockObjectWeMake) it would also automatically define a provider for that service if it didn't exist.

I can fix it by doing this which I didn't need in ngMocks v13.0.3

mockBuilder(SomeComponent, SomeModule)
      .mock(SomeService, aCustomMockObjectWeMake, {export: true})

So possibly there is a change to how that mock method works with services that aren't directly imported in any module in the test. I don't know if this is possibly related or causing the standalone component issue as well.

I realize that maybe we should not be using ngMocks.mock with a service that isn't registered in any modules

@satanTime satanTime self-assigned this Jul 31, 2022
@satanTime satanTime added the bug Something isn't working label Jul 31, 2022
satanTime added a commit to satanTime/ng-mocks that referenced this issue Jul 31, 2022
…encies help-me-mom#3161

MockBuilder will process dependencies of standalone declarations with respect of the next configuration:

- ngMocks.globalKeep
- ngMocks.globalMock
- ngMocks.globalReplace
- ngMocks.globalExclude
satanTime added a commit that referenced this issue Jul 31, 2022
fix(MockBuilder): respects global configuration for standalone dependencies #3161
@satanTime
Copy link
Member

Hi @DW8Reaper, I hope this finds you well.

the fix is here.

Regarding MockBuilder(SomeComponent, SomeModule).mock(SomeService, aCustomMockObjectWeMake), this is a breaking change in ng-mocks@14.x. if you specify 2 parameters in MockBuilder, then chain calls expect all declarations to be a part of those 2 parameters: https://ng-mocks.sudo.eu/migrations#from-13-to-14

Basically, you need to add SomeService to the 2nd parameter for mocks:

MockBuilder(SomeComponent, [SomeModule, SomeService]).mock(SomeService, aCustomMockObjectWeMake);

Then, it's clear that it's an external dependency and neither SomeComponent nor SomeModule provides it.

@satanTime
Copy link
Member

v14.1.1 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.

@DW8Reaper
Copy link
Author

Thanks @satanTime I missed that breaking change from 13 to 14 so that's my bad on that one, thanks for fixing the global mocks that looks like the change you made in v14.1.1. I'll give it a shot and update our mocks based on the required migrations and see how it works

@satanTime
Copy link
Member

Sounds good! Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released v14.1.1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants