logicalparadox/chai — pull request #14 by jfirebaugh (2012-02-12 22:56:54+00:00)
Title: Make Assertion.prototype properties configurable

This allows plugins to install object-specific behavior for built-in
assertions.

I'm working on an assertion library for jQuery (inspired by https://github.com/velesin/jasmine-jquery).
The goal is to write assertions like:

$('#foo').should.exist;
$('#foo').should.contain('#bar');

These would be implemented by overriding the existing properties, e.g.:

```
chai.use(function (chai) {
var chaiExist = Object.getOwnPropertyDescriptor(chai.Assertion.prototype, 'exist');
Object.defineProperty(chai.Assertion.prototype, 'exist', {
get : function () {
if (this.obj instanceof jQuery) {
this.assert(
this.obj.length > 0
, 'expected ' + inspect(this.obj.selector) + ' to exist'
, 'expected ' + inspect(this.obj.selector) + ' not to exist');
return this;
} else {
return chaiExist.get.call(this);
}
}
});
});
```

But in order for this to work, the original `Object.defineProperty` needs to have
been passed `configurable: true`.

I'm also amenable to other suggestions as to how to accomplish this.