Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cognac.js - Testing on the Rocks

miksago
October 12, 2011

Cognac.js - Testing on the Rocks

An introduction to the current problems with JavaScript Testing, and a looking at trying to address them.

miksago

October 12, 2011
Tweet

More Decks by miksago

Other Decks in Programming

Transcript

  1. • We provide WebSockets as a Service • We’re currently

    hiring (talk to me) Wednesday, 12 October 2011
  2. • At Pusher we have a heap of asynchronous javascript

    code • We must test this code to ensure it works • There are a heap of testing frameworks • So we tried a few... Wednesday, 12 October 2011
  3. The two core issues with current JavaScript testing frameworks •

    Everything is in a shared “scope” • They are designed for synchronous code Wednesday, 12 October 2011
  4. • QUnit • DOH (Dojo Objective Harness) • YUI Test

    / JUTE The ones we tried: Wednesday, 12 October 2011
  5. QUnit • Maintained by jQuery team • Use in jQuery

    test suite (~75k assertions) • Has been around for quite a while Wednesday, 12 October 2011
  6. however: Everything is in a shared scope Everything is assumed

    to be synchronous Wednesday, 12 October 2011
  7. An Example: (QUnit) module("core"); test("Basic requirements", function() { expect(3); ok(

    Array.prototype.push, "Array.push()" ); ok( RegExp, "RegExp" ); ok( jQuery, "jQuery" ); }); Wednesday, 12 October 2011
  8. An Asynchronous Example: function checkSomething(){ ok(true, "This test actually ran");

    start(); } test("Set Timeout Test", function() { stop(); setTimeout(function(){ checkSomething(); }, 1000) }); Wednesday, 12 October 2011
  9. JUTE - YUI Test • Released Aug 11 (a few

    days ago!) • Builds on top of YUI Test • Claims to: “kick your Javascript Unit Testing up to the next level” Wednesday, 12 October 2011
  10. The issue with tests sharing a single “scope” • There

    is no tight coupling between test cases and assertions • This makes debugging tests hard • This issue is compounded when testing asynchronous code Wednesday, 12 October 2011
  11. A biased testing framework • Node is asynchronous, so tests

    should be too • You should be able to run tests in parallel or serially • You shouldn't have to learn new assertion functions • No specifications, and no natural language suites • Test files should be executable by Node Wednesday, 12 October 2011
  12. Introducing: Cognac cognacc㷦L⒕O㷧ZBL㷦LÅO㷦LÔOc noun 1) a high-quality brandy, that is

    distilled in Cognac in western France. Wednesday, 12 October 2011
  13. Introducing: Cognac cognacc㷦L⒕O㷧ZBL㷦LÅO㷦LÔOc noun 1) a high-quality brandy, that is

    distilled in Cognac in western France. 2) a testing framework for Asynchronous JavaScript Wednesday, 12 October 2011
  14. Core features • Forces tests to be asynchronous • Everything

    is tightly scoped to it’s owner • Can work both client-side and server-side Wednesday, 12 October 2011
  15. An Example: runner.addSuite('Pusher.Connection', { 'Initialisation': function(test) { var c =

    Pusher.Connection('foo'); test.equal(c.key, 'foo', 'Key should be stored'); // The important line: test.finish(); } }); Wednesday, 12 October 2011
  16. Thanks for listening! @miksago http://github.com/miksago Talk to me for a

    Pusher t-shirt, I’ve only got three tonight! Wednesday, 12 October 2011