jQuery Style BDD

I have been warming up to the jQuery style of using method chaining. Then I thought: why are most unit test frameworks class based? There's no reason why we can't do it in the jQuery style...
Something like this:
  $.describe('Bowling')
    .beforeEach(function(){
      this.bowling = new Bowling();
    })
    .should('score 0 for gutter game', function(){
      for (var i = 0; i < 20; i++) this.bowling.hit(i);
      this.bowling.score.shouldEqual(0);
    })
    .should('accumulate score', function(){
      this.bowling.hit(20);
      this.bowling.hit(10);
      this.bowling.score.shouldEqual(30);
    });
I am liking this syntax. The full bowling example is here. I guess I'll call this jquery.bdd. I just started using git/github btw. This is one of my first projects on it. Fork me. Fork me please.

Update: jquery.bdd has been replaced by another project of mine called describe - which is agnostic to Javascript framework and could be used with server-side Javascript environments like node.js. Other Javascript BDD testing frameworks include:

  1. jasmine from Pivotal Labs
  2. jspec from Visionmedia
  3. expresso from Visionmedia
  4. ntest from technoweenie
Simon Kenyon Shepard said over 2 years ago
Wow, this is awesome, a really nice way of writing BDD tests in jQuery. Have you used it on any commercial or big scale projects? I'd love to see a fully blown test suite in this style.
Toby said over 2 years ago
Hello Simon! This is more of a pet project. But all the source is there on GitHub, it's not like it's anything complex. I have been only using this for an Adobe Air project so far.
Simon Kenyon Shepard said over 2 years ago
I'll have to take a good look at the source, but the syntax seems leagues ahead of current jQuery unit testing stuff, and so much more intuative and bdd orientated. Do you have any ways of dealing with async js calls/animations, with wait functions etc.?
Toby said over 2 years ago
No I do not. I don't imagine it'd be hard to rip it off from an existing framework though. If you point me to a existing solution, I might implement it.
Simon Kenyon Shepard said over 2 years ago
Hey Toby, well, I guess the jQuery benchmark is qUnit,

I've been through the qUnit stuff myself to figure out how it works and whether I could reuse it for other projects, my general concenus was the code quality was not quite up to the level I'd want it to be, it could do with some decent abstraction and general refactoring. It might be quicker to start from scratch, I've always preferred a test queueing system where you can stop and start the queue processing whenever you choose and have some fairly simple timeouts that modify when the queue automatically starts reprocessing.
Toby said over 2 years ago
Simon, check out jspec at http://github.com/bhauman/jspec/tree/master. I've been keeping my eye on this and bhauman put out a new release.
Toby said over 2 years ago
Simon, not sure you still care, but I did take a look at both implementations of async by jspec vs Qunit. jspec cheated. He just overwrote all the ajax calls to by synchronus for the test duration, this wouldn't work for non-ajax code with delays. For Qunit, it uses the queue process approach that you described briefly, but with this approach, you'd better be sure that the setTimeout() call in your test case is the last instruction that has any consequence, am I right?
TJ Holowaychuk said over 2 years ago
We support mock timers now (just include lib/jspec.timers.js) for async support. You should never have to resort to true async code during tests, this will just slow things down and is unnecessary. 

Leave Comment

optional