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

Angular Component Testing: providing of transitive dependencies fails #24047

Closed
rainerhahnekamp opened this issue Sep 29, 2022 · 4 comments · Fixed by #24394
Closed

Angular Component Testing: providing of transitive dependencies fails #24047

rainerhahnekamp opened this issue Sep 29, 2022 · 4 comments · Fixed by #24394

Comments

@rainerhahnekamp
Copy link
Contributor

Current behavior

If I use a service in a component that has a dependency to HttpClient and I provide the HttpClient, the mounting fails.

The error message is

NullInjectorError
...
NullInjectorError: No provider for HttpClient!

If the component under test depends directly on the HttpClient and I provide the HttpClient, the mounting works.

Desired behavior

Providing of transitive dependencies should work.

Test code to reproduce

https://github.com/rainerhahnekamp/cypress-component-angular

@Component({
  template: '',
})
class NonTransitiveServicesComponent {
  constructor(private httpClient: HttpClient) {}
}

@Injectable({ providedIn: 'root' })
class LoaderService {
  constructor(private httpClient: HttpClient) {}
}

@Component({
  template: '',
})
class TransitiveServicesComponent {
  constructor(private loaderService: LoaderService) {}
}

describe('Component Testing', () => {
  it('succeeds to mount', () => {
    cy.mount(NonTransitiveServicesComponent, {
      providers: [{ provide: HttpClient, useValue: null }],
    });
  });

  it('fails to mount', () => {
    cy.mount(TransitiveServicesComponent, {
      providers: [{ provide: HttpClient, useValue: null }],
    });
  });
});

Cypress Version

10.9.0

Node version

16.15.0

Operating System

Windows 11 Professional

Debug Logs

No response

Other

No response

@astone123 astone123 self-assigned this Oct 3, 2022
@astone123
Copy link
Contributor

@rainerhahnekamp thanks for the detailed issue. This is a known issue with Angular CT, see the discussion on #23427. For the time being, you can work around this by doing something similar to what is mentioned in this comment.

@astone123 astone123 removed their assignment Oct 3, 2022
@yjaaidi
Copy link

yjaaidi commented Oct 7, 2022

I totally agree with Rainer!

I think that I mislead us with this issue jordanpowell88/angular-ct#37

We actually need both: providers and componentProviders

This will be a breaking change so it might be better to do it while we're in the early stage.

What do you think?
cc. @astone123 @jordanpowell88

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review stage: review and removed stage: needs review The PR code is done & tested, needs review stage: review labels Oct 25, 2022
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 31, 2022

The code for this is done in cypress-io/cypress#24394, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 10, 2022

Released in 11.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v11.0.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Nov 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants