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; + } + }); + +});