+ - 0:00:00
Notes for current slide
Notes for next slide
  • We're not hiring (but you can contribute!)

It Takes a Village to Raise an App

How to Get Started Writing Tests Right Now

Jon Johnson

Jon's avatar

jrjohnson jrjohnson_

1 / 19

Who am I?

  • Technical lead for the Ilios Project open source curriculum management system for health science education. (https://github.com/ilios/frontend)
  • Author of the ember-noscript addon
  • Minor contributor to:
    • ember-cli
    • ember-try
    • ember-cli-blanket
    • heroku-buildpack-ember-cli
    • coveralls javascript-test-reporter
  • Working with ember for about a year
    • aka: I have no idea what sprout core is
2 / 19
  • We're not hiring (but you can contribute!)

What we will talk about?

  • coverage (ember-cli-blanket)
  • data fixtures (ember-cli-mirage)
  • multiple browsers (ember-cli-sauce)
  • future versions (ember-try)
  • human testing (heroku deploy)
3 / 19
  • Why am I standing up here?
    • We're currently at 83% code coverage!
    • We all know we should be testing, but getting started is a pain
    • A lot of that pain goes away when you have good infrastructure
    • Conference Driven Development

Our Personal Yak Shaver Ember CLI

bugs bunny shaving not a yak

$ ember new sandbox
version: 1.13.1
installing app
...
create testem.json
create tests/.jshintrc
create tests/helpers/resolver.js
create tests/helpers/start-app.js
create tests/index.html
create tests/test-helper.js
create tests/unit/.gitkeep
4 / 19
  • Test structure imposed by ember-cli takes a lot of bike shedding off the table
  • Addons take us even further, not just not reinventing the wheel, but providing the self driving car.

Ember tests in the browser

$ember server

sample test coverage

5 / 19
  • great for TDD
  • Can stop tests use pauseTest() and look at app state

Always generate with ember-cli

$ ember generate component playground-sandbox
version: 1.13.1
installing component
create app/components/playground-sandbox.js
create app/templates/components/playground-sandbox.hbs
installing component-test
create tests/integration/components/playground-sandbox-test.js

Congrats you now have a test!

6 / 19
  • generating creates test structure even if you don't use it
  • gives the next developer something to work from

Test coverage with ember-cli-blanket

OR

How to shame your team by adding coverage stats to pull requests

7 / 19

Adding coverage as simple as...

$ember install ember-cli-blanket
$ember server

blanket.js coverage

8 / 19

Generating statistics

$ember test
$cat coverage.json
{
"coverage": {
"total": {
"statementsTotal": 46,
"statementsCovered": 32,
"percentage": 69.57
},
"files": [
{
"name": "playground/router",
"statementsTotal": 5,
"statementsCovered": 5,
"percentage": 100
},
{
"name": "playground/components/playground-sandbox",
"statementsTotal": 3,
"statementsCovered": 3,
"percentage": 100
},
]
}
9 / 19
  • You can also generate lcov files for use in something like code climate
  • The file name is suposed to be a path, but it is acutally a broccoli module name to deal with this blanket can rename the modules to paths until a better long term solution is found.

Data for tests

Test data is usually one of:

  • Fixtures
  • Factories
  • Real Data

We use it in:

  • Development
  • Click around testing
  • Acceptance Tests
10 / 19

A few of the things we've tried for data

  • POJO's in Route::model()
  • Ember-cli Mocks
  • Pretender

and.....

11 / 19
  • POJO is inflexiable and doesn't handle updates well
  • CLI mocks require writing a lot of boiler plate
  • CLI mocks don't reset between test runs so you can't depend on constant data
  • Pretender can be hard to setup and takes a lot of work to share common data for tests

Let mirage handle your data

$ember install ember-cli-mirage
  • Sits on top of trek/pretender to organize test data
  • Replaces custom express configuration created with ember generate mock
  • Handles data for automated and manual testing through factories and fixtures
  • Has an internal database concept that makes it easier to write portable data handers
12 / 19

Add data routes

Setup a route for each of your API endpoints

app/mirage/config.js

export default function() {
this.get('/api/toys');
this.get('/api/toys/:id');
this.post('/api/toys');
this.put('/api/toys/:id');
this.del('/api/toys/:id');
}
13 / 19

Use factories to define data types

app/mirage/factories/tool.js

import Mirage, {faker} from 'ember-cli-mirage';
export default Mirage.Factory.extend({
name: function(i) {
return 'tool ' + i + '';
},
type: faker.list.random('Hammer', 'Wrench'),
weight: faker.list.cycle(1, 8, 11, 42),
color: faker.commerce.color,
});
14 / 19

Build data on demand

server becomes a global which we can call from acceptance tests

test('List all the tools', function() {
//create a whole bunch of tools
server.createList('tool', 10);
//create a single tool overriding the factory properties
server.create('tool', {
name: 'green hammer',
type: 'Hammer',
color: 'green'
});
visit('/tools');
andThen(function() {
equal( find('.tool').length, 11 );
equal( find('.tool').eq(10).text(), 'green hammer');
});
});
15 / 19

Browser testing made convenient

$ember install ember-cli-sauce
$ember sauce --browser 'chrome'
$ember test --launch='Chrome40'
ok 1 Chrome 40.0 - JSHint - .: app.js
...
# tests 12
# pass 11
# fail 1
$ember sauce:disconnect
16 / 19
$export SAUCE_USERNAME=your_user_name
$export SAUCE_ACCESS_KEY=your_access_key
$ember sauce:connect

Testing for the future

$ember install ember-try
$ember try ember-beta serve
17 / 19
  • You now have no excuse for not testing your app against beta Ember releases and reporting any bugs you find.

Sometimes you just need Dave

<div>
<label>{{t "general.end"}}:</label>
<div>{{inplace-date value=course.endDate save='changeStartDate'}}</div>
</div>

Deploy your app to heroku

$heroku create --buildpack https://github.com/tonycoco/heroku-buildpack-ember-cli.git
...
https://stormy-lake-4610.herokuapp.com/
$heroku config:set EMBER_ENV=development
$git push heroku master
#npm install; bower install; ember build --env=development

Heroku Review Apps (beta)

18 / 19

Questions?

Jon's avatar

jrjohnson jrjohnson_

19 / 19

Who am I?

  • Technical lead for the Ilios Project open source curriculum management system for health science education. (https://github.com/ilios/frontend)
  • Author of the ember-noscript addon
  • Minor contributor to:
    • ember-cli
    • ember-try
    • ember-cli-blanket
    • heroku-buildpack-ember-cli
    • coveralls javascript-test-reporter
  • Working with ember for about a year
    • aka: I have no idea what sprout core is
2 / 19
  • We're not hiring (but you can contribute!)
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow