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
166
votes
4 answers

React Enzyme find second (or nth) node

I'm testing a React component with Jasmine Enzyme shallow rendering. Simplified here for the purposes of this question... function MyOuterComponent() { return (
... ... …
sfletche
  • 47,248
  • 30
  • 103
  • 119
166
votes
20 answers

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

I have an angular service called requestNotificationChannel: app.factory("requestNotificationChannel", function($rootScope) { var _DELETE_MESSAGE_ = "_DELETE_MESSAGE_"; function deleteMessage(id, index) { …
Mdb
  • 8,338
  • 22
  • 63
  • 98
163
votes
4 answers

How do I read an Istanbul Coverage Report?

I've always used Jasmine for my unit tests, but recently I started using Istanbul to give me code coverage reports. I mean I get the gist of what they are trying to tell me, but I don't really know what each of these percentages represent (Stmts,…
Scott Sword
  • 4,648
  • 6
  • 32
  • 37
157
votes
8 answers

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

I have myService that uses myOtherService, which makes a remote call, returning promise: angular.module('app.myService', ['app.myOtherService']) .factory('myService', [ myOtherService, function(myOtherService) { function…
Georgii Oleinikov
  • 3,865
  • 3
  • 27
  • 27
149
votes
11 answers

How to check multiple arguments on multiple calls for jest spies?

I have the following function in a React component: onUploadStart(file, xhr, formData) { formData.append('filename', file.name); formData.append('mimeType', file.type); } This is my test that at least gets the spy to be called: const formData…
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
149
votes
10 answers

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

Jasmine's spyOn is good to change a method's behavior, but is there any way to change a value property (rather than a method) for an object? the code could be like below: spyOn(myObj, 'valueA').andReturn(1); expect(myObj.valueA).toBe(1);
Shuping
  • 5,388
  • 6
  • 43
  • 66
138
votes
7 answers

How can I get WebStorm to recognize Jasmine methods?

I have a node.js project that contains some Jasmine specifications. The specifications are in a spec/ subdirectory and have the .spec.coffee extension, as required by jasmine-node. When I open one of my spec files in the WebStorm IDE, all the calls…
Joe White
  • 94,807
  • 60
  • 220
  • 330
138
votes
2 answers

How to have different return values for multiple calls on a Jasmine spy

Say I'm spying on a method like this: spyOn(util, "foo").andReturn(true); The function under test calls util.foo multiple times. Is it possible to have the spy return true the first time it's called, but return false the second time? Or is there a…
mikhail
  • 5,019
  • 2
  • 34
  • 47
131
votes
1 answer

How can I test that a function has not been called?

I'm testing router and have two functions, and I need to test if first function was called and second was not. There is method toHaveBeenCalled but there is no method to test if function was not called. How can I test that? I have code like…
jcubic
  • 61,973
  • 54
  • 229
  • 402
127
votes
10 answers

How can I test that a value is "greater than or equal to" in Jasmine?

I want to confirm that a value is a decimal (or 0), so the number should be greater than or equal to zero and less than 1. describe('percent',function(){ it('should be a decimal', function() { var percent = insights.percent; …
Bryce Johnson
  • 6,689
  • 6
  • 40
  • 51
123
votes
12 answers

Unit Testing AngularJS directive with templateUrl

Using AngularJS. Have a directive. Directive defines templateUrl. Directive needs unit testing. Currently unit testing with Jasmine. This recommends code like: describe('module: my.module', function () { beforeEach(module('my.module')); …
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97
121
votes
9 answers

TypeError: moment().tz is not a function

When testing using jasmine, I am getting this error. TypeError: moment().tz is not a function My code that I try to test is: let myDate = moment().tz(undefined, vm.timeZone).format('YYYY-MM-DD');
Sanath
  • 4,774
  • 10
  • 51
  • 81
119
votes
13 answers

using Jasmines spyon upon a private method

is it possible to use Jasmine unit testing framework's spyon method upon a classes private methods? The documentation gives this example but can this be flexible for a private function? describe("Person", function() { it("calls the sayHello()…
user502014
  • 2,241
  • 5
  • 24
  • 32
117
votes
6 answers

How do I verify jQuery AJAX events with Jasmine?

I am trying to use Jasmine to write some BDD specs for basic jQuery AJAX requests. I am currently using Jasmine in standalone mode (i.e. through SpecRunner.html). I have configured SpecRunner to load jquery and other .js files. Any ideas why the…
mnacos
  • 1,195
  • 2
  • 8
  • 8
115
votes
7 answers

Injecting a mock into an AngularJS service

I have an AngularJS service written and I would like to unit test it. angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']). factory('myService', function ($http, fooService, barService) { this.something =…
BanksySan
  • 27,362
  • 33
  • 117
  • 216