Skip to content

Instantly share code, notes, and snippets.

@RyanParsley
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save RyanParsley/e9733f8d472ae7778346 to your computer and use it in GitHub Desktop.

Select an option

Save RyanParsley/e9733f8d472ae7778346 to your computer and use it in GitHub Desktop.
A brief introduction to testing javascript.

name: Testing 101 class: middle, center

Testing 101

Mar 10, 2015

Unit Testing

  • Test small bits of code
    • Run javascript without involving the whole app
    • Faster to run
    • Great for adding to a (grunt) watch task
  • Written in Jasmine
it('should return the proper value for functionName', function(){
  expect(scope.functionName('argument')).toBe('value');
});

E2E (Integration?) Testing

  • Test app as a user interacting with a browser
  • Dependancies are automatic as you're just hitting a url
  • Sure beats clicking links over and over in different browsers
  • Run via Web Driver (aka Selenium) or Protractor (adds angular magic to Web Driver)
  • Also can be written in Jasmine
it('should have an h1 of proper size', function() {
  browser.get('http://yoursite.whatever');
  expect(element(by.binding('boundData')).getCssValue('fontSize')).toBe('21px');
});

Visual

  • Automatically generated screenshots compared to each other
  • easy to define
  • fast to run
  • PhantomCSS runs on PhantomJS enhanced with CasperJS
  • Screenshots are named and compared
casper.
  start( 'http://yoursite.whatever' ).
  then(function(){
    casper.viewport(1024, 768);
    phantomcss.screenshot('.btn-cta', 'Desktop: Call to action');
  });

Cucumber

  • A dialect of English used to describe desired behavior.
  • Can be used as a cross team way to describe features
  • Step definitions look a bit messy to me
Feature: whatever layout test
As a user I want visual consistency on the http://whatever.dev website

  Scenario: Contact form
    Given I visit "http://whatever.dev"
    Then "line of copy" should have "font-size" of "14px"
    Then "line of copy" should have "color" of "rgba(97, 97, 97, 1)"

    Then "label, .label" should have "font-size" of "12px"
    Then "label, .label" should have "color" of "rgba(97, 97, 97, 1)"

    Given the window size is "1400" by "768"

Unit Test Demo

I made a small app to show how little you need to configure to start using jasmine.


E2E (Integration) Test Demo

A simple app to demonstrate using protractor to test a page without angular.


Jenkins

  • On git push, deploy code from a given branch if tests pass
  • Store git hashes with test results and build status
  • Slack push notifications

Recap

  • Protractor is awesome!
    • Finding elements via bindings makes testing the frontend far less brittle
    • No dependencies to fuss with
  • Unit testing is worth the effort
    • Most friction I found had to do with RequireJS
    • Great
  • I'm not convinced that visual diffs are worth the effort
    • They're brittle
    • The test runner is fussy compared to the other tools
    • PhantomJS does not render exactly like chrome

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment