93
Easy-Bake Testing Liz Baillie @infinite_math

Easy-Bake Testing - EmberConf 2016

Embed Size (px)

Citation preview

Page 1: Easy-Bake Testing - EmberConf 2016

Easy-Bake TestingLiz Baillie@infinite_math

Page 2: Easy-Bake Testing - EmberConf 2016

Last year...

Page 3: Easy-Bake Testing - EmberConf 2016

this year...

Page 4: Easy-Bake Testing - EmberConf 2016

What do i wish i knew a year ago?★ What do I have access to with Ember out of the box?

Page 5: Easy-Bake Testing - EmberConf 2016

What do i wish i knew a year ago?★ What do I have access to with Ember out of the box?

★ How do I use Ember’s built-in test helpers?

Page 6: Easy-Bake Testing - EmberConf 2016

What do i wish i knew a year ago?★ What do I have access to with Ember out of the box?

★ How do I use Ember’s built-in test helpers?

★ What’s the difference between unit, acceptance, and integration?

Page 7: Easy-Bake Testing - EmberConf 2016

What do i wish i knew a year ago?★ What do I have access to with Ember out of the box?

★ How do I use Ember’s built-in test helpers?

★ What’s the difference between unit, acceptance, and integration?

★ What’s the deal with all these other frameworks and libraries?

Page 8: Easy-Bake Testing - EmberConf 2016

What do i wish i knew a year ago?★ What do I have access to with Ember out of the box?

★ How do I use Ember’s built-in test helpers?

★ What’s the difference between unit, acceptance, and integration?

★ What’s the deal with all these other frameworks and libraries?

★ What are mocks, stubs, and spies? Do I need them?

Page 9: Easy-Bake Testing - EmberConf 2016

What do i wish i knew a year ago?★ What do I have access to with Ember out of the box?

★ How do I use Ember’s built-in test helpers?

★ What’s the difference between unit, acceptance, and integration?

★ What’s the deal with all these other frameworks and libraries?

★ What are mocks, stubs, and spies? Do I need them?

★ How can I mock API responses?

Page 10: Easy-Bake Testing - EmberConf 2016

What do i wish i knew a year ago?★ What do I have access to with Ember out of the box?

★ How do I use Ember’s built-in test helpers?

★ What’s the difference between unit, acceptance, and integration?

★ What’s the deal with all these other frameworks and libraries?

★ What are mocks, stubs, and spies? Do I need them?

★ How can I mock API responses?

★ How do I deal with async in my tests?

Page 11: Easy-Bake Testing - EmberConf 2016

What do i wish i knew a year ago?★ What do I have access to with Ember out of the box?

★ How do I use Ember’s built-in test helpers?

★ What’s the difference between unit, acceptance, and integration?

★ What’s the deal with all these other frameworks and libraries?

★ What are mocks, stubs, and spies? Do I need them?

★ How can I mock API responses?

★ How do I deal with async in my tests?

★ What are some best practices when it comes to testing?

Page 12: Easy-Bake Testing - EmberConf 2016

What do i get without even trying?★ Qunit: a JavaScript testing framework

○ Default testing framework

○ Includes everything QUnit has

■ assertions, async control, and other helpers

Page 13: Easy-Bake Testing - EmberConf 2016

What do i get without even trying?

★ Ember Test Helpers

○ Built-in methods to automate common tasks in acceptance tests

Page 14: Easy-Bake Testing - EmberConf 2016

What do i get without even trying?★ Testem

○ Runs your tests!

Page 15: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Async

Page 16: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Async

○ Are aware of and ‘wait for’ async behavior

Page 17: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Async

○ Are aware of and ‘wait for’ async behavior

○ Are run in order

Page 18: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Async

○ Are aware of and ‘wait for’ async behavior

○ Are run in order

■ click(selector)

Page 19: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Async

○ Are aware of and ‘wait for’ async behavior

○ Are run in order

■ click(selector)

■ fillIn(selector, value)

Page 23: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Sync

Page 24: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Sync

○ Are performed immediately when triggered

Page 25: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Sync

○ Are performed immediately when triggered

■ currentPath()

Page 29: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Wait

Page 30: Easy-Bake Testing - EmberConf 2016

Ember acceptance test helpers★ Wait

○ andThen() - waits for all preceding async helpers to finish

before executing code wrapped within

