Testing / Jasmine Interview Questions
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.
Karma is a tool of running tests on browsers it lets us spawn browsers and run jasmine tests inside of them.
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!'); }); });
Jasmine is a JavaScript-based Behavior Driven Development (BDD) testing framework. It is not dependent on any browsers, the DOM, or JavaScript frameworks.
We can disable tests by using xit instead of 'it', xdescribe instead of 'describe' as shown below:
xdescribe('Hello', () => { (1) xit('says Hello', () => { (1) expect(Hello()) .toEqual('Hello!'); }); });
Karma is a tool of running tests on browsers it lets us spawn browsers and run jasmine tests inside of them.
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);
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.
describe('Hello world', () => { let expected = ""; beforeEach(() => { expected = "Hello World"; }); afterEach(() => { expected = ""; }); it('says hello', () => { expect(message()) .toEqual(expected); }); });
Protractor is an end-to-end test framework for Angular. It runs your tests inside a real browser, interacting with it as real person would. Unlike unit tests, where we test individual functions, here we test the entire logic. Protractor is able to fill in forms, click buttons and confirm that the expected data and styling is displayed in the HTML document.