How to spyOn a value property (rather than a method) with Jasmine

In February 2017, they merged a PR adding this feature, they released in April 2017. so to spy on getters/setters you use: const spy = spyOnProperty(myObj, ‘myGetterName’, ‘get’); where myObj is your instance, ‘myGetterName’ is the name of that one defined in your class as get myGetterName() {} and the third param is the type … Read more

How can I write a test which expects an ‘Error’ to be thrown in Jasmine?

Try using an anonymous function instead: expect( function(){ parser.parse(raw); } ).toThrow(new Error(“Parsing is not possible”)); you should be passing a function into the expect(…) call. Your incorrect code: // incorrect: expect(parser.parse(raw)).toThrow(new Error(“Parsing is not possible”)); is trying to actually call parser.parse(raw) in an attempt to pass the result into expect(…),

Click() function isn’t working in protractor scripts

There might be multiple reasons for that and it is going to be a guessing game anyway. it could be that there is an another element matching the .login-button locator and you are clicking a different element. Let’s improve the locator: element(by.css(“.login-form .login-button”)).click(); wait for the element to be clickable: var EC = protractor.ExpectedConditions; element(by.model(‘credentials.username’)).sendKeys(‘RET02’); … Read more

How can I unit test a component that uses the Router in Angular?

You can also just use the RouterTestingModule and just spyOn the navigate function like this… import { TestBed } from ‘@angular/core/testing’; import { RouterTestingModule } from ‘@angular/router/testing’; import { Router } from ‘@angular/router’; import { MyModule } from ‘./my-module’; import { MyComponent } from ‘./my-component’; describe(‘something’, () => { let fixture: ComponentFixture<LandingComponent>; let router: Router; … Read more

How do I mock a service that returns promise in AngularJS Jasmine unit test?

I’m not sure why the way you did it doesn’t work, but I usually do it with the spyOn function. Something like this: describe(‘Testing remote call returning promise’, function() { var myService; beforeEach(module(‘app.myService’)); beforeEach(inject( function(_myService_, myOtherService, $q){ myService = _myService_; spyOn(myOtherService, “makeRemoteCallReturningPromise”).and.callFake(function() { var deferred = $q.defer(); deferred.resolve(‘Remote call result’); return deferred.promise; }); } it(‘can … Read more

jasmine: Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

Having an argument in your it function (done in the code below) will cause Jasmine to attempt an async call. //this block signature will trigger async behavior. it(“should work”, function(done){ //… }); //this block signature will run synchronously it(“should work”, function(){ //… }); It doesn’t make a difference what the done argument is named, its … Read more