mirror of
https://github.com/chaijs/chai
synced 2024-11-15 00:07:11 +00:00
Add a setter for Object.prototype.should
. Closes #86.
This commit is contained in:
parent
35bec1b896
commit
1024e2dd14
2 changed files with 32 additions and 1 deletions
|
@ -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
17
test/globalShould.js
Normal 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;
|
||||
}
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in a new issue