Questions tagged [jasmine]

Jasmine is a behavior-driven development (BDD) framework for testing JavaScript code. Jasmine has no external dependencies and does not require a DOM.

Jasmine is a stand-alone behavior-driven development (BDD) framework used for unit testing JavaScript code.

Jasmine tests are broken up into describe and it statements. describe is used to denote the start of a test suite and it is used to denote the start of a particular test. expect statements are then used to outline the conditions under which a test should pass.

beforeEach and afterEach are some other frequently used blocks. beforeEach is used to run some code before each test. Something like loading a module. Similarly afterEach is used to run some code after each test. Running some cleanup code for instance.

Jasmine DOM Testing

If you want to write unit tests for code that does a lot of DOM interactions using jQuery, consider using jasmine-jquery. Jasmine-jQuery offers interacting with dummy HTML through HTML fixtures.

Resources

13306 questions
71
votes
6 answers

Spying on a constructor using Jasmine

I am using Jasmine to test if certain objects are created and methods are called on them. I have a jQuery widget that creates flipcounter objects and calls the setValue method on them. The code for flipcounter is here:…
gerky
  • 6,267
  • 11
  • 55
  • 82
71
votes
2 answers

How to change return value of jasmine spy?

I'm using Jasmine to create a spy like so: beforeEach(inject(function ($injector) { $rootScope = $injector.get('$rootScope'); $state = $injector.get('$state'); $controller = $injector.get('$controller'); socket = new…
Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94
71
votes
5 answers

Jasmine 2.0 async done() and angular-mocks inject() in same test it()

My usual test case looks like it("should send get request", inject(function(someServices) { //some test })); And Jasmine 2.0 async test should look like it("should send get request", function(done) { someAsync.then(function(){ …
huston007
  • 1,972
  • 1
  • 16
  • 13
71
votes
3 answers

Mocking AngularJS module dependencies in Jasmine unit tests

I'm attempting to unit test controller code inside a module that takes other modules as dependencies, but haven't been able to figure out how to mock them properly. I'm using the Jasmine Framework and running my tests with Karma (Testacular).…
fscof
  • 1,593
  • 2
  • 17
  • 28
71
votes
8 answers

Jasmine tests AngularJS Directives with templateUrl

I'm writing directive tests for AngularJS with Jasmine, and using templateUrl with them: https://gist.github.com/tanepiper/62bd10125e8408def5cc However, when I run the test I get the error included in the gist: Error: Unexpected request: GET…
Tane Piper
  • 1,121
  • 2
  • 8
  • 17
70
votes
2 answers

Force-failing a Jasmine test

If I have code in a test that should never be reached (for example the fail clause of a promise sequence), how can I force-fail the test? I use something like expect(true).toBe(false); but this is not pretty. The alternative is waiting for the test…
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
69
votes
5 answers

How to unit test a filter in AngularJS 1.x

How do you unit test a filter in Angular?
eddiec
  • 7,608
  • 5
  • 34
  • 36
68
votes
5 answers

How does one stub promise with sinon?

I have a data service with following function function getInsureds(searchCriteria) { var deferred = $q.defer(); insuredsSearch.get(searchCriteria, function (insureds) { deferred.resolve(insureds); }, …
epitka
  • 17,275
  • 20
  • 88
  • 141
68
votes
5 answers

Getting requirejs to work with Jasmine

I first want to say that I am new to RequireJS and even newer to Jasmine. I am having some issues with the SpecRunner and require JS. I have been following the tutorials of Uzi Kilon and Ben Nadel (along with some others) and they helped some but…
The Sheek Geek
  • 4,126
  • 6
  • 38
  • 48
67
votes
4 answers

angular2 test, how do I mock sub component

How do I mock sub component in jasmine tests? I have MyComponent, which uses MyNavbarComponent and MyToolbarComponent import {Component} from 'angular2/core'; import {MyNavbarComponent} from './my-navbar.component'; import {MyToolbarComponent} from…
allenhwkim
  • 27,270
  • 18
  • 89
  • 122
67
votes
3 answers

How can I test events in angular?

I need to test that events get correctly emitted or broadcast, and trigger events manually. What's the best way to do this?
Kenneth Lynne
  • 15,461
  • 12
  • 63
  • 79
66
votes
2 answers

Jasmine - Spying on a method call within a constructor

I want to test whether the following method is called with in my Javascript object constructor. From what I have seen in the Jasmine documentation, I can spy on a constructor method and I can spy on methods after an object has been instantiated, but…
stillmotion
  • 4,608
  • 4
  • 30
  • 36
66
votes
5 answers

Unit-testing directive controllers in Angular without making controller global

In Vojta Jina's excellent repository where he demonstrates testing of directives, he defines the directive controller outside of the module wrapper. See here: https://github.com/vojtajina/ng-directive-testing/blob/master/js/tabs.js Isn't that bad…
65
votes
2 answers

Sinon JS "Attempted to wrap ajax which is already wrapped"

I got the above error message when I ran my test. Below is my code (I'm using Backbone JS and Jasmine for testing). Does anyone know why this happens? $(function() { describe("Category", function() { beforeEach(function() { category =…
trivektor
  • 5,608
  • 9
  • 37
  • 53
65
votes
3 answers

Testing for instanceof using Jasmine

I'm new to Jasmine and testing in general. One block of my code checks whether my library has been instantiated using the new operator: //if 'this' isn't an instance of mylib... if (!(this instanceof mylib)) { //return a new instance …
Mike Rifgin
  • 10,409
  • 21
  • 75
  • 111