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
3 answers

Testing NgRx 6 Effects

I am trying to test a ngrx effects in Angular 6 project, I always get error: Expected $[0].notification.kind = 'C' to equal 'N'. Expected $[0].notification.hasValue = false to equal true. I tried this post…
Laurent B
  • 215
  • 1
  • 11
3
votes
2 answers

Angular testing - observable pipe is not a function

I want to write a unit test for a photo-upload-mehtod. But I get the Failed: this.task.snapshotChanges(...).pipe is not a function TypeError: this.task.snapshotChanges(...).pipe is not a function Error. For the sake of simplicity of this…
MarcoLe
  • 2,309
  • 6
  • 36
  • 74
3
votes
1 answer

Angular - Testing Routerguard

I'm currently struggling on unit testing my canActivate() method from my Routerguard Service. The Service looks as follows: import {Injectable} from '@angular/core'; import {ActivatedRouteSnapshot, CanActivate, Router} from '@angular/router'; import…
MarcoLe
  • 2,309
  • 6
  • 36
  • 74
3
votes
1 answer

Angular 2 - Test component with ChangeDetectorRef dependency

i'm trying to testing a component with ChangeDetectorRef constructor(private cdRef: ChangeDetectorRef) {} And this is the spec file import {RTLateralMenuComponent} from "./RTLateralMenu.component"; describe('RTLateralMenuComponent', () => { let…
user8893305
3
votes
1 answer

How should I test error message appears in Angular 6

I am testing the validation of a component, and I can test if it is falsy or not, but I don't know how to test if the error message appears. Here is the HTML:
Juliatzin
  • 18,455
  • 40
  • 166
  • 325
3
votes
1 answer

Angular Unit Testing - Mock async calls of injected services with TestBed

I need to write unit tests for the following DataService , @Injectable() export class DataService { constructor(private config: ConfigService, private http: HttpClient) { } ..... someMethod(){ let apiUrl =…
JPS
  • 2,730
  • 5
  • 32
  • 54
3
votes
1 answer

Testing Angular Directive with async call in onInit

I have an Angular Directive, which makes call for authorization service in it's onInit method. It looks like that: @Directive({ selector: '[checkRights]' }) export class RightsDirective implements OnInit{ @Input('checkRights') expectedRights:…
KKeff
  • 348
  • 3
  • 12
3
votes
1 answer

angular 5 test static method of component

I have a static method on my Component class that I am testing, my question is how it is possible to access that method within my spec testing file? So far I can access a component instance with: let fixture =…
Rodriguez David
  • 541
  • 9
  • 25
3
votes
1 answer

Expected object to be a kind of ScalarObservable, but was Observable

In my Angular application, I was using Observables in the following way: getItem(id): Observable { return this.myApi.myMethod(...); // returns an Observable } and I unit tested it: it('getItem(...) should correctly do its job', …
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
3
votes
1 answer

Error: clientWidth is not declared configurable in http://localhost:9877node_modules/jasmine-core/lib/jasmine-core/jasmine.js (line 4410)

I am trying to mock ElementRef properties while unit testing my angular component and it give me the below error Error: clientWidth is not declared configurable in http://localhost:9877node_modules/jasmine-core/lib/jasmine-core/jasmine.js (line…
Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
2
votes
1 answer

Angular 14 + Jest - typescript_1.default.canHaveDecorators is not a function

After configuring Jest with Angular 14, I'm getting this error: Test suite failed to run TypeError: typescript_1.default.canHaveDecorators is not a function at TypeScriptReflectionHost.getDecoratorsOfDeclaration…
2
votes
1 answer

How to test private field in subscribe of dispatcher in Angular?

I have file: @Component({ selector: 'page', templateUrl: './page.html', styleUrls: ['./page.scss'] }) export class PageComponent implements OnInit, OnDestroy { private lts: Igst; constructor( private router: Router, private route:…
tryingHard
  • 1,794
  • 4
  • 35
  • 74
2
votes
1 answer

TypeError: this.restoreDialogRef.afterClosed is not a function

I am getting the following error Should open confirmation dialog ConfigurationRestoreComponent TypeError: restoreDialogRef.afterClosed is not a function at ConfigurationRestoreComponent.openConfirmationDialog (main.js:16303:22) at ts file looks…
user19592921
2
votes
1 answer

Angular testing Expected spy navigateByUrl to have been called 0 times. It was called 1 times

I have a strange behavior on testing Angular RxJS based funtions with Karma/Jasmine. I have this test: describe('LoginComponent', () => { let fixture: ComponentFixture; let component: LoginComponent; let debugElement:…
netdjw
  • 5,419
  • 21
  • 88
  • 162
2
votes
1 answer

Call created spy before change detection

I want to call created spy method before change detection is triggered. here is my mocked class. const stubStudentService: Pick = { getAllStudents: jasmine .createSpy('getAllStudents') …
NicoleZ
  • 1,485
  • 3
  • 22
  • 43