Angular 4 fixture component persists in DOM during Jasmine tests

Jasmine nor Angular will ever remove it automatically, maybe to help you to get more details about your test execution. To remove it you simply use afterEach(…), like: beforeEach(() => { fixture = TestBed.createComponent(MyComponent); comp = fixture.componentInstance; debugElement = fixture.debugElement; element = debugElement.nativeElement; }); afterEach(() => { document.body.removeChild(element); }); For none Angular users: afterEach(() => … Read more

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;

Why is $provide only available in the ‘angular.mock.module’ function, and $q only available in the ‘angular.mock.inject’ function?

You can’t use $provide within the inject function because the former registers providers for the latter to use. Take a look: describe(‘…’, function() { beforeEach(function() { module(function($provide) { $provide.constant(‘someValue’, ‘foobar’); }); inject(function(someValue) { var value = someValue; // will be ‘foobar’; }); }); }); You can though write your test this way: describe(‘…’, function() { … Read more

AngularJS $timeout function not executing in my Jasmine specs

According to the Angular JS documentation for $timeout, you can use $timeout.flush() to synchronously flush the queue of deferred functions. Try updating your test to this: it(‘should do stuff’, function() { expect($scope.stuffDone).toBeFalsy(); $scope.doStuff(); expect($scope.stuffDone).toBeFalsy(); $timeout.flush(); expect($scope.stuffDone).toBeTruthy(); }); Here is a plunker showing both your original test failing and the new test passing.

Does Jasmine’s toThrow matcher require the argument to be wrapped in an anonymous function?

We can do away with the anonymous function wrapper by using Function.bind, which was introduced in ECMAScript 5. This works in the latest versions of browsers, and you can patch older browsers by defining the function yourself. An example definition is given at the Mozilla Developer Network. Here’s an example of how bind can be … Read more

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