Angular Component Testing: provide access to DI
See original GitHub issueWhat 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:
- Created a year ago
- Comments:10 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 thecypress/angular
lib (same one) that gets shipped with the binary.Quantarius (cool name by the way 👍), it could be that the information of how many items are in
Cart
is not shown inMyComponent
. 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.