Add a setter for Object.prototype.should. Closes #86.

This commit is contained in:
Domenic Denicola 2012-08-24 20:28:13 -04:00
parent 35bec1b896
commit 1024e2dd14
2 changed files with 32 additions and 1 deletions

View file

@ -10,7 +10,21 @@ module.exports = function (chai, util) {
function loadShould () {
// modify Object.prototype to have `should`
Object.defineProperty(Object.prototype, 'should',
{ set: function () {}
{
set: function (value) {
// See https://github.com/chaijs/chai/issues/86: this makes
// `whatever.should = someValue` actually set `someValue`, which is
// especially useful for `global.should = require('chai').should()`.
//
// Note that we have to use [[DefineProperty]] instead of [[Put]]
// since otherwise we would trigger this very setter!
Object.defineProperty(this, 'should', {
value: value,
enumerable: true,
configurable: true,
writable: true
});
}
, get: function(){
if (this instanceof String || this instanceof Number) {
return new Assertion(this.constructor(this));

17
test/globalShould.js Normal file
View file

@ -0,0 +1,17 @@
if (!chai) {
var chai = require('..');
}
suite('global should', function () {
test('works', function () {
global.should = chai.should();
try {
should.not.exist(undefined);
} finally {
delete global.should;
}
});
});