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

Angular jasmine test not able to trigger Observable created with fromEvent rxjs operator

I have a simple case. The standard AppComponent of an Angular app contains a ChildComponent which is defined in its own module ChildModule. The template of ChildComponent is very simple
Picci
  • 16,775
  • 13
  • 70
  • 113
9
votes
1 answer

Angular How to test @HostListener

I have the following directive. When applied to an input element, it checks for characters and calls preventDefault when the character is forbidden: @Directive({ selector: '[cdtPreventInput]' }) export class PreventInputDirective implements…
Sam
  • 13,934
  • 26
  • 108
  • 194
9
votes
2 answers

How to reuse all imports in Angular test files

Lets say I have a simple module AppModule which has many imports, declarations and providers. Now I want to write a test for a component ListComponent which is located in this module's declaration list. ListComponent itself uses many, (but not…
8
votes
3 answers

Angular cli showing error "An unhandled exception occurred: No projects support the 'test' target." when running ng test or ng serve command

I have a basic angular application , which was working fine. The ng serve and ng test commands were working fine. Recently as part of build automation for different environments , I introduced some configuration changes to allow different…
Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
8
votes
1 answer

Setting karma jasmine DEFAULT_TIMEOUT_INTERVAL globally

In my Angular app, I use Karma and Jasmine to run my unit tests. I'd like to change the default timeout interval for async tests, from the 5 seconds default to, let's say, 10 seconds. I see that you can use jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000…
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
8
votes
6 answers

ng test fails in angular universal Error "Incomplete: No specs found, , randomized with seed 48751"

convert angular 7 project into angular universal while running "ng test" command giving error as "Incomplete: No specs found, , randomized with seed 48751". Tried different ways mention over stackoverflow but nothing work for me. ERROR in…
8
votes
1 answer

Async beforeEach finished before/after each test unit(it)

I am using Angular 6 and I have one concern regarding every test which I have seen so far. Here's a simple test of simple component which is generated by cli. describe('CompComponent', () => { let component: CompComponent; let fixture:…
Brk
  • 1,247
  • 2
  • 23
  • 55
8
votes
1 answer

When does Jasmine .toHaveBeenCalledWith match the parameters?

I have the following angular service and its jasmine test. The test calls f1() and spies on f2(). The function f2 takes variable v2 and modifies it (sets field a to 3). The function f2 should be called with v2 (as declared in f1) but my test fails…
Harsh Shah
  • 83
  • 1
  • 1
  • 4
8
votes
1 answer

Testing Http Service making multiple calls and returning observable without mapping the responses

I have a data service which fetches the data from server and makes multiple requests which then return an array of observables. I want to test the data. What i tried doing is in the mockrespone I sent array which contains two observables i dont know…
SONGSTER
  • 585
  • 3
  • 11
  • 28
7
votes
0 answers

Angular Storybook Subscribe Output

How to subscribe to the component Output in Storybook for Angular? For example, I have such component with its Output actions: import { Component, Input, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-task', template:…
ame
  • 773
  • 9
  • 21
7
votes
1 answer

Angular Testing - expect not seen if encompassed within whenStable

I have a component with tabs. I need to navigate to a tab and then test the viewable inputs, etc. I can see from the console log that the actions that I am taking are calling the function that I am spying on using the process below. I can also see…
Joshua Foxworth
  • 1,236
  • 1
  • 22
  • 50
7
votes
2 answers

Unable to test Angular Material Select Component using CDK Test Harness

I am trying to write some tests for a component that uses Angular Material Components. I read about the CDK Test Harness https://material.angular.io/guide/using-component-harnesses and I want to fetch the count of options in the Mat Select component…
Arindam Dawn
  • 1,769
  • 2
  • 18
  • 35
7
votes
2 answers

How test request http with parameters with jest - angular

I'm testing an angular service with Jest. I can to mock my httpClient, but I have a problem it is that I cannot test a url with a parameter (ex: "http://localhost:8080/api/feature/num?id=25663"). This my test code : describe('MyService', () => { …
De Giovanni
  • 319
  • 1
  • 3
  • 13
7
votes
1 answer

Angular test with asyncScheduler and flush - why it fails with flush()?

There is this Angular component: import { Component, OnDestroy, OnInit } from '@angular/core'; import { asyncScheduler, Observable, of, queueScheduler, scheduled } from 'rxjs'; @Component({ selector: 'test-component', templateUrl:…
Felix
  • 3,999
  • 3
  • 42
  • 66
7
votes
3 answers

Angular Form is not valid even after all form fields are valid

I m trying to solve one Angular form validation Hands on, for that i made the below form, this form is passing all the test cases except one, looks like the problem is with testing file(app.component.spec.ts) that is readable only working Demo of…
bali
  • 327
  • 1
  • 5
  • 14
1 2
3
47 48