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

QUnit vs Jasmine?

What are the main differences between these two testing frameworks? I am a totally new to Test Driven Development and starting from the very beginning.
Sahat Yalkabov
  • 32,654
  • 43
  • 110
  • 175
111
votes
11 answers

How do I change the timeout on a jasmine-node async spec

How can I get this test to pass without resorting to runs/waitsFor blocks? it("cannot change timeout", function(done) { request("http://localhost:3000/hello", function(error, response, body){ expect(body).toEqual("hello world"); …
Brian Low
  • 11,605
  • 4
  • 58
  • 63
109
votes
5 answers

Unit testing click event in Angular

I'm trying to add unit tests to my Angular 2 app. In one of my components, there is a button with a (click) handler. When the user clicks the button a function is called which is defined in the .ts class file. That function prints a message in the…
Aiguo
  • 3,416
  • 7
  • 27
  • 52
109
votes
5 answers

How can I unit test a component that uses the Router in Angular?

In Angular 2.0.0, I am unit testing a component that uses Router. However I get the 'Supplied parameters do not match any signature of call target.' error. In Visual studio code in spec.ts it is the new Router() that is highlighted in red What is…
Ka Tech
  • 8,937
  • 14
  • 53
  • 78
108
votes
4 answers

How do I test an AngularJS service with Jasmine?

(There is a related question here: Jasmine test does not see AngularJS module) I just want to test a service without bootstrapping Angular. I have look at some examples and the tutorial but I am not going anywhere. I have just three…
Robert
  • 31,925
  • 8
  • 35
  • 33
100
votes
3 answers

jasmine toHaveBeenCalledWith partial matching

With Jasmine, I could spy on methods and figure out the arguments. I want to be able to call toHaveBeenCalledWith(something, anything). Let's say I want to spy on a method .on(event, callback). All I care about is if the event is listened to rather…
Pwnna
  • 9,178
  • 21
  • 65
  • 91
98
votes
6 answers

Checking object equality in Jasmine

Jasmine has built-in matchers toBe and toEqual. If I have an object like this: function Money(amount, currency){ this.amount = amount; this.currency = currency; this.sum = function (money){ return new Money(200, "USD"); …
Dan
  • 11,077
  • 20
  • 84
  • 119
98
votes
5 answers

Is there a jasmine matcher to compare objects on subsets of their properties

I have an object that may be extended along my behavior under test, but I want to make sure that the original properties are still there. var example = {'foo':'bar', 'bar':'baz'} var result = extendingPipeline(example) // {'foo':'bar', 'bar':'baz',…
iwein
  • 25,788
  • 10
  • 70
  • 111
97
votes
8 answers

Angular Material and Jasmine : " No provider for InjectionToken MdDialogData! "

I have a component which is meant to be used in an Angular Material MdDialog : @Component({ ... }) export class MyComponent { constructor(@Inject(MAT_DIALOG_DATA) public data: any, public dialogRef: MdDialogRef) { ... } } I…
Wenneguen
  • 3,196
  • 3
  • 13
  • 23
94
votes
6 answers

Angular unit testing with Jasmine: how to remove or modify spyOn

AngularJS v1.2.26 Jasmine v2.2.0 How can I change or remove the behavior of a spyOn? When I try to override it, I get the following error: Error: getUpdate has already been spied upon var data1 = 'foo'; var data2 = 'bar'; describe("a spec with a…
emersonthis
  • 32,822
  • 59
  • 210
  • 375
94
votes
4 answers

How to stub a method of jasmine mock object?

According to the Jasmine documentation, a mock can be created like this: jasmine.createSpyObj(someObject, ['method1', 'method2', ... ]); How do you stub one of these methods? For example, if you want to test what happens when a method throws an…
Adelin
  • 18,144
  • 26
  • 115
  • 175
91
votes
10 answers

How do we clear spy programmatically in Jasmine?

How do we clear the spy in a jasmine test suite programmatically? Thanks. beforeEach(function() { spyOn($, "ajax").andCallFake(function(params){ }) }) it("should do something", function() { //I want to override the spy on ajax here and do it…
trivektor
  • 5,608
  • 9
  • 37
  • 53
90
votes
4 answers

Angular2 testing: What's the difference between a DebugElement and a NativeElement object in a ComponentFixture?

I'm currently putting together some best practices for testing Angular 2 apps on a component level. I've seen a few tutorials query a fixture's NativeElement object for selectors and the like, e.g. it('should render "Hello World!" after click',…
dgkane
  • 1,005
  • 1
  • 7
  • 9
90
votes
4 answers

Angular2 unit test with @Input()

I've got a component that uses the @Input() annotation on an instance variable and I'm trying to write my unit test for the openProductPage() method, but I'm a little lost at how I setup my unit test. I could make that instance variable public, but…
hartpdx
  • 2,230
  • 2
  • 15
  • 18
89
votes
5 answers

Angular 7 Test: NullInjectorError: No provider for ActivatedRoute

Hi have some error with testing my App made with Angular 7. I do not have much experience in angular, so I would need your help+ Error: StaticInjectorError(DynamicTestModule)[BeerDetailsComponent -> ActivatedRoute]: StaticInjectorError(Platform:…
Daniele Caputo
  • 899
  • 1
  • 6
  • 8