adam klein - js tests like a pro | codemotion milan 2015

30
MILAN 20/21.11.2015 Adam Klein - 500Tech JS tests like a pro creativecommons.org/licenses/by-nc-sa/3.0/

Upload: codemotion

Post on 16-Jan-2017

522 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

MILAN 20/21.11.2015

Adam Klein - 500Tech

JS tests like a pro

creativecommons.org/licenses/by-nc-sa/3.0/

Page 2: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

MILAN 20/21.11.2015 - Adam Klein

Leave your feedback on Joind.in! https://joind.in/16318

Page 3: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

Adam Klein

- Developing for >18 years - Development, Consulting & Training

in Angular, React & Node - International clients

CTO @ 500Tech

Page 4: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa

Page 5: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

MEMES

Page 6: Adam Klein - JS tests like a PRO | Codemotion Milan 2015
Page 7: Adam Klein - JS tests like a PRO | Codemotion Milan 2015
Page 8: Adam Klein - JS tests like a PRO | Codemotion Milan 2015
Page 9: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

DEMO APP

Page 10: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

The projecthttp://localhost:8080

https://github.com/500tech/js-tests-pro

Page 11: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

Tools๏ webpack

๏ karma

๏ jasmine

๏ ES6

๏ angular

๏ angular-kick

Page 12: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

TESTABLE CODE

Page 13: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

import _ from 'lodash' class TabsController { constructor(tabs, onActivate) { this.tabs = tabs; this._onActivate = onActivate; } isActive(tab) { return this._activeTab === tab; } activate(tab) { this._activeTab = tab; this._onActivate(tab); } }

Unitinitialisation

dependencies

internal state

publicAPI

side effects

Page 14: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

WHAT TO TEST?

Page 15: Adam Klein - JS tests like a PRO | Codemotion Milan 2015
Page 16: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

Models & Services- Little dependencies

- Not coupled to view

- Framework agnostic

- Easiest to test

Page 17: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

Controllers- Coupled to the view

- Many dependencies

- Might be heavily reused

- Change a lot

Page 18: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

Integration tests- Using Selenium

- Very slow

- Very reliable

- Hard to write and maintain

Page 19: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

UI Components- unit tests using DOM API

- Unit tests are faster

Page 20: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

Floating code- router resolve

- run / config blocks

- super objects / main controllers

- inside event listeners

Page 21: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

TOOLS

Page 22: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

jasmine / mocha / chaidescribe('some action', function() { beforeEach(function() { given = something(); spyOn(http, 'get'); result = action(given); }); it ('should return something', function() { expect(result).toBeGreaterThan(0); }); it ('should have side effects', function() { expect(http.get).toHaveBeenCalled(); }); });

Page 23: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

karma๏ Test Runner

๏ Tests run in the browser

Page 24: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

ES6 - babel + webpack / browserify

๏ arrow functions

๏ classes

๏ import / export

๏ lots more features

Page 25: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

Saucelabs / Browserstack๏ Different browsers

๏ Different OS

๏ Different resolutions

Page 26: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

TEST SERVICE

Page 27: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

// TODO:

Page 28: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

TEST UI COMPONENT

Page 29: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

Questions?

Page 30: Adam Klein - JS tests like a PRO | Codemotion Milan 2015

MILAN 20/21.11.2015

Keep in touch

[email protected] blog.500Tech.com @500techil

https://www.slideshare.net/500Tech/js-tests-like-a-pro