21

What the HTML? - The Holy Grail

Embed Size (px)

DESCRIPTION

Cross-browser JavaScript Unit Testing with Code Coverage

Citation preview

Page 1: What the HTML? - The Holy Grail
Page 2: What the HTML? - The Holy Grail
Page 3: What the HTML? - The Holy Grail
Page 4: What the HTML? - The Holy Grail
Page 5: What the HTML? - The Holy Grail

Documents & Defines the expected input and output of your code

Makes it Easier to Refactor.

Helps you write Better, re-usable code.

Enables Automated testing.

Page 6: What the HTML? - The Holy Grail

Unit Testing requires you change the way you write your code.

(But this is good);

Page 7: What the HTML? - The Holy Grail

JavaScript is interpreted at runtime.

Across a variety of different browsers.

Mutable , Loosely-typed ,, Global scope.

SUDDEN DEATH Mode

Page 8: What the HTML? - The Holy Grail

Tests should::

● Run in a real browser environment

● Run in any & all browsers

● Integrate with our CI setup

● Output code coverage metrics

● Easy to write

● Be reliable, execute fast

Page 9: What the HTML? - The Holy Grail
Page 10: What the HTML? - The Holy Grail

http://karma-runner.github.io/.

Page 11: What the HTML? - The Holy Grail

http://pivotal.github.io/jasmine/.

Page 12: What the HTML? - The Holy Grail

1. Karma runs a server

2. Real-world browsers connect

3. Karma serves your tests

4. Browsers execute tests

5. Karma collates the output

Page 13: What the HTML? - The Holy Grail

> karma init karma.config.js

> karma start

Page 14: What the HTML? - The Holy Grail

● Tests written in JavaScript

● BDD syntax

● Anything you can do with JavaScript,you can test with JavaScript

Page 15: What the HTML? - The Holy Grail

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

Page 16: What the HTML? - The Holy Grail

● describe(name, function)

● it(name, function)

● beforeEach(function) / afterEach(function)

● expect(condition).toBe(value);

● expect(condition).not.toBe(value);

● .toEqual() / .toBeTruthy() / .toBeFalsy()

● waitsFor(function) / runs(function)

Writing tests in Jasmine

Page 17: What the HTML? - The Holy Grail

it('checks that the Quicknav control navigates to a page', function() {

loadFixtures('simple-fixture.html');

var activeTextInstance = new ActiveText(...);

waitsFor(function() { return activeTextInstance.ready; }, 500);

runs(function() {

var element = $('.quicknav input');

element.focus();

element.val("5");

var e = jQuery.Event("keydown");

e.which = ActiveText.Keymap.ENTER;

$(element).trigger(e);

e = jQuery.Event("keyup");

e.which = ActiveText.Keymap.ENTER;

$(element).trigger(e);

expect(element.val()).toBe("Pages 4–5 of 26");

expect(activeTextInstance.model.getCurrentIndex()).toBe(3);

expect(activeTextInstance.model.getCurrentPageNumber()).toBe(4);

});

});

Page 18: What the HTML? - The Holy Grail

Console Output;

Page 19: What the HTML? - The Holy Grail

CI Integration;

Page 20: What the HTML? - The Holy Grail

LCOV Output;

Page 21: What the HTML? - The Holy Grail