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
600
votes
12 answers

How can I 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.js module I have the following code: throw new Error("Parsing is not possible"); Now I…
echox
  • 9,726
  • 6
  • 33
  • 51
430
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
  • 35,740
  • 58
  • 156
  • 248
304
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
  • 4,200
  • 2
  • 20
  • 35
256
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
  • 462,703
  • 120
  • 1,088
  • 1,195
251
votes
16 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
  • 8,312
  • 4
  • 28
  • 41
244
votes
15 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
  • 10,669
  • 5
  • 38
  • 57
235
votes
17 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
  • 10,919
  • 18
  • 73
  • 136
235
votes
11 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
  • 11,254
  • 13
  • 51
  • 75
230
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,599
  • 2
  • 14
  • 12
199
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
  • 12,078
  • 4
  • 39
  • 33
191
votes
4 answers

What is the difference between describe and it in Jest?

When writing a unit test in Jest or Jasmine when do you use describe? When do you use it? I usually do describe('my beverage', () => { test('is delicious', () => { }); }); When is it time for a new describe or a new it?
Brown Limie
  • 2,249
  • 3
  • 17
  • 18
183
votes
10 answers

Using Jasmine to spy on a function without an object

I'm using Jasmine and 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, but the spy did not work even…
Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
175
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
  • 75,180
  • 37
  • 119
  • 173
173
votes
20 answers

How to mock window.location.href with Jest + Vuejs?

Currently, I am implementing unit tests for my project and there is a file that contains window.location.href. I want to mock this to test and here is my sample code: it("method A should work correctly", () => { const url =…
Hoang Subin
  • 6,610
  • 6
  • 37
  • 56
168
votes
14 answers

Expect Arrays to be equal ignoring order

With Jasmine is there a way to test if 2 arrays contain the same elements, but are not necessarily in the same order? ie array1 = [1,2,3]; array2 = [3,2,1]; expect(array1).toEqualIgnoreOrder(array2);//should be true
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
1
2 3
99 100