Stop jasmine test after first expect fails

@Gregg’s answer was correct for the latest version of Jasmine at that time (v2.0.0). However, since then, this new feature was added in v2.3.0: Allow user to stop a specs execution when an expectation fails (Fixes #577) It’s activated by adding throwFailures=true to the query string of the runner page, eg: http://localhost:8000/?throwFailures=true

E2E mock $httpBackend doesn’t actually passThrough for me

I stumbled on the same problem but instead of implementing a rich API or replacing the original angular-mocks simply added in the following helper: angular.module(‘httpReal’, [‘ng’]) .config([‘$provide’, function($provide) { $provide.decorator(‘$httpBackend’, function() { return angular.injector([‘ng’]).get(‘$httpBackend’); }); }]) .service(‘httpReal’, [‘$rootScope’, function($rootScope) { this.submit = function() { $rootScope.$digest(); }; }]); It patches two issues that prevent an HTTP … Read more

Expect item in array

Looks like you need a custom matcher. Depending on the version of Jasmine you are using: With Jasmine 1: this.addMatchers({ toBeIn: function(expected) { var possibilities = Array.isArray(expected) ? expected : [expected]; return possibilities.indexOf(this.actual) > -1; } }); With Jasmine 2: this.addMatchers({ toBeIn: function(util, customEqualityTesters) { return { compare: function(actual, expected) { var possibilities = Array.isArray(expected) … Read more

What is the difference between fakeAsync and async in Angular testing?

tl;dr In almost all cases, they can be used interchangeably, but using fakeAsync()/tick() combo is preferred unless you need to make an XHR call, in which case you MUST use async()/whenStable() combo, as fakeAsync() does not support XHR calls. For the most part they can be used interchangeably. I can’t think of anything off the … Read more

How to mock Angular 4.3 httpClient an error response in testing

The expectOne method in HttpTestingController class returns a TestRequest object. This TestRequest class has a flush method which can be used to deliver both successful and unsuccessful responses. We can resolve the request by returning a body along with some additional response headers (if any). Relevant info can be found here. Now, coming back to … Read more

Spying on a constructor using Jasmine

flipCounter is just another function, even if it also happens to construct an object. Hence you can do: var cSpy = spyOn(window, ‘flipCounter’); to obtain a spy on it, and do all sorts of inspections on it or say: var cSpy = spyOn(window, ‘flipCounter’).andCallThrough(); var counter = flipCounter(‘foo’, options); expect(cSpy).wasCalled(); However, this seems overkill. It … Read more

How do I change the timeout on a jasmine-node async spec

You can (now) set it directly in the spec, as per Jasmine docs. describe(“long asynchronous specs”, function() { var originalTimeout; beforeEach(function() { originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; }); it(“takes a long time”, function(done) { setTimeout(function() { done(); }, 9000); }); afterEach(function() { jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; }); });