How do I unit test if an element is visible when the *ngIf directive is used using Jasmine in Angular

If the element is hidden, then it wont be rendered inside the dom. You can check expect(fixture.debugElement.query(By.css(‘.header’))).toBeUndefined(); EDIT : toBeNull() works better in the above case expect(fixture.debugElement.query(By.css(‘.header’))).toBeNull(); And also you have a syntax error while fetching the button element. nativeElement is not a function. Change it this way : const button = fixture.debugElement.query(By.css(‘button’)).nativeElement;

Test pipe with dependencies on services

Because of the DI in your pipe, you need to configure a test environment (test bed) to resolve the dependency: import { BrowserModule, DomSanitizer } from ‘@angular/platform-browser’; import { inject, TestBed } from ‘@angular/core/testing’; describe(‘SanitiseHtmlPipe’, () => { beforeEach(() => { TestBed .configureTestingModule({ imports: [ BrowserModule ] }); }); it(‘create an instance’, inject([DomSanitizer], (domSanitizer: DomSanitizer) … Read more

Angular 6 – NullInjectorError: No provider for HttpClient in unit tests

import { TestBed } from ‘@angular/core/testing’; import { HttpClientTestingModule, HttpTestingController } from ‘@angular/common/http/testing’; import {HttpClientModule} from ‘@angular/common/http’; import { myService } from ‘./myservice’; describe(‘myService’, () => { beforeEach(() => TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [myService] })); it(‘should be created’, () => { const service: myService = TestBed.get(myService); expect(service).toBeTruthy(); }); it(‘should have getData function’, () => { … Read more

Angular2 unit test with @Input()

this is from official documentation https://angular.io/docs/ts/latest/guide/testing.html#!#component-fixture. So you can create new input object expectedHero and pass it to the component comp.hero = expectedHero Also make sure to call fixture.detectChanges(); last, otherwise property will not be bound to component. Working Example // async beforeEach beforeEach( async(() => { TestBed.configureTestingModule({ declarations: [ DashboardHeroComponent ], }) .compileComponents(); // … Read more

Angular 2 Testing – Async function call – when to use

async will not allow the next test to start until the async finishes all its tasks. What async does is wrap the callback in a Zone, where all asynchronous tasks (e.g. setTimeout) are tracked. Once all the asynchronous tasks are complete, then the async completes. If you have ever worked with Jasmine outside out Angular, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)