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: Unhelpful error message when MockBuilder .keep() or mock() is used instead of .provide() #10020

Open
ajlouie opened this issue Sep 13, 2024 · 0 comments · May be fixed by #10021
Open

Bug: Unhelpful error message when MockBuilder .keep() or mock() is used instead of .provide() #10020

ajlouie opened this issue Sep 13, 2024 · 0 comments · May be fixed by #10021
Labels
bug Something isn't working

Comments

@ajlouie
Copy link

ajlouie commented Sep 13, 2024

Description of the bug

When accidentally mocking a provider dependency with MockBuilder using .keep() or .mock() instead of .provide(), I get the following mysterious and uninformative error message:

Error: MockBuilder has found a missing dependency: Object. It means no module provides it. Please, use the "export" flag if you want to add it explicitly. https://ng-mocks.sudo.eu/api/MockBuilder#export-flag

An example of the bug

For example, try providing ActivatedRoute with the .keep():

  beforeEach(() => {
    return MockBuilder(TargetComponent, TargetModule).keep({
      provide: ActivatedRoute,
      useValue: {
        queryParams: of({ query: '', keys: [] }),
        queryParamMap: of({ query: '', keys: [] }),
      },
    });
  });

The test fails with the error: MockBuilder has found a missing dependency: Object.

Changing .keep() to .provide() fixes the problem:

  beforeEach(() => {
    return MockBuilder(TargetComponent, TargetModule).provide({
      provide: ActivatedRoute,
      useValue: {
        queryParams: of({ query: '', keys: [] }),
        queryParamMap: of({ query: '', keys: [] }),
      },
    });
  });

Link: https://stackblitz.com/edit/github-hzu37y?file=src%2Ftest.spec.ts

Expected vs actual behavior

Expected behavior

Test fails to run with a helpful error message, such as:

Error: A provider object has been incorrectly passed to the MockerBuilder.keep() method. Did you mean to use MockerBuilder.provide()?

Actual behavior

Test fails to run with the following error message, which is not helpful (e.g., adding keep(Object, {export: true} still fails).

Error: MockBuilder has found a missing dependency: Object. It means no module provides it. Please, use the "export" flag if you want to add it explicitly. https://ng-mocks.sudo.eu/api/MockBuilder#export-flag
@ajlouie ajlouie added the bug Something isn't working label Sep 13, 2024
@ajlouie ajlouie linked a pull request Sep 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant