Javascript - EmberJS - Testing

Chai is a pretty reasonable testing framework. But there are some key problems that make it much harder than it should be to test.

Very few methods provide an optional message

  • expect(true)'message')
  • expect(undefined).to.exist('message')

Both of these assertions will fail with “is not a function”. This means the only message you will get back is expected true to be false and expected undefined to be defined. How helpful. I’ve created to hopefully resolve this.

Update: It may be the best way to do this is expect(true).to.equal(true, 'message').

define and it cannot have parameterised tests

define 'my thing', ->
  for i in [0..6]
    it 'tests ' + i, ->

This won’t work. I haven't found a way to do parameterised tests yet. One option is to use async.each:

async.each([1,2,3], function(itemNumber, callback) {
  describe('Test # ' + itemNumber, function () {
    it("should be a number", function (done) {

Or, you can use Array#forEach (thanks @jamesotron):

define 'my thing', ->
  [0..6].forEach (i) ->
    it 'tests ' + i, ->

Chaining your own properties is way too hard

It’s easy enough to define a basic assertion, for example expect(foo)

But if you want to also support expect(foo), then you have to go through a huge amount of work to store these properties through the assertion chains or something.

All assertions are defined in terms of the expect() object

That means you cannot (easily) have a new assertion like expect(".class"), because we need to define the “.present” w.r.t. the ".class" string, and not what would eventually be a $() selector.

That also means that you can not have an easy-to-understand message for the presence (or not) of elements, because the only context that you have available is the tested object.

(In our project, we could not use the chai-jquery plugin because it interfered with EmberJS: it messes up expect(find(...)) from EmberJS (find() returns a JQuery object).)

You cannot run partial tests or rerun failed tests

This is probably a problem with Mocha instead.