Zum Inhalt springen

Jasmine (JavaScript)

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 17. November 2011 um 16:42 Uhr durch imported>Sdesalas (Added section on matchers). Sie kann sich erheblich von der aktuellen Version unterscheiden.
Jasmine
Basisdaten

Entwickler Pivotal Labs
Erscheinungsjahr 12. August 2009
Aktuelle Version 5.8.0[1]
(7. Juni 2025)
Betriebssystem Plattformunabhängig
Programmier­sprache JavaScript
Lizenz MIT-Lizenz[2]
jasmine.github.io

Jasmine is an open source unit testing framework for JavaScript.[3] It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.[4]

Usage

Jasmine aims to be easy to read. A simple hello world test looks like this:

describe('Hello world', function() {
	it('says hello', function() {
		expect(helloWorld()).toEqual("Hello world!");
	});
});

Jasmine has a number of other features, such as custom matchers, spies, and support for asynchronous specifications.

Matchers

As of version 1.1, the Jasmine framework supports the following matchers: [5]

toEqual

it("converts inches to centimeters", function () {  
    expect(Convert(12, "in").to("cm")).toEqual(30.48);  
});

toThrow

it("throws an error when passed an unknown to-unit", function () {  
    var testFn = function () {  
        Convert(1, "cm").to("furlongs");  
    }  
    expect(testFn).toThrow(new Error("unrecognized to-unit"));  
});

toBeDefined / toBeUndefined

it("is defined", function () {  
    var name = "Andrew";  
    expect(name).toBeDefined();  
})  
  
it("is not defined", function () {  
    var name;  
    expect(name).toBeUndefined();  
});

toBeTruthy / toBeFalsy

it("is true", function () {  
    expect(Lib.isAWeekDay()).toBeTruthy();  
});  
it("is false", function () {  
    expect(Lib.finishedQuiz).toBeFalsy();  
});

toBeLessThan / toBeGreaterThan

it("is less than 10", function () {  
    expect(5).toBeLessThan(10);  
});  
it("is greater than 10", function () {  
    expect(20).toBeGreaterThan(10);  
});

toMatch

it("outputs the right text", function () {  
    expect(cart.total()).toMatch(/\$\d*.\d\d/);  
});

toContain

it("should contain oranges", function () {  
    expect(["apples", "oranges", "pears"]).toContain("oranges");  
});


References

Vorlage:Reflist

Vorlage:Programming-software-stub

  1. Release 5.8.0. 7. Juni 2025 (abgerufen am 18. Juni 2025).
  2. api.github.com. (abgerufen am 19. Oktober 2023).
  3. http://pivotal.github.com/jasmine/
  4. https://github.com/pivotal/jasmine/wiki/Background
  5. NetTuts+, 2011 http://net.tutsplus.com/tutorials/javascript-ajax/testing-your-javascript-with-jasmine/