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

Jasmine spy is not overwriting mock implementation

In the ngOnInit method of the class I am testing I call a function of a service which retruns an observable. I have implemented a mock for the service, but I'm trying to use a spy for this exact test case. In my understanding the spy would…
Cines of Lode
  • 119
  • 2
  • 7
4
votes
2 answers

RouterTestingModule not providing provider for Location

I am unit-testing a Component which uses two other components. The other components are created when button is clicked .html
...
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
4
votes
1 answer

Mock static data in a service - angular 2 testing

In my ANGULAR 2 application there is a service; that contains a static reference property. export class MyService { public static OneProperty: ClassA; } And here is a sample implementation of the method I am going to test in my ts file. export…
Prateek
  • 135
  • 3
  • 15
4
votes
3 answers

How to create new event for unit testing in Angular 2?

Hi I am writing unit test case for my angular code. I am trying to update textbox in gridview. Below is my gridview code.
Niranjan
  • 537
  • 2
  • 14
  • 31
4
votes
3 answers

Angular Testing Getting Actual HTTP Response

I'm new to angular unit testing. What I want to do is getting actual results from my API. I checked this documentation but as I understand, I should create mock responses. Here is my code, myService.ts: ... public GetAll = (apiname: string):…
onurcanak
  • 109
  • 1
  • 9
4
votes
1 answer

Angular 5 HttpClient HttpTestingController test error path

I have a service that makes a HTTP get request as follows public get(uri: string, params?: HttpParams): Observable { return params ? this._http.get(uri, { params: params }) : this._http.get(uri) .catch((error: HttpErrorResponse)…
Steve Fitzsimons
  • 3,754
  • 7
  • 27
  • 66
4
votes
1 answer

How to mock ApplicationRef in unit test

I tried to test both methods by Jasmine and Angular 4 but this.applicationRef is always returned an empty object. How to resolve this one? Here is my code: @Injectable() class Dialog { .... getRootViewContainerRef(): ViewContainerRef { const…
Vo Xuan Hoan
  • 121
  • 2
  • 8
4
votes
1 answer

Mocking rest calls in functional e2e protractor tests in angular 4 project

We have a huge project and we have written a lot of test cases to cover a lot of real scenario user behavior in our e2e functional test cases. As the test goes on, it makes a lot of rest calls to complete the test cases. When we cover different…
Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
4
votes
1 answer

How to test component error handling with observable service

Context I'm trying to test a component. In another post I asked about testing the direct function call. Here I'm focusing, using same example component, on the error handling of my component. I want to test, when my service returns an observable…
BlackHoleGalaxy
  • 9,160
  • 17
  • 59
  • 103
4
votes
3 answers

Why do my unit tests pass with Chrome and fail with PhantomJS?

I am using Angular2 final (2.0.2) with angular-cli. I am trying to set it up for running unit tests with PhantomJS. Running the specs with Chrome and karma-chrome-launcher works - all tests pass. Running the same with Phantomjs-prebuilt 2.1.13 and…
Sefi Ninio
  • 427
  • 1
  • 5
  • 12
4
votes
1 answer

Control mouse movement with Protractor

In a particular scenario I have to get full control of my mouse and with the actions() class I'm not able to do it. In my application there is a map and there is a functionality, which uses lasso to select certain portion of the map (say we draw a…
Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
3
votes
0 answers

What is the main usage of MatTestDialogOpener into Angular 15 - Material?

In an Angular developement I am trying to write a test for testing a dialog component. I am quite new with angular testing especially with Material harnessing but I found it very cool. Here is this dialog component code: export class…
3
votes
1 answer

Cypress Component Testing Angular Components spying on private service

I am trying to use Cypress's new Component Testing for Angular. Given the following test beforeEach(() => { mockService.ageRangeName = '0-5'; cy.viewport(1920, 1080); cy.mount(CategoryBuilderComponent, { componentProperties:…
Ken
  • 423
  • 6
  • 13
3
votes
0 answers

ng test --no-watch --watch=false never exiting in gitlab CI

As the title says, I simply want to run ng test from within gitlabCI.. but all the flags I've seen mentioned don't affect anything.. the routine just sits and sits. are these flags enough or do I need to setup jest.config.js ? I see mention of…
Erik
  • 2,782
  • 3
  • 34
  • 64
3
votes
0 answers

Storybook with Angular Couldn't find story matching 'story-title--task-name'

I try to work with Storybook in my Angular project, and I run into a misterious error message, and I have no clue what's happening here. This is my tag.component.stories.ts file: import { CommonModule } from '@angular/common'; import { BrowserModule…
netdjw
  • 5,419
  • 21
  • 88
  • 162