Page 31: Easy-Bake Testing - EmberConf 2016
Page 32: Easy-Bake Testing - EmberConf 2016

Unit - integration - acceptance★ Unit

○ Self-contained

○ Only testing the code itself

○ NOT user interaction, testing only a particular function or

other unit of code

Page 33: Easy-Bake Testing - EmberConf 2016
Page 34: Easy-Bake Testing - EmberConf 2016
Page 35: Easy-Bake Testing - EmberConf 2016

Unit - integration - acceptance★ Integration / Component

○ Test how one layer of your application interacts with

another, usually how components interact with each other or

with other templates

○ Component tests are integration tests!

Page 36: Easy-Bake Testing - EmberConf 2016
Page 37: Easy-Bake Testing - EmberConf 2016
Page 38: Easy-Bake Testing - EmberConf 2016

Unit - integration - acceptance★ Acceptance

○ “I accept that my app has actual users who will actually

interact with it, so I’d better test for that!”

Page 39: Easy-Bake Testing - EmberConf 2016
Page 40: Easy-Bake Testing - EmberConf 2016
Page 41: Easy-Bake Testing - EmberConf 2016

mocks - spies - stubs★ Help you write unit tests in isolation

Page 42: Easy-Bake Testing - EmberConf 2016

mocks - spies - stubs★ Help you write unit tests in isolation

★ Probably won’t come up very often in Ember, but good to be aware

of

Page 43: Easy-Bake Testing - EmberConf 2016

mocks - spies - stubs★ Help you write unit tests in isolation

★ Probably won’t come up very often in Ember, but good to be aware

of

★ Sinon.js is a great library that provides mocks, stubs, spies,

and more

Page 44: Easy-Bake Testing - EmberConf 2016

mocks - spies - stubs★ Help you write unit tests in isolation

★ Probably won’t come up very often in Ember, but good to be aware

of

★ Sinon.js is a great library that provides mocks, stubs, spies,

and more

★ 2 highly-rated Ember Sinon addons are ember-sinon and ember-

sinon-qunit

Page 45: Easy-Bake Testing - EmberConf 2016

mocks - spies - stubs★ Help you write unit tests in isolation

★ Probably won’t come up very often in Ember, but good to be aware

of

★ Sinon.js is a great library that provides mocks, stubs, spies,

and more

★ 2 highly-rated Ember Sinon addons are ember-sinon and ember-

sinon-qunit★ https://spin.atomicobject.com/2015/05/18/sinon-js-ember-cli-testing/

Page 46: Easy-Bake Testing - EmberConf 2016

mocks

★ Also known as ‘test doubles’

★ Fake methods with pre-programmed behavior and pre-preprogrammed

expectations

Page 47: Easy-Bake Testing - EmberConf 2016

Spies★ A function that records arguments, returns the value, as well as

the value of ‘this’ and exception thrown (if any) for all its calls

★ Good for seeing how many times a particular function was called or how a function handles a specific callback

Page 48: Easy-Bake Testing - EmberConf 2016

Stubs★ Stubs are spies!

★ Force code down a specific path

★ In the case of Sinon.js, there is a lot to explore, as stubs support the full spy API as well

Page 49: Easy-Bake Testing - EmberConf 2016

Mocking api responses★ Mirage is awesome and a super popular solution for this

Page 50: Easy-Bake Testing - EmberConf 2016

Mocking api responses★ Mirage is awesome and a super popular solution for this

★ Can be used in development as well as test environments

Page 51: Easy-Bake Testing - EmberConf 2016

Mocking api responses★ Mirage is awesome and a super popular solution for this

★ Can be used in development as well as test environments

★ Offers factories for use in testing

Page 52: Easy-Bake Testing - EmberConf 2016

Mocking api responses★ Mirage is awesome and a super popular solution for this

★ Can be used in development as well as test environments

★ Offers factories for use in testing

★ Can seed your test database and/or mock one-off responses in

individual tests

Page 53: Easy-Bake Testing - EmberConf 2016

Mocking api responses★ Mirage is awesome and a super popular solution for this

★ Can be used in development as well as test environments

★ Offers factories for use in testing

★ Can seed your test database and/or mock one-off responses in

individual tests

★ Sinon also offers mock API responses, but Mirage offers so much

more that it’s definitely worth looking into

Page 54: Easy-Bake Testing - EmberConf 2016
Page 55: Easy-Bake Testing - EmberConf 2016

Framework FOMO?

