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

Bug: ngMocks.globalReplace() doesn't work #6402

Closed
mfrieling opened this issue Aug 2, 2023 · 4 comments · Fixed by #7439
Closed

Bug: ngMocks.globalReplace() doesn't work #6402

mfrieling opened this issue Aug 2, 2023 · 4 comments · Fixed by #7439
Assignees
Labels
bug Something isn't working released v14.12.0

Comments

@mfrieling
Copy link

Description of the bug

Using Jest, ngMocks.globalReplace() in test-setup.ts doesn't work as expected, compared to MockBuilder(MyComponent).replace(HttpClientModule, HttpClientTestingModule).

If you have a lot of tests (tested components, services etc.) which depend on HttpClientModule, instead of repeating MockBuilder(MyComponent).replace(HttpClientModule, HttpClientTestingModule) in every spec you instead want to just write MockBuilder(MyComponent) and put a ngMocks.globalReplace(HttpClientModule, HttpClientTestingModule) into test-setup.ts.

But this doesn't work as expected.

An example of the bug

test-setup.ts:

// Jest configuration skipped here for simplicity.

ngMocks.autoSpy('jest')

ngMocks.globalKeep(ApplicationModule, true)
ngMocks.globalKeep(CommonModule, true)
ngMocks.globalKeep(BrowserModule, true)
ngMocks.globalKeep(RouterModule, true)

//ngMocks.globalReplace(HttpClientModule, HttpClientTestingModule)

app.component.spec.ts:

describe('AppComponent', () => {
  beforeEach(async () =>
    MockBuilder(AppComponent)
      .replace(HttpClientModule, HttpClientTestingModule)
      .keep(RouterTestingModule.withRoutes([])),
  )

  test('should create the app', () => {
    const fixture = MockRender(AppComponent)

    expect(fixture.point.componentInstance).toBeDefined()
  })
})

Expected vs actual behavior

This test passes. If I now comment out the .replace() line in the spec file and uncomment the corresponding line ngMocks.globalReplace() the test should still pass.

Instead I get the following error:

● AppComponent › should create the app

NullInjectorError: R3InjectorError(DynamicTestModule)[WorkflowManagerService -> WorkflowManagerApiService -> AssetLoaderService -> _HttpClient -> _HttpClient]:
NullInjectorError: No provider for _HttpClient!

@satanTime
Copy link
Member

Hi @mfrieling,

thanks for reporting. The issue will be fixed with the next release.

satanTime added a commit that referenced this issue Nov 15, 2023
fix(MockBuilder): respects global rules as they would be chain calls #6402
@satanTime
Copy link
Member

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

@cluxig
Copy link

cluxig commented Mar 7, 2024

Still the same issue for version 14.12.1. I tried ngMocks.globalReplace(HttpClientModule, HttpClientTestingModule); and ngMocks.globalKeep(HttpClientTestingModule, true). Both resulting in HttpClient not provided.

Whereas using .replace(HttpClientModule, HttpClientTestingModule) or .keep(HttpClientTestingModule) directly on MockBuilder works as expected.

@Eagl9Eye
Copy link

@satanTime should a new issue be opened?

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.12.0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants