Learn and master the Top 30 Jasmine JS interview questions with detailed answers and code examples:

1. What is Jasmine JS?

Jasmine is a behavior-driven development (BDD) testing framework for JavaScript.

It provides a clean and readable syntax for writing tests and comes with a rich set of assertion functions and test runner capabilities.

2. How do you install Jasmine JS?

You can install Jasmine JS by including the Jasmine library in your project or by using npm. To install via npm, run the following command:

npm install jasmine

3. How do you write a basic test case in Jasmine?

In Jasmine, you use the `describe` and `it` functions to define test suites and test cases, respectively. Here’s an example:

describe('Math operations', () => {
   it('should add two numbers correctly', () => {
     expect(add(2, 3)).toBe(5);
   });
});

4. How do you run Jasmine tests?

Jasmine tests can be run by opening the HTML file that includes the Jasmine test runner or by using a test runner like Karma or Jasmine command-line interface (CLI).

5. How do you write an expectation in Jasmine?

Jasmine provides the `expect` function for writing expectations. You can use various matcher functions to define the expected behavior. Here’s an example:

expect(result).toBe(expected);

6. How do you use matchers in Jasmine?

Matchers are used in Jasmine to define the expected behavior in test assertions.

Jasmine provides a wide range of matchers such as `toBe`, `toEqual`, `toContain`, `toHaveBeenCalled`, etc. Here’s an example:

expect(value).toBe(expected);

7. How do you use the `toBe` matcher in Jasmine?

   – The `toBe` matcher compares the actual value with the expected value using strict equality (`===`). It is used to test for exact equality. Here’s an example:

expect(value).toBe(expected);

8. How do you use the `toEqual` matcher in Jasmine?

The `toEqual` matcher recursively checks the equality of all properties of the actual and expected values. It is used for deep equality checks. Here’s an example:

expect(obj1).toEqual(obj2);

9. How do you use the `toContain` matcher in Jasmine?

See also  How to create a server in node.js in 4 steps

The `toContain` matcher is used to check if a value is present in an array or a substring is present in a string. Here’s an example:

expect(array).toContain(value);
expect(string).toContain(substring);

10. How do you use the `toHaveBeenCalled` matcher in Jasmine?

The `toHaveBeenCalled` matcher is used to check if a function has been called. Here’s an example:

expect(spy).toHaveBeenCalled();

11. How do you use the `toThrow` matcher in Jasmine?

The `toThrow` matcher is used to check if a function throws an exception when called. Here’s an example:

      expect(function).toThrow();

12. How do you use the `beforeEach` function in Jasmine?

The `beforeEach` function is used to define setup code that should be executed before each test case in a test suite. Here’s an example:

describe('Math operations', () => {
   let result;
     beforeEach(() => {
        result = add(2, 3);
     });

     it('should add two numbers correctly', () => {
        expect(result).toBe(5);
      });
});

13. How do you use the `afterEach` function in Jasmine?

The `afterEach` function is used to define teardown code that should be executed after each test case in a test suite. Here’s an example:

describe('Math operations', () => {
  afterEach(() => {
     // Cleanup code here
  });

  it('should add two numbers correctly', () => {
     // Test case here
  });
});

14. How do you use the `beforeAll` function in Jasmine?

The `beforeAll` function is used to define setup code that should be executed once before all the test cases in a test suite.

Here’s an example:

describe('Math operations', () => {
  beforeAll(() => {
     // Setup code here
  });
  it('should add two numbers correctly', () => {
     // Test case here
  });
});

15. How do you use the `afterAll` function in Jasmine?

The `afterAll` function is used to define teardown code that should be executed once after all the test cases in a test suite. Here’s an example:

describe('Math operations', () => {
  afterAll(() => {
    // Teardown code here
  });

  it('should add two numbers correctly', () => {
     // Test case here
  });
});

16. How do you use spies in Jasmine?

See also  JavaScript Object Methods With Examples

Spies in Jasmine are used to track function calls, return values, and more.

You can create a spy using the `jasmine.createSpy()` function. Here’s an example:

const spy = jasmine.createSpy('spy');

17. How do you use the `toHaveBeenCalled` matcher with spies in Jasmine?

    – The `toHaveBeenCalled` matcher can be used to check if a spy has been called. Here’s an example:

expect(spy).toHaveBeenCalled();

18. How do you use the `toHaveBeenCalledWith` matcher with spies in Jasmine?

The `toHaveBeenCalledWith` matcher can be used to check if a spy has been called with specific arguments. Here’s an example:

expect(spy).toHaveBeenCalledWith(arg1, arg2);

19. How do you use the `and.callThrough` feature with spies in Jasmine?

The `and.callThrough` feature allows a spy to invoke the original function it is spying on. Here’s an example:

spyOn(obj, 'method').and.callThrough();

20. How do you use the `and.returnValue` feature with spies in Jasmine?

The `and.returnValue` feature allows a spy to return a specific value. Here’s an example:

spyOn(obj, 'method').and.returnValue(value);

21. How do you use the `and.callFake` feature with spies in Jasmine?

The `and.callFake` feature allows a spy to call a custom function when invoked. Here’s an example:

spyOn(obj, 'method').and.callFake(() => {
   // Custom implementation here
});

22. How do you use the `and.throwError` feature with spies in Jasmine?

    – The `and.throwError` feature allows a spy to throw an error when invoked. Here’s an example:

spyOn(obj, 'method').and.throwError('Error message');

23. How do you use the `jasmine.any` matcher in Jasmine?

    – The `jasmine.any` matcher can be used to check if a value is of a specific type. Here’s an example:

expect(value).toEqual(jasmine.any(Type));

24. How do you use the `jasmine.arrayContaining` matcher in Jasmine?

The `jasmine.arrayContaining` matcher can be used to check if an array contains specific elements. Here’s an example:

expect(array).toEqual(jasmine.arrayContaining(elements));

25. How do you use the `jasmine.objectContaining` matcher in Jasmine?

See also  Top 20 Scrum Master Interview Questions and Answers

The `jasmine.objectContaining` matcher can be used to check if an object contains specific properties. Here’s an example:

expect(obj).toEqual(jasmine.objectContaining(properties));

26. How do you use the `jasmine.stringMatching` matcher in Jasmine?

The `jasmine.stringMatching` matcher can be used to check if a string matches a specific pattern. Here’s an example:

expect(string).toMatch(jasmine.stringMatching(pattern));

27. How do you use the `jasmine.clock` object in Jasmine?

    – The `jasmine.clock` object allows you to control the timing of your tests. You can use `jasmine.clock().install()` to install the clock and `jasmine.clock().uninstall()` to uninstall it. Here’s an example:

      beforeEach(() => {
       jasmine.clock().install();

      });

      afterEach(() => {

        jasmine.clock().uninstall();

      });

      it('should test time-dependent code', () => {

        // Test case here

        jasmine.clock().tick(1000); // Advance the clock by 1000 milliseconds

      });

28. How do you use the `jasmine.anything` matcher in Jasmine?

The `jasmine.anything` matcher can be used to check if a value is anything but `null` or `undefined`. Here’s an example:

expect(value).toEqual(jasmine.anything());

29. How do you use the `jasmine.addMatchers` function in Jasmine?

The `jasmine.addMatchers` function allows you to define custom matchers. Here’s an example:

      beforeEach(() => {
       jasmine.addMatchers({

          toBeEven: () => {

            return {

              compare: (actual) => {

                const result = {

                  pass: actual % 2 === 0,

                  message: 'Expected ' + actual + ' to be even'

                };

                return result;

              }

            };

          }

        });

      });

      it('should test a custom matcher', () => {

        expect(4).toBeEven();

      });

30. How do you use the `jasmine.anything` matcher in Jasmine?

The `jasmine.anything` matcher can be used to check if a value is anything but `null` or `undefined`. Here’s an example:

expect(value).toEqual(jasmine.anything());

These are some of the top Jasmine JS interview questions with detailed answers and code examples. Remember to customize the code snippets and answers based on your specific project requirements and use cases.