★ Jasmine

○ BDD / behavior-driven testing framework

Page 56: Easy-Bake Testing - EmberConf 2016

Framework FOMO?

★ Jasmine

○ BDD / behavior-driven testing framework

○ Does not require a DOM

Page 57: Easy-Bake Testing - EmberConf 2016

Framework FOMO?

★ Jasmine

○ BDD / behavior-driven testing framework

○ Does not require a DOM

○ Does not depend on any other frameworks

Page 58: Easy-Bake Testing - EmberConf 2016

Framework FOMO?

★ Jasmine

○ BDD / behavior-driven testing framework

○ Does not require a DOM

○ Does not depend on any other frameworks

○ Similar in syntax to RSpec, so may be good for people coming

over from a Rails background

Page 59: Easy-Bake Testing - EmberConf 2016

Framework FOMO?★ Chai

○ An assertion library (NOT a framework)

Page 60: Easy-Bake Testing - EmberConf 2016

Framework FOMO?★ Chai

○ An assertion library (NOT a framework)

○ People seem to really like having a ton of different

assertions

Page 61: Easy-Bake Testing - EmberConf 2016

Framework FOMO?★ Chai

○ An assertion library (NOT a framework)

○ People seem to really like having a ton of different

assertions

○ Apparently very difficult to implement in Ember with QUnit

Page 62: Easy-Bake Testing - EmberConf 2016

Framework FOMO?★ Chai

○ An assertion library (NOT a framework)

○ People seem to really like having a ton of different

assertions

○ Apparently very difficult to implement in Ember with QUnit

○ A few addons are out there, but there doesn’t seem to be one

obviously best solution

Page 63: Easy-Bake Testing - EmberConf 2016

Framework FOMO?★ Mocha

○ Another testing framework (so you would probably use this in

place of QUnit)

Page 64: Easy-Bake Testing - EmberConf 2016

Framework FOMO?★ Mocha

○ Another testing framework (so you would probably use this in

place of QUnit)

○ Can use any assertion library you want (like Chai)

Page 65: Easy-Bake Testing - EmberConf 2016

Framework FOMO?★ Mocha

○ Another testing framework (so you would probably use this in

place of QUnit)

○ Can use any assertion library you want (like Chai)

