Jasmine Framework For Testing JavaScript Code

Posted By : Shilpa Adlakha | 30-Nov-2017

Jasmine is an open source framework for testing the code written in JavaScript . This doesn't have any dependency on other JavaScript frameworks And it is easy to understand and the test can be written very easily.

Suites:

It explains your tests

A test suite works with the global Jasmine function, test suit calls jasmine function.It has two parameters:
 1) a string and
 2) a function.
The string is the name or title for a spec suite - which explains what has to be tested.
 The function represents a block of code .

 

Specs:

Specs, which are defined with the help of calling the global function it, it takes two parameters a string and a function.
where, the string is the title of the spec and the function is the  test. A test has one or more expectations which are used to test the state of the code. An expectation in this framework results, which is either result of true or false. A spec with all true outcomes passes spec. A spec with one or more false results is a failing test.


for Example:


 

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

It's Just Functions

The describe and its blocks represent functions, they can have any executable code which is used to implement the test.It follows the javascript scoping rule.


 

describe("A suite is just a function", function() {
  var a;

  it("and so is a spec", function() {
    a = true;

    expect(a).toBe(true);
  });
});

Expectations:

Expectations which are created with the function expect which takes a value called the Actual Value. It is chained with the Matcher function, that takes the expected value.

describe("The 'havetoBe' matcher compares with ===", function() {

Matchers:

Every matcher performs a comparison which is boolean between the two values i.e actual value and the expected value. It reports to the framework if any expectation is true or false. Jasmine will results either pass of fail.

  it("and has a positive case", function() {
    expect(true).toBe(true);
  });

Any matcher can result in a negative output, it does by chaining the call to the expectations with a not before calling that matcher.

  it("and can have a negative case", function() {
    expect(false).not.toBe(true);
  });
});

 

Thanks

 

About Author

Author Image
Shilpa Adlakha

Shilpa is a bright Wordpress Developer, she has good knowledge of HTML, CSS, PHP and Wordpress.

Request for Proposal

Name is required

Comment is required

Sending message..