请输入您要查询的百科知识:

 

词条 Jasmine (JavaScript testing framework)
释义

  1. History

  2. Features

  3. Usage

  4. Jasmine test runners

  5. Comparison between Jasmine and Mocha[11]

  6. Benefits

  7. See also

  8. References

  9. External links

{{Primary sources|date=September 2012}}{{Infobox Software
| name = Jasmine
| logo = Logo jasmine.svg
| logo size = 120px
| screenshot =
| caption =
| developer = Pivotal Labs
| released = {{Start date and age|2010|09|14}}[1]
| status = Active
| latest release version = 2.6.1
| latest release date = {{Start date and age|2017|04|27}} [2]
| latest preview version =
| latest preview date =
| operating system = Cross-platform
| programming language = JavaScript
| genre = Unit Test
| license = MIT License[3]
| website = https://jasmine.github.io/
|repo=https://github.com/jasmine/jasmine}}Jasmine is an open source testing framework for JavaScript.[4] 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.[5]

History

The developers at Pivotal Labs for Jasmine previously developed a similar unit testing framework called JsUnit before active development of Jasmine.[6]

Features

  • Supports asynchronous testing.[7]
  • Makes use of 'spies' for implementing test doubles.[7]
  • Supports testing of front-end code through a front-end extension of Jasmine called Jasmine-jQuery.[7]

Usage

Jasmine aims to be easy to read. A simple hello world test looks like the code below, where describe() describes a suite of tests and it() is an individual test specification. The name "it()" follows the idea of behavior-driven development and serves as the first word in the test name, which should be a complete sentence. Usage follows syntax similar to that of RSpec.

The code below tests this function

function helloWorld() {

}

and verifies that its output is indeed the text "Hello world!".

describe('Hello world', function() {

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

});

Jasmine provides a rich set of built-in matchers. In the above example, toEqual checks the equality between the value returned from the helloWorld() function and the 'Hello world!' string. This is the same as assertions used in other testing frameworks. Jasmine matchers simply return a Boolean value: true if the expectation is matched (a way to indicate that the test has passed) or false if the expectation doesn’t match.[7] A good practice is to put a single expectation in an individual it() test specification.

Other built-in matchers include toBe, toBeTruthy, toBeFalsy, toContain, toBeDefined, toBeUndefined, toBeNull, toBeNaN, toBeGreaterThan, toBeLessThan, toBeCloseTo.[8] The identity matcher toBe checks if two things are the same object. The condition matchers toBeTruthy, toBeFalsy evaluate if something is true or false and toBeDefined, toBeUndefined check if something is defined or undefined. As the name suggests toBeNull checks if something is null and toBeNaN checks if something is NotANumber(NaN). Precision matcher toBeCloseTo accepts two parameters and checks if a number is close to the first parameter, given a certain amount of decimal precision as indicated by the second parameter. Matcher toContain is used to verify that an element, object or sub-string is contained in an array, list or string.

The special built-in matcher toThrow is used to verify that an exception has been thrown.[7] The code below verifies that "Some exception" is thrown.

describe('Expect to throw an exception', function() {

  it('throws some exception', function() {    expect( function(){ throw(“Some exception”); }).toThrow(“Some exception”);  });

});

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

Jasmine test runners

Jasmine comes with inbuilt test runner. Jasmine tests can run browser tests by including a simple SpecRunner.html[9] file or by using it as a command line test runner supported for various languages like [https://jasmine.github.io/pages/getting_started.html Nodejs, Python, Ruby], or (old way) by using Karma,[10] a simple JavaScript test runner tool.

Comparison between Jasmine and Mocha[11]

Mocha is another popular Javascript testing framework. The comparison between Jasmine and Mocha is given in the table below.

JasmineMocha
Jasmine comes with test doubles by using spies.Mocha does not come with a test double library, and generally uses an external library like Sinon.
Jasmine has a [https://jasmine.github.io/pages/getting_started.html command line utility] to run tests.Mocha has a command line utility to run tests.
Jasmine has assertions built into it.Mocha does not have an assertions library and uses Chai for assertions.

Benefits

  • The aim of Jasmine is to be browser, framework, platform and language independent.[12]
  • Besides behavior-driven development, Jasmine also supports test driven development.[12]

See also

  • List of JavaScript libraries
  • List of unit testing frameworks
  • Mocha (JavaScript framework)
  • QUnit

References

1. ^{{cite web |url=http://pivotallabs.com/jasmine-1-0-released/ |title=Jasmine 1.0 Released |author=Davis W. Frank |website=Pivotal Labs|accessdate=11 February 2014}}
2. ^{{cite web |url=https://github.com/jasmine/jasmine/releases |title=Releases |accessdate=30 April 2017}}
3. ^{{cite web|title=jasmine/MIT.LICENSE|url=https://github.com/jasmine/jasmine/blob/master/MIT.LICENSE|website=GitHub|accessdate=25 April 2017|language=en}}
4. ^https://jasmine.github.io/
5. ^https://github.com/pivotal/jasmine/wiki/Background
6. ^[https://github.com/pivotal/jsunit Github JsUnit Project Page]
7. ^{{Cite book|title=Jasmine JavaScript Testing|last=Ragonha|first=Paulo|publisher=Packt Publishing|year=2013|isbn=978-1782167211|location=|pages=|quote=|via=}}
8. ^{{Cite book|title=JavaScript Testing with Jasmine|last=Hahn|first=Evan|publisher=O'Reilly Media|year=2013|isbn=978-1449356378|location=|pages=|quote=|via=}}
9. ^{{Cite web|url=https://github.com/jasmine/jasmine/wiki/A-simple-project|title=A simple project|last=|first=|date=|website=|archive-url=|archive-date=|dead-url=|access-date=}}
10. ^{{Cite web|url=https://www.npmjs.com/package/karma-jasmine|title=Karma Jasmine|last=|first=|date=|website=|archive-url=|archive-date=|dead-url=|access-date=}}
11. ^{{Cite news|url=https://marcofranssen.nl/jasmine-vs-mocha/|title=Jasmine vs. Mocha|newspaper=Marco Franssen|access-date=13 February 2017}}
12. ^{{Cite web|url=https://stackshare.io/stackups/jasmine-vs-mocha-vs-qunit|title=Comparison: Jasmine vs Mocha vs QUnit {{!}} StackShare|last=|first=|date=|website=|archive-url=|archive-date=|dead-url=|access-date=13 February 2017}}

External links

  • [https://jasmine.github.io/ Jasmine website]
  • [https://github.com/pivotal/jasmine Jasmine on GitHub]
  • [https://www.npmjs.com/package/js.spec JSSpec]
  • JSpec
  • [https://content.pivotal.io/blog/screw-unit-a-new-js-testing-framework-version-0-1 ScrewUnit]

2 : JavaScript programming tools|Unit testing frameworks

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/12 12:23:49