question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular Component Testing: provide access to DI

See original GitHub issue

What would you like?

Angular’s CT mount function should return an inject function that allows DI.

e.g.:

const {inject} = cy.mount(...);
const service = inject(MyService);

where inject is nothing more than a wrapper of TestBed.inject just to hide the implementation detail which is TesBed. we might also provide a cy.inject command, that does something like this to provide a better DX:

cy.then(() => TestBed.inject(...))

What do you think?

cc. @jordanpowell88

Why is this needed?

As an advocate for fakes (vs. stubs/spies/mocks), I often end up with stateful services that I might want to access & check manually afterward in my test. But of course, there are way many other use cases like arranging the test etc…

cy.mount(MyComponent, {providers: [{provide: Cart, useClass: CartFake}]})

cy.contains('Buy').click()

cy.inject(CartFake).invoke('getItems').should('have.length', 1);

or

const { inject } = cy.mount(MyComponent, {providers: [{provide: Cart, useClass: CartFake}]})

cy.contains('Buy').click()

cy.then(() => inject(CartFake).getItems()).should('have.length', 1);

cc. @jordanpowell88

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jordanpowell88commented, Dec 6, 2022

@rainerhahnekamp Feel free to work on a solution you feel like improves the API. Also for clarification we release @cypress/angular as a separate lib on npm for anyone who wants to eject or do something custom with it as apposed to the cypress/angular lib (same one) that gets shipped with the binary.

1reaction
rainerhahnekampcommented, Nov 16, 2022

Quantarius (cool name by the way 👍), it could be that the information of how many items are in Cart is not shown in MyComponent. Then the only way to find out is to check the service directly.

But I think Younes just made this example primarily to demonstrate the requirement of the inject command.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component testing scenarios - Angular
The ComponentFixtureAutoDetect service responds to asynchronous activities such as promise resolution, timers, and DOM events. But a direct, synchronous update ...
Read more >
Testing Components depending on Services - Testing Angular
Testing Components depending on Services · Service dependency integration test · Faking Service dependencies · Fake Service with minimal logic.
Read more >
Angular Component Testing: A Detailed How-To With Examples
Angular component testing means to check the quality and performance of your components. Angular component testing can be done manually by ...
Read more >
Angular unit testing tutorial with examples - LogRocket Blog
The properties of the component can be accessed upon instantiation, so the rendered component detects the new changes when a value is passed ......
Read more >
angular - How to access private service which is provided by ...
I guess this makes sense, but I cannot find a solution for this, because for my tests I need access to that service...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found