What is the difference between describe() and it() in spec.js file in protractor?

When writing the test case in spec.js file in protractor then 2 fields are showing describe() and it(). what exactly the use of them and when to use?

// spec.js

describe('Protractor Demo App', function() {
  it('should have a title', function() {
    ..
  });
});

Answer

By reading this hopefully you will come to know your answer.

spec.js about (how Jasmine a behavior-driven development framework for testing JavaScript code works)

It has two main functions

Suite describe Your Tests

A test suite begins with a call to the global Jasmine function describe with two parameters: a string and a function. The string is a name or title for a spec suite – usually what is being tested. The function is a block of code that implements the suite.

Specs

Specs are defined by calling the global Jasmine function it, which, like describe takes a string and a function. The string is the title of the spec and the function is the spec, or test. A spec contains one or more expectations that test the state of the code. An expectation in Jasmine is an assertion that is either true or false. A spec with all true expectations is a passing spec. A spec with one or more false expectations is a failing spec.

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

It’s Just Functions

Since describe and it blocks are functions, they can contain any executable code necessary to implement the test. JavaScript scoping rules apply, so variables declared in a describe are available to any it block inside the suite.

For more details, you can see this link

Leave a Reply

Your email address will not be published. Required fields are marked *