tj / should.js

BDD style assertions for node.js -- test framework agnostic
MIT License
2.75k stars 195 forks source link

Messages support (in case of assertion failure) #13

Closed aseemk closed 11 years ago

aseemk commented 13 years ago

This patch adds an optional msg arg to all assertion methods:

foo.should.equal(bar, 'baz didn't have an effect!');
all.should.have.length(10, 'elements weren't added')

Because this wasn't possible for testing that properties exist:

// doesn't work, tests that foo.bar === 'custom message'
foo.should.have.property('bar', 'custom message')

I added a new verb called expose which is like property but doesn't take an optional value:

foo.should.expose('bar', 'custom message')

Hat tip to @dominictarr for pointing this out.

This isn't possible for non-method assertions like ok and arguments, which is why I'd love to change those to methods as well. Haven't figured out a clean way to do that without breaking existing code or having the change be opt-in via a config, which is kinda lame. What are your thoughts on that?

I also improved the static should.fail() --- which was just a wrapper for the native assert.fail() --- to actually set the passed-in message to the error's message property.

Other than that, some minor code and test cleanup. Let me know if something isn't clear.

Cheers, Aseem

aseemk commented 13 years ago

Funny, for some reason it includes all my old commits for should.exist() even though you already pulled those in. Any idea why that happens?

aseemk commented 13 years ago

Note that this pull request includes all of the changes in pull request #14. That now includes the automatic error message support:

someAsyncTask(function (err, result) {
    should.not.exist(err);    // if this fails, uses err as the message
    // ...
});