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
5
votes
1 answer

Is Jasmine's beforeEach synchronous?

If you have multiple beforeEach's, will they always run one after another? beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); It seems that they will. I tried…
Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
5
votes
1 answer

WebStorm: Karma server not starting, TypeError: undefined is not a function on server.start();

I have the latest version of WebStorm (10.0.4). Today I wanted to include karma in my project so I installed Python and ran: npm install -g karma npm install karma npm install karma-jasmine npm install karma-chrome-launcher npm install…
luuksen
  • 1,357
  • 2
  • 12
  • 38
5
votes
1 answer

Jasmine Testing angular directive with [^form] dependency

I am trying to test a directive, as explained e.g. here http://angular-tips.com/blog/2014/06/introduction-to-unit-test-directives/. However, in my directive I use form, so I have this in my directive declaration object: return { …
onkami
  • 8,791
  • 17
  • 90
  • 176
5
votes
1 answer

Why call scope.$digest() after $compile(element)(scope) in unit testing

Below is a very common generic scenario used for testing directive: var element,scope; beforeEach(inject(function ($rootScope,$compile) { scope = $rootScope.$new() element = angular.element('
') …
philomath
  • 2,209
  • 6
  • 33
  • 45
5
votes
2 answers

How to test window.location.href using jasmine

This is my controller code $scope.loadApplications = function () { var cacheKey = { key: cacheKeys.appList() }; dataFactory.get("/Application/All", { cache: cacheKey }) .then(function (result) { …
Syed Rasheed
  • 559
  • 2
  • 10
  • 29
5
votes
3 answers

Unit testing with Jasmine, mocking a constructor

I'm unit testing JavaScript with Jasmine and I am running into some problems. I have a large file to test and it has a lot of dependencies and those dependencies have their own dependencies. Because of said dependencies I want to mock all I can.…
sergei
  • 157
  • 3
  • 9
5
votes
2 answers

Testing non-scope functions in Angular Controller with Jasmine

Jasmine is one of the most widely used testing frameworks to unit-test javascript code in BDD manner. I tried to use it for AngularJS components testing. AngularJS documentation provides the following sample code describe('PasswordController',…
Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76
5
votes
1 answer

Proper use of karma-commonjs with Jasmine 2

I've spent a fair amount of time trying to debug this, and figured I would ask. I even created a GitHub repository but won't rely on it, so here goes. I'm trying to take advantage of CommonJS syntax within the Karma test runner using PhantomJS. For…
zen
  • 571
  • 5
  • 13
5
votes
0 answers

No pending request to flush on using whenGET, but not expectGET

When testing an angular service using $httpBackend I get some unexpected results. When I run the test using httpBackend.expectGET the test works as expected. However if I run the exact same test using whenGET the test fails with the message 'No…
Daltin
  • 91
  • 6
5
votes
1 answer

Expected undefined to be defined while testing angular controller with Jasmine

I'm a newbie in testing Angular Apps with Jasmine, and I can't figure out the cause of this problem... The controller programsModule.controller('ticketCtrl', ['$scope', function ($scope) { $scope.disableSendBtn = true; }); And this is the unit…
LeoAref
  • 174
  • 1
  • 12
5
votes
1 answer

Angular + Jasmine: beforeEach() syntax with module()

Can you please explain how this line works: beforeEach(module('phonecatApp')); beforeEach() expects a callback function to call before each test. module() returns an angular.Module object. What does beforeEach() do with an object?
Laszlo B
  • 455
  • 3
  • 14
5
votes
1 answer

Jasmine 2 custom matcher for hasClass in Protactor

I upgrade my Jasmine 1.3 to 2.0 so I added a custom matcher to check css is present.Below is the code to check the matcher hasClass = function(actual,expected){ return actual.getAttribute('class').then(function (classes) { return…
Arpit
  • 159
  • 8
5
votes
1 answer

How can I simulate a timeout event in a XHR using jasmine?

I'm testing a function that makes AJAX requests, allowing retries when the network is not working or there are timeouts because the connection is unstable (I'm thinking about mobile devices). I'm sure it works because I've used it integrated with…
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
5
votes
1 answer

Mocking global HTTP requests for all suites in Angular and Jasmine

In my app.config I am using a service called ui-router-extras FutureStates to dynamically create states from the results of a REST call. One of the side effects of this is that when my tests run, since I am loading my main app module in all of them,…
DigTheDoug
  • 1,420
  • 10
  • 20
5
votes
1 answer

loadStyleFixtures doesn't load css in jasmine tests

Background is that I'm trying to create a very basic thermostat in javascript and then test it, using jasmine. I want to be able to test that the page loads with certain css (i.e. that the thermostat is the right colour) and then updates it. Tests…
George McG
  • 156
  • 10
1 2 3
99
100