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
85
votes
5 answers

How can I run tests with a headless browser?

Using: ng test Angular CLI runs the tests by default in Chrome, which is great, but what if I need to run them in a console-only environment (headless browser)? Also it would be nice if I can specify if I want browser-less or not each time I run…
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
81
votes
10 answers

mocking window.location.href in Javascript

I have some unit tests for a function that makes use of the window.location.href -- not ideal I would far rather have passed this in but its not possible in the implementation. I'm just wondering if its possible to mock this value without actually…
wmitchell
  • 5,665
  • 10
  • 37
  • 62
81
votes
4 answers

How to Unit Test Isolated Scope Directive in AngularJS

What is a good way to unit test isolated scope in AngularJS JSFiddle showing unit test Directive snippet scope: {name: '=myGreet'}, link: function (scope, element, attrs) { //show the initial state greet(element,…
SavoryBytes
  • 35,571
  • 4
  • 52
  • 61
80
votes
2 answers

Unit testing AngularJS factories that have dependencies

When unit testing an Angular factory (with Karma + Jasmine), how do I inject a stub dependency into the factory under test? Here's my factory: mod = angular.module('myFactoryMod', []); mod.factory('myFactory', [ '$log', 'oneOfMyOtherServices',…
Roy Truelove
  • 22,016
  • 18
  • 111
  • 153
79
votes
2 answers

Using object types with Jasmine's toHaveBeenCalledWith method

I've just started using Jasmine so please forgive the newbie question but is it possible to test for object types when using toHaveBeenCalledWith? expect(object.method).toHaveBeenCalledWith(instanceof String); I know I could this but it's checking…
screenm0nkey
  • 18,405
  • 17
  • 57
  • 75
79
votes
6 answers

Unit testing an observable in Angular 2

What is the correct way of unit testing a service returning an Observable result in Angular 2? Let's say we have a getCars method in a CarService service class: ... export class CarService{ ... getCars():Observable{ return…
Erdinc Guzel
  • 937
  • 1
  • 6
  • 10
79
votes
10 answers

Protractor : How to wait for page complete after click a button?

In a test spec, I need to click a button on a web page, and wait for the new page completely loaded. emailEl.sendKeys('jack'); passwordEl.sendKeys('123pwd'); btnLoginEl.click(); // ...Here need to wait for page complete...…
Zach
  • 5,715
  • 12
  • 47
  • 62
77
votes
10 answers

Print message on expect() assert failure

Is there a way to print a custom error message when a Jasmine expect() fails? As an example, for end to end testing I have an array of web pages and I use one test to go to each URL and assert an element exists on each page. I know I can put every…
user3517049
  • 826
  • 1
  • 6
  • 9
76
votes
1 answer

What does the underscores in _servicename_ mean in AngularJS tests?

In the following example test, the original provider name is APIEndpointProvider, but for injection and service instantiation the convention seems to be it has to be injected with underscores wrapping it. Why is that? 'use…
Kenneth Lynne
  • 15,461
  • 12
  • 63
  • 79
76
votes
6 answers

Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression?

I have reviewed Jasmine's documentation of the toHaveBeenCalledWith matcher in order to understand whether it's possible to pass in a regular expression for an argument, if that argument is expected to be a string. Unfortunately, this is unsupported…
zealoushacker
  • 6,766
  • 5
  • 35
  • 44
74
votes
2 answers

Jasmine vs. Mocha JavaScript testing for Rails 3.1+

I have experience with Jasmine and do like it quite a bit. Does anyone have experience with both Jasmine and Mocha, specifically for Rails? I am wondering if it's worth switching to.
LupineDev
  • 1,046
  • 1
  • 8
  • 10
74
votes
5 answers

Test a function that contains a setTimeout()

I have a close function in my component that contains a setTimeout() in order to give time for the animation to complete. public close() { this.animate = "inactive" setTimeout(() => { this.show = false }, 250) } this.show is…
ed-tester
  • 1,596
  • 4
  • 17
  • 24
74
votes
1 answer

What is the difference between createspy and createspyobj

I have used in my code like. return $provide.decorator('aservice', function($delegate) { $delegate.addFn = jasmine.createSpy().andReturn(true); return $delegate; }); In that what createSpy do? can i change the…
user3686652
  • 805
  • 1
  • 6
  • 11
73
votes
3 answers

Why can't nested describe() blocks see vars defined in outer blocks?

I've run into this issue in real code, but I put together a trivial example to prove the point. The below code works fine. I've set up a variable in my root describe() block that is accessible within my sub-describe()s' it() blocks. describe('simple…
Ken Bellows
  • 6,711
  • 13
  • 50
  • 78
73
votes
8 answers

Redirect calls to console.log() to standard output in Jasmine tests

I'm using Jasmine via the jasmine-maven-plugin, and I would like to see console.log() messages in the Maven build output. Is there a way to achieve this? If console.log() cannot be redirected, is there any other way to log from my tests so that they…
Lóránt Pintér
  • 10,152
  • 14
  • 47
  • 53