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
17
votes
2 answers

How to mock ControlContainer in angular unit test?

How can I mock a ControlContainer instance so that I can test my component? I have a child component that injects a ControlContainer into the constructor, so its usage is and the component…
Neil Stevens
  • 3,534
  • 6
  • 42
  • 71
17
votes
2 answers

Submitting form with enter key in Angular unit test

I'm writing a test for an Angular 4 component that is a login form. The form can be submitted by clicking on the "submit" button or by hitting enter in any input fields. This behaviour is dictated by the Angular form directive. I can write a test…
13
votes
5 answers

DEPRECATED: DI is instantiating a token "MockLocationStrategy" that inherits its @Injectable decorator but does not provide one itself

While testing my Angular application I am getting deprecation warnings 'DEPRECATED: DI is instantiating a token "MockLocationStrategy" that inherits its @Injectable decorator but does not provide one itself. This will become an error in a future…
Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
13
votes
5 answers

How to make ng test to fail on warnings

I've just finished fixing several warnings on my unit/component tests of my Angular app, that run using ng test (with Karma/Jasmine). This process can be quite time-consuming as it is not always obvious to spot which test case(s) is actually causing…
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
13
votes
5 answers

rxjs how to expect an observable to throw error

In my TypeScript app I have a method that return an rxjs Observable which, in a certain case, can return throwError: import { throwError } from 'rxjs'; // ... getSomeData(inputValue): Observable { if (!inputValue) { return…
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
12
votes
6 answers

How to mock HTTP Request in Angular?

I have checked a lot of articles and answers but I don't seem to find the right way to mock HTTP Requests for my methods. I want to test my frontend application independently from the backend. Here is the type of methods I have: private…
12
votes
6 answers

AsyncTestZoneSpec is needed for the async - Angular

The application was built on Angular v4 and was gradually updated with every release up until now. Currently we're on Angular v7 and finally the CEO has agreed to write unit test while it wasn't the case before. I just created a simple spec to just…
Clean Code
  • 291
  • 1
  • 2
  • 9
12
votes
2 answers

How to write the unit testing for the file upload method in the Angular 7 (or 2+)

I'm trying to write the unit testing for the file upload method in the angular 7. Getting the below error in the testing window. I'm new for angular unit testing. Could someone help, How to add mock files to get the full code coverage? TypeError:…
Helphin
  • 143
  • 1
  • 1
  • 10
11
votes
2 answers

Angular11 test: ReferenceError: ResizeObserver is not defined

I used ResizeObserver in my component and its works fine. But get such error when running ut: ReferenceError: ResizeObserver is not defined 133 | }); 134 | > 135 | this.resizeObserver = new…
FakeAlcohol
  • 860
  • 7
  • 28
11
votes
4 answers

ReferenceError: customElements is not defined

I'm using @angular@9.0.7, @ngneat/spectator@5.3.1 (with Jest), Inputmask@5.0.3 in a project and everything works on application when I run ng serve or even ng build, but it fails when I try to run a test suite for a @Pipe that uses…
dev_054
  • 3,448
  • 8
  • 29
  • 56
11
votes
2 answers

Angular 9 TestBed.inject & Provider Overrides

When using provider overrides what is the alternative of the following now that TestBed.get has been deprecated in Angular 9 TestBed.configureTestingModule({ providers: [{ provide: MyClass, useClass: MyStub}] }); const obj : MyStub =…
C. Rodwell
  • 113
  • 1
  • 4
10
votes
2 answers

How to test that an Observable does not emit in Angular Component which contains asynchronous logic

Let's say I have an Angular Component which defines an Observable myObs$ as one of its properties. In one test, given certain conditions, I want to test that myObs$ does not notify. In the logic there are some delays involved, so the test must be…
Picci
  • 16,775
  • 13
  • 70
  • 113
10
votes
2 answers

Angular testing - ngBootstraps typeahead

I am currently using a autocomplete mechanism (typeahead) of ngBootstrap. Now I want to unit test if a method is called on every sequence of an input event. The error on my test case is currently: Cannot read property 'pipe' of…
MarcoLe
  • 2,309
  • 6
  • 36
  • 74
10
votes
2 answers

Angular Testing for Angular-Material on Mat-Menu

I'm trying to write a test for my mat-menu in my application's toolbar. When I call button.click() in my test, I get a Cannot read property 'templateRef' of undefined error in the console. As all works find in the browser, I believe this is to do…
9
votes
2 answers

Angular Test error -- Can't bind to 'items' since it isn't a known property of 'app-dropdown'

Just want to say that I recognize that there are many SO posts related to "Can't bind to X since it isn't a known property of Y" errors. I've looked at a ton of them, and have found a number of answers which solve the specific problems, but which…
Sasha
  • 6,224
  • 10
  • 55
  • 102
1
2
3
47 48