Top Banner
Javascript Testing Tools of the trade
21

Javascript testing: tools of the trade

Apr 12, 2017

Download

Software

Juanma Orta
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Javascript testing: tools of the trade

Javascript Testing

Tools of the trade

Page 2: Javascript testing: tools of the trade

Juanma OrtaFront/Back end developer

at Ulabox.com

@JuanmaOrta

Page 3: Javascript testing: tools of the trade

The Tools

Page 4: Javascript testing: tools of the trade

JasmineThe testing framework

Page 5: Javascript testing: tools of the trade

KarmaThe test runner

Page 6: Javascript testing: tools of the trade

ĥThe headless browser

Page 7: Javascript testing: tools of the trade

ProtractorThe E2E framework

Page 8: Javascript testing: tools of the trade

Jasmine

Suite and spec based testing framework. Allows dependency mocking and custom matchers.

Page 9: Javascript testing: tools of the trade

The suite

(function (rps) {

‘use strict’;

describe(‘when playing rock-paper-scissors’, function() {

});

}(RockPaperScissors)); // the module under test

Page 10: Javascript testing: tools of the trade

Setup and tear downdescribe(‘when playing rock-paper-scissors’, function() {

var foo = ‘’;

beforeEach(function() {

foo = ‘bar’;

});

afterEach(function() {

foo = ‘’;

});

});

Page 11: Javascript testing: tools of the trade

The specification (spec)

describe(‘when playing rock-paper-scissors’, function() {

it(‘will show that rock wins scissors’, function() {

var result = rps.whoWins(‘rock’, ‘scissors’);

expect(result).toEqual(1);

})

});

Page 12: Javascript testing: tools of the trade

“Suites and specs are just plain JS functions”

- From an expert

Page 13: Javascript testing: tools of the trade

Mocking dependencies

it(‘will show that rock wins scissors’, function() {

var choice = ‘rock’;

spyOn(rps, ‘validate’).and.callFake(function() { // Jasmine 2.0 syntax

return choice;

});

spyOn(rps, ‘whoWins’).andCallFake(function() { // Jasmine 1.x syntax

$(document).trigger(‘score:user1’);

});

Page 14: Javascript testing: tools of the trade

Matchers

Common matchers

expect(true).toBe(true);

expect(true).not.toBe(fase);

.toEqual

...

jQuery matchers

Using jasmine-jquery module

expect($(‘#info’)).toBeVisible();

expect($(‘#info’)).toHaveClass();

expect($(‘#myField’)).toHaveValue(‘123’);

Page 15: Javascript testing: tools of the trade

Custom Matchers

var customMatchers = {

toBeActive: function() {

return {

compare: function(item) {

...

}

}

}

};

beforeEach(function() {

jasmine.addMatchers(customMatchers);

});

expect($(‘#element’)).toBeActive();

expect($(‘#element’)).not.toBeActive();

Page 16: Javascript testing: tools of the trade

Enter the Karma

“Karma is essentially a tool which spawns a web server that executes source code against test code for each of the

browsers connected. The results for each test against each browser are examined and displayed via the command line to

the developer such that they can see which browsers and tests passed or failed.”

$ karma start

Page 17: Javascript testing: tools of the trade

The browsers

● Google Chrome

● Mozilla Firefox

● Safari (only on OS X)

● Internet Explorer (yes, IE too, but only on Win machines)

and

● Phantom JS -> the headless one

Page 18: Javascript testing: tools of the trade

Protractor: End to end testing (e2e)

Designed basically for AngularJS e2e testing,

runs tests in a real browser.

Simulates user interactions

Can use any test framework,

but works smoothly with Jasmine

It can also be used to test

non-angularjs applications

Page 19: Javascript testing: tools of the trade

Example protractor test

beforeEach(function () {

browser.ignoreSynchronization = true;

browser.get(‘http://localhost:9000’);

});

it(‘should show …’, function() {

element(by.id(‘myChoice’))

.sendKeys(‘stone’);

element(by.css(‘.btn’)).click();

alert = element.all(by.css(‘.alert’))

expect(alert.count()).toBe(1);

});

Page 20: Javascript testing: tools of the trade

Resources

The repo of this tests and exercises:

https://github.com/juanmaorta/rockpaperscissors.git

Jasmine: http://jasmine.github.io/

Karma: http://karma-runner.github.io/0.13/index.html

Protractor: https://angular.github.io/protractor/#/

Page 21: Javascript testing: tools of the trade

Thanks!Contact me:

Juanma OrtaUlabox.com

[email protected]@JuanmaOrta