Questions tagged [angular-test]

Questions about testing Angular code, addressing either specific issues ("why is this test failing"), test flows ("how to test this async call of my component") or test setup ("how do I mock router in this component test"), integration tests ("how to bypass a proxy to my backend in this angular test"), or possibly test-related questions, such as ("Why does this component work and the test is failing?").

Angular test suite, running on top of Jasmine testing framework and Angular test framework.

Questions with angular-test tag about testing Angular code should address test code.

It can be related to specific issues ("why is this test failing"), test flows ("how to test this async call of my component") or test setup ("how do I mock router in this component test"), integration tests ("how to bypass a proxy to my backend in this angular test"), or possibly test-code-related questions, such as ("Why does this component work and the test is failing?").

This tag should not be used for generic and overbroad questions such as "How to test an Angular directive?" or questions that have no relation to Angular test suite ("I am testing my code in browsers, why does it not work?"), but rather test code itself. Rule of thumb can be, "does this question include parts of my *.spec.ts file.

A good angular-test question will, as any good test, include your expected results, your actual results, and your attempts so far, if possible with test code included.

More information on Angular Testing can be found on Angular guide on testing.

718 questions
3
votes
1 answer

Catch console errors while running e2e tests with jasmine and protractor

While running e2e tests (protractor, selenium) I want to catch any console errors and warnings generated by application. I know protractor plugin protractor-console-plugin which catches console and make tests fail but it prints all logs from all…
koral
  • 2,807
  • 3
  • 37
  • 65
3
votes
2 answers

Problems to run any Jest tests with jQuery and jQuery UI

So I have an Open Source library named Angular-Slickgrid which doesn't have tests yet and I'm trying to use Jest with it but it's really hard to get going with it. The library is a wrapper of and old jQuery datagrid library (SlickGrid) which also…
ghiscoding
  • 12,308
  • 6
  • 69
  • 112
3
votes
1 answer

Angular/Karma/Jasmine unit test fails silently when error is thrown

I have Karma/Jasmine unit test for Angular interceptor intercept method which returns rxjs Observable. If there is some error in success callback unit test will appear as passed even though it should fail. Code: method under test: intercept( …
jbojcic
  • 954
  • 1
  • 11
  • 25
3
votes
1 answer

How to test the code logic of ngOnInit in Jasmine and Angular

My component looks for presence of a parameter in the route in ngOnInit. If the parameter is not present, it shows error. I want to test this logic. ngOnInit() { this.id = this.route.snapshot.paramMap.get("id"); if(this.id != null) { ... …
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
3
votes
0 answers

Testing window resize on Angular directive - window dimensions undefined after resize event

I have a directive that changes height of the text within element on window resize: import {Directive, ElementRef, Renderer2, HostListener} from '@angular/core'; @Directive({selector: '[fittext]'}) export class FittextDirective { …
Gitnik
  • 564
  • 7
  • 26
3
votes
2 answers

Is there a way to configure component.spec.ts automatically?

I am adding unit tests in my Angular 7 app. I have 100 components to test, at least, and each one are failing because of the configuration: they need the declaration of each dependencies needed. This is my component.spec.ts where is the…
Fabien
  • 389
  • 2
  • 5
  • 15
3
votes
3 answers

Angular Testing Error: Can't resolve all parameters for Service:

I'm new to testing. I have a service and a spec file that when run I get the following error: Error: Can't resolve all parameters for DashboardService: (?). error properties: Object({ ngSyntaxError: true }) The spec file looks like…
Edgar Quintero
  • 4,223
  • 2
  • 34
  • 37
3
votes
1 answer

How to inject Angular 6+ service variables into app-routing.module.spec.ts and test router redirection

I'm trying to test whether a user is redirected successfully if an authenticated variable is set to true. I've tried injecting my LoginService into a beforeEach and setting the authenticated variable to false. Then, in the unit test, setting that…
James Barrett
  • 2,757
  • 4
  • 25
  • 35
3
votes
1 answer

Jasmine Testing Constructor That has a Promise

I have a service that acts as a data-store. In it's constructor, it attempts to "hydrate" the data-set from the device's storage (using Ionic and it's Storage service): @Injectable() export class SimpleDataStore { private _data:…
EHorodyski
  • 774
  • 1
  • 8
  • 30
3
votes
1 answer

How to test a simple Angular Reactive Form

I have a Component which contains a very simple Reactive Form, with just one input field and one button to be used to submit the form. Here the code of the html template
Picci
  • 16,775
  • 13
  • 70
  • 113
3
votes
1 answer

Angular unit testing - How can I get the child component inside an overlay container

I have a custom component called MatSelectControls that is used like so:
parliament
  • 21,544
  • 38
  • 148
  • 238
3
votes
0 answers

Is it possible to use an existing build for Karma testing in Angular

I would like ng test to use the build that I have created with ng build --prod. What configuration has to be made to enable this behaviour? I want to be sure that I test exactly what goes to production rather than a separate build. (Let's assume I…
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
3
votes
1 answer

Angular + Redux Jasmine testing "Cannot read property 'dispatch' of undefined" randomly thrown in runs

I have an integration test that is dependant on 2 services which I am providing to the testbed with stubs. When I am testing a function updateCategory() in the subscribe block of this category I have a function ngRedux.dispatch({type:…
SebastianG
  • 8,563
  • 8
  • 47
  • 111
3
votes
2 answers

Angular component test error: TypeError Cannot read property 'subscribe' of undefined

I have a simple unit test that is done using karma/jasmine on an Angular 6 component. From the pluralsight course and documentation I have gathered it appears I am mocking my service correctly that my component requires, but when the method is…
Austin Born
  • 1,339
  • 2
  • 11
  • 22
3
votes
1 answer

Angular 2+ Service Testing. Service imports are undefined at ng test runtime

I'm new to Angular and attempting to test an Angular 6 service which utilizes handlebars.js to build client-side html templates, based on input JSON / data. The service produces the desired result in development, but I'm unable to build a test…
Andrew
  • 1,406
  • 1
  • 15
  • 23