From 1024e2dd148539abc1ca9aff06270ed39c04a358 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Fri, 24 Aug 2012 20:28:13 -0400 Subject: [PATCH] Add a setter for `Object.prototype.should`. Closes #86. --- lib/chai/interface/should.js | 16 +++++++++++++++- test/globalShould.js | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/globalShould.js diff --git a/lib/chai/interface/should.js b/lib/chai/interface/should.js index 115786b..b60f5c4 100644 --- a/lib/chai/interface/should.js +++ b/lib/chai/interface/should.js @@ -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)); diff --git a/test/globalShould.js b/test/globalShould.js new file mode 100644 index 0000000..36a63e5 --- /dev/null +++ b/test/globalShould.js @@ -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; + } + }); + +});