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
4
votes
1 answer

Property 'configureTestingModule' doesn't exist on type TestBedStatic

I have updated my Angular 12 application to Angular 14 using ng update. The build was successful and can see my application running perfectly fine but my tests are failing. One of the errors I am getting is this: Property 'configureTestingModule'…
Aayushi Jain
  • 2,861
  • 2
  • 29
  • 36
4
votes
2 answers

Angular: 12 - Cannot access 'AppComponent' before initialization when doing "ng test"

When I'm trying to run "ng test", I just getting like the below: Error message in UI by Karma v 6.3.4: 0 specs, 0 failures, randomized with seed 98146 Error during loading: Uncaught ReferenceError: Cannot access 'AppComponent' before initialization…
4
votes
1 answer

Why do I get the NG0303 or NG0300 errors? (Angular Testing, Karma Jasmine)

I have several components (about,service,dashboard, etc) in which a header component is added. The application is running fine. However, I am getting errors when testing. import { Component } from '@angular/core'; import { ComponentFixture, TestBed,…
Doncarlito87
  • 359
  • 1
  • 8
  • 25
4
votes
1 answer

Testing angular component form for pristine is not working

I have a form that has an input and I have a button that needs to be enabled only when the form data was changed. I am using a pristine check and it all works functionally in the browser but I am having trouble to test it. No matter what I do the…
cp5
  • 1,087
  • 6
  • 26
  • 58
4
votes
0 answers

How to get the value of a select when using ngValue in Angular unit tests

In my Angular app I have a
4
votes
1 answer

Testing Angular interceptors response with Jest and Spectator

I'm trying to test an Interceptor that modifies the response from an HTTP Request. Here's my current code: @Injectable() export class ResponseCamelCaseInterceptor implements HttpInterceptor { intercept( httpRequest: HttpRequest
dev_054
  • 3,448
  • 8
  • 29
  • 56
4
votes
1 answer

Testing UpgradeComponent throws NullInjectorError: No provider for $injector! error

I have a angularjs component that I am upgrading: import { Directive, ElementRef, Injector, Output, EventEmitter } from '@angular/core'; import { UpgradeComponent } from '@angular/upgrade/static'; @Directive({ selector: 'up' }) export class…
lostintranslation
  • 23,756
  • 50
  • 159
  • 262
4
votes
1 answer

Angular Unit Testing with Spectator - Unexpected value 'FormBuilder' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation

I want to use spectator to assist me with my unit tests as I have read great things about it. However I'm not sure about how to resolve a problem with my tests. In my component template I have a formGroup like so...
NewToAngular
  • 975
  • 2
  • 13
  • 23
4
votes
1 answer

Angular testing component with mocked structural directive fails

I am trying to mock structural directive in a component test but Im getting error. Following test is failing with a message: Property binding appSome not used by any directive on an embedded template. Make sure that the property name is spelled…
Felix
  • 3,999
  • 3
  • 42
  • 66
4
votes
1 answer

Angular's FormControl method markAsTouched not working in a test case

I have this function which checks that the password and confirmPassword fields have the same value. If not, the form is marked a invalid confirmPasswordValidator(passwordGroupForm: AbstractControl){ let password =…
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
4
votes
2 answers

NgRx 8 Test provideMockStore - setState for State Slice

I have a smart component test for my NgRx implementation that looks something like this: describe( 'Component', () => { let store: MockStore; beforeEach( async( () => { TestBed.configureTestingModule( { /* ... */ …
Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137
4
votes
1 answer

Unit Test for function with mat paginator method firstPage()

I have a function I am trying to test. I am using the matPaginator.firstPage() method inside of it. However any test I write for this function I can an error of "Cannot read property 'firstPage' of undefined" I cannot find anything on how to mock…
Brian Stanley
  • 2,078
  • 5
  • 24
  • 43
4
votes
1 answer

Test adding class to the in angular 7

I have this root AppComponent that listenens for a change on a service, and then adds or removes a CSS class on the document.body import { Component, OnInit, Renderer2 } from '@angular/core'; import { SideMenuService } from…
Chris Barr
  • 29,851
  • 23
  • 95
  • 135
4
votes
2 answers

waitForAngularRequestsToFinish() -- What is for?

java-selenium Hi, I am recently working with selenium-java for project automation. I found that in a lot of codes are using this method. waitForAngularRequestsToFinish(); I don't know exactly why is this in the main function. And in the next…
4
votes
4 answers

Error: ExpressionChangedAfterItHasBeenCheckedError when an Angular Component is tested with synchronous Observables

I have a simple component which uses a service returning an Observable. Such Observable is used to control the creation of an html element via *ngIf. Here the code @Component({ selector: 'hello', template: `

Picci
  • 16,775
  • 13
  • 70
  • 113