What does the "it" function do in this code?

tarrball picture tarrball · Jun 10, 2014 · Viewed 28.6k times · Source

I'm hoping somebody could explain to me what "it" does (is used for) in AngularJS or just plain JavaScript (I'm not sure if it's specific to Angular). This, turns out, is a difficult thing to Google for, being named "it" and all. I've seen it used throughout the AngularJS docs. I'll give you an example from the ngShow page (it's code to hide/show a div containing a thumbs up or thumbs down).

var thumbsUp = element(by.css('span.glyphicon-thumbs-up'));
var thumbsDown = element(by.css('span.glyphicon-thumbs-down'));

it('should check ng-show / ng-hide', function() {
  expect(thumbsUp.isDisplayed()).toBeFalsy();
  expect(thumbsDown.isDisplayed()).toBeTruthy();

  element(by.model('checked')).click();

  expect(thumbsUp.isDisplayed()).toBeTruthy();
  expect(thumbsDown.isDisplayed()).toBeFalsy();
});

Answer

maerics picture maerics · Jun 10, 2014

See the Jasmine testing framework.

The it(...) function defines a test case (aka a "spec").

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

Note that AngularJS E2E Testing...

... uses Jasmine for its test syntax.