0 votes
in AngularJS Packaging and Testing by
What is Jasmine? What is the Use in Angular?

1 Answer

0 votes
by
Jasmine is a javascript testing framework that supports a software development practice called Behaviour Driven Development, or BDD for short. It’s a specific flavour of Test Driven Development (TDD).

Jasmine, and BDD in general, attempts to describe tests in a human readable format so that non-technical people can understand what is being tested.

For example if we wanted to test this function:

function message() {

  return 'Hello world!';

}

We would write a jasmine test spec like so:

describe('Hello world', () => {

  it('says hello', () => {

    expect(message())

        .toEqual('Hello world!');

  });

});

The describe(string, function) function defines what we call a Test Suite, a collection of individual Test Specs.

The it(string, function) function defines an individual Test Spec, this contains one or more Test Expectations.

The expect(actual) expression is what we call an Expectation. In conjunction with a Matcher it describes an expected piece of behaviour in the application.

The matcher(expected) expression is what we call a Matcher. It does a boolean comparison with the expected value passed in vs. the actual value passed to the expect function, if they are false the spec fails.

Built-in matchers

expect(array).toContain(member);

expect(fn).toThrow(string);

expect(fn).toThrowError(string);

expect(instance).toBe(instance);

expect(mixed).toBeDefined();

expect(mixed).toBeFalsy();

expect(mixed).toBeNull();

expect(mixed).toBeTruthy();

expect(mixed).toBeUndefined();

expect(mixed).toEqual(mixed);

expect(mixed).toMatch(pattern);

expect(number).toBeCloseTo(number, decimalPlaces);

expect(number).toBeGreaterThan(number);

expect(number).toBeLessThan(number);

expect(number).toBeNaN();

expect(spy).toHaveBeenCalled();

expect(spy).toHaveBeenCalledTimes(number);

expect(spy).toHaveBeenCalledWith(...arguments);

Setup and teardown

beforeAll: This function is called once, before all the specs in describe test suite are run.

afterAll: This function is called once after all the specs in a test suite are finished.

beforeEach: This function is called before each test specification, it function, has been run.

afterEach: This function is called after each test specification has been run.

We might use these functions like so:

describe('Hello world', () => {

  let expected = "";

  beforeEach(() => {

    expected = "Hello World";

  });

  afterEach(() => {

    expected = "";

  });

  it('says hello', () => {

    expect(message())

        .toEqual(expected);

  });

});

Related questions

0 votes
asked Aug 20, 2021 in AngularJS Packaging and Testing by SakshiSharma
0 votes
asked Aug 19, 2021 in AngularJS Packaging and Testing by SakshiSharma
...