○ Can’t use fat arrow? :(

■ Their lexical binding of the ‘this’ value makes them unable to access the Mocha context, and statements like this.timeout(1000); will not work inside an arrow function

Page 66: Easy-Bake Testing - EmberConf 2016

Framework FOMO?

★ Selenium

○ For end-to-end testing

Page 67: Easy-Bake Testing - EmberConf 2016

Framework FOMO?

★ Selenium

○ For end-to-end testing

○ Drives browser in application environment rather than test

environment

Page 68: Easy-Bake Testing - EmberConf 2016

Framework FOMO?

★ Selenium

○ For end-to-end testing

○ Drives browser in application environment rather than test

environment

○ Difficult to hook up to Ember?

Page 69: Easy-Bake Testing - EmberConf 2016

Framework FOMO?

★ Selenium

○ For end-to-end testing

○ Drives browser in application environment rather than test

environment

○ Difficult to hook up to Ember?

○ Much slower to run tests

Page 70: Easy-Bake Testing - EmberConf 2016

The Verdict?

★ QUnit is great! I have yet to come upon a situation in which I

really need a ton of extra assertions or am unsatisfied with what

QUnit offers me

Page 71: Easy-Bake Testing - EmberConf 2016

TDD vs. Everything Else★ TDD = Test-Driven Development

Page 72: Easy-Bake Testing - EmberConf 2016

TDD vs. Everything Else★ TDD = Test-Driven Development

○ Write a failing test for something that doesn’t exist yet

Page 73: Easy-Bake Testing - EmberConf 2016

TDD vs. Everything Else★ TDD = Test-Driven Development

○ Write a failing test for something that doesn’t exist yet

○ Build the thing to make the test pass

Page 74: Easy-Bake Testing - EmberConf 2016

TDD vs. Everything Else★ TDD = Test-Driven Development

○ Write a failing test for something that doesn’t exist yet

○ Build the thing to make the test pass

○ Write another test

Page 75: Easy-Bake Testing - EmberConf 2016

TDD vs. Everything Else★ TDD = Test-Driven Development

○ Write a failing test for something that doesn’t exist yet

○ Build the thing to make the test pass

○ Write another test

○ Rinse, repeat!

Page 76: Easy-Bake Testing - EmberConf 2016

TDD vs. Everything Else★ TDD = Test-Driven Development

○ Write a failing test for something that doesn’t exist yet

○ Build the thing to make the test pass

○ Write another test

○ Rinse, repeat!

★ Great way to ensure as complete test coverage as possible

Page 77: Easy-Bake Testing - EmberConf 2016

TDD vs. Everything Else★ TDD = Test-Driven Development

○ Write a failing test for something that doesn’t exist yet

○ Build the thing to make the test pass

○ Write another test

○ Rinse, repeat!

★ Great way to ensure as complete test coverage as possible

★ Can be very difficult for beginners, as beginners usually don’t

know exactly what they’re expecting until they build it

Page 78: Easy-Bake Testing - EmberConf 2016

Page Objects★ UI changes can make your tests brittle if they’re frequently

dependent on specifically-named selectors

Page 79: Easy-Bake Testing - EmberConf 2016

Page Objects★ UI changes can make your tests brittle if they’re frequently

dependent on specifically-named selectors

★ Encapsulate the structure of your page within an object

Page 80: Easy-Bake Testing - EmberConf 2016

Page Objects★ UI changes can make your tests brittle if they’re frequently

dependent on specifically-named selectors

★ Encapsulate the structure of your page within an object

★ Keep your tests DRY as well as keep the intention of your tests

clear

Page 81: Easy-Bake Testing - EmberConf 2016

Page Objects★ UI changes can make your tests brittle if they’re frequently

dependent on specifically-named selectors

★ Encapsulate the structure of your page within an object

★ Keep your tests DRY as well as keep the intention of your tests

clear

★ Well-supported Ember addon: http://ember-cli-page-object.js.org

Page 82: Easy-Bake Testing - EmberConf 2016

Page Objects★ UI changes can make your tests brittle if they’re frequently

dependent on specifically-named selectors

★ Encapsulate the structure of your page within an object

★ Keep your tests DRY as well as keep the intention of your tests

clear

★ Well-supported Ember addon: http://ember-cli-page-object.js.org

★ http://martinfowler.com/bliki/PageObject.html

Page 83: Easy-Bake Testing - EmberConf 2016
Page 84: Easy-Bake Testing - EmberConf 2016
Page 85: Easy-Bake Testing - EmberConf 2016
Page 86: Easy-Bake Testing - EmberConf 2016

Best testing practices★ Keep your test code DRY by writing test helpers

Page 87: Easy-Bake Testing - EmberConf 2016

Best testing practices★ Keep your test code DRY by writing test helpers

★ Protect your tests from UI changes and keep their intention clear

with page objects

Page 88: Easy-Bake Testing - EmberConf 2016

Best testing practices★ Keep your test code DRY by writing test helpers

★ Protect your tests from UI changes and keep their intention clear

with page objects

★ Keep each test discrete in its function

Page 89: Easy-Bake Testing - EmberConf 2016

Best testing practices★ Keep your test code DRY by writing test helpers

★ Protect your tests from UI changes and keep their intention clear

with page objects

★ Keep each test discrete in its function

★ Most tests should be acceptance, some integration, least of all

unit

Page 90: Easy-Bake Testing - EmberConf 2016

Best testing practices★ Keep your test code DRY by writing test helpers

★ Protect your tests from UI changes and keep their intention clear

with page objects

★ Keep each test discrete in its function

★ Most tests should be acceptance, some integration, least of all

unit

★ Testing styles such as TDD are a matter of personal preference

and aren’t better or worse than any other

Page 91: Easy-Bake Testing - EmberConf 2016

Resources★ Ember Guides for details on test helpers

○ https://guides.emberjs.com/v2.4.0/testing/acceptance/

★ Qunit Docs - http://api.qunitjs.com/

★ Global Ember Meetup: Intro to Testing

○ https://vimeo.com/146960505

★ Sinon for mocks, stubs, & spies - http://sinonjs.org/

★ Mirage for mock API responses - http://www.ember-cli-mirage.com/

★ For more on mocks, stubs, & spies - http://gaboesquivel.

com/blog/2014/unit-testing-mocks-stubs-and-spies/

Page 92: Easy-Bake Testing - EmberConf 2016

Thanks!

Page 93: Easy-Bake Testing - EmberConf 2016

Liz Baillie @infinite_math