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

12446 questions
532
votes
9 answers

How to write a test which expects an Error to be thrown in Jasmine?

I'm trying to write a test for the Jasmine Test Framework which expects an error. At the moment I'm using a Jasmine Node.js integration from GitHub. In my Node module I have the following code: throw new Error("Parsing is not possible"); Now I try…
echox
  • 8,926
  • 6
  • 30
  • 51
380
votes
7 answers

Jasmine JavaScript Testing - toBe vs toEqual

Let's say I have the following: var myNumber = 5; expect(myNumber).toBe(5); expect(myNumber).toEqual(5); Both of the above tests will pass. Is there a difference between toBe() and toEqual() when it comes to evaluating numbers? If so, when I should…
Lloyd Banks
  • 32,108
  • 50
  • 143
  • 228
233
votes
12 answers

How to write unit testing for Angular / TypeScript for private methods with Jasmine

How do you test a private function in angular 2 ? class FooBar { private _status: number; constructor( private foo : Bar ) { this.initFooBar(); } private initFooBar(){ this.foo.bar( "data" ); this._status…
tymspy
  • 3,419
  • 2
  • 16
  • 33
222
votes
15 answers

Angular 2 Unit Tests: Cannot find name 'describe'

I'm following this tutorial from angular.io As they said, I've created hero.spec.ts file to create unit tests: import { Hero } from './hero'; describe('Hero', () => { it('has name', () => { let hero: Hero = {id: 1, name: 'Super Cat'}; …
Piotrek
  • 9,838
  • 14
  • 66
  • 120
214
votes
4 answers

Jasmine.js comparing arrays

Is there a way in jasmine.js to check if two arrays are equal, for example: arr = [1, 2, 3] expect(arr).toBe([1, 2, 3]) expect(arr).toEqual([1, 2, 3]) Neither seems to work.
user2032804
  • 2,419
  • 2
  • 11
  • 12
207
votes
9 answers

How to access and test an internal (non-exports) function in a node.js module?

I'm trying to figure out on how to test internal (i.e. not exported) functions in nodejs (preferably with mocha or jasmine). And i have no idea! Let say I have a module like that: function exported(i) { return notExported(i) + 1; } function…
xavier.seignard
  • 10,084
  • 12
  • 44
  • 71
196
votes
5 answers

toBe(true) vs toBeTruthy() vs toBeTrue()

What is the difference between expect(something).toBe(true), expect(something).toBeTruthy() and expect(something).toBeTrue()? Note that toBeTrue() is a custom matcher introduced in jasmine-matchers among other useful and handy matchers like…
alecxe
  • 414,977
  • 106
  • 935
  • 1,083
185
votes
10 answers

Running a single test file

Is there a way to run ng test for a single file instead of for the entire test suite? Ideally, I'd like to get the quickest possible feedback loop when I'm editing a file, but karma executes the whole suite on each save, which is a bit slow when…
Elliot Larson
  • 9,039
  • 4
  • 33
  • 55
182
votes
11 answers

How to execute only one test spec with angular-cli

I have Angular2 project build with Angular-CLI (beta 20). Is there a way to run tests against only one selected spec file? I used to have a project based on Angular2 quick start, and I could manually add specs to jasmine file. But I don't know how…
Zielu
  • 7,144
  • 4
  • 26
  • 39
176
votes
3 answers

Any way to modify Jasmine spies based on arguments?

I have a function I'd like to test which calls an external API method twice, using different parameters. I'd like to mock this external API out with a Jasmine spy, and return different things based on the parameters. Is there any way to do this in…
Jmr
  • 11,788
  • 4
  • 37
  • 33
169
votes
7 answers

Using Jasmine to spy on a function without an object

I'm new to Jasmine and have just started using it. I have a library js file with lots of functions which are not associated with any object (i.e. are global). How do I go about spying on these functions? I tried using window/document as the object,…
Chetter Hummin
  • 6,249
  • 8
  • 30
  • 44
163
votes
9 answers

How do I focus on one spec in jasmine.js?

I have a bunch of failing specs from a rather large architectural change. I'd like to work on fixing them one by one by tagging each one with 'focus'. Does jasmine.js have a feature like this? I swore I read at one point that it does but I don't see…
Dane O'Connor
  • 67,996
  • 36
  • 114
  • 164
153
votes
18 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
  • 7,538
  • 20
  • 58
  • 94
152
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,707
  • 3
  • 24
  • 26
144
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
  • 36,606
  • 25
  • 86
  • 108
1
2 3
99 100