Skip to content

Commit

Permalink
Make sure TypeErrors thrown by frozen are treated in a ES6 way and te…
Browse files Browse the repository at this point in the history
…st these
  • Loading branch information
astorije committed Jul 26, 2015
1 parent bad5fa7 commit eae67c0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1719,11 +1719,24 @@ module.exports = function (chai, _) {
Assertion.addProperty('frozen', function() {
var obj = flag(this, 'object');

// In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError.
// In ES6, a non-object argument will be treated as if it was a frozen ordinary object, simply return true.
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen
// The following provides ES6 behavior when a TypeError is thrown under ES5.

var isFrozen;

try {
isFrozen = Object.isFrozen(obj);
} catch (err) {
if (err instanceof TypeError) isFrozen = true;
else throw err;
}

this.assert(
Object.isFrozen(obj)
isFrozen
, 'expected #{this} to be frozen'
, 'expected #{this} to not be frozen'
);
});

};
30 changes: 30 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,14 @@ describe('assert', function () {
err(function() {
assert[isFrozen]({});
}, 'expected {} to be frozen');

// Making sure ES6-like Object.isFrozen response is respected for all primitive types

assert[isFrozen](42);
assert[isFrozen](null);
assert[isFrozen]('foo');
assert[isFrozen](false);
assert[isFrozen](undefined);
});
});

Expand All @@ -845,6 +853,28 @@ describe('assert', function () {
err(function() {
assert[isNotFrozen](frozenObject);
}, 'expected {} to not be frozen');

// Making sure ES6-like Object.isFrozen response is respected for all primitive types

err(function() {
assert[isNotFrozen](42);
}, 'expected 42 to not be frozen');

err(function() {
assert[isNotFrozen](null);
}, 'expected null to not be frozen');

err(function() {
assert[isNotFrozen]('foo');
}, 'expected \'foo\' to not be frozen');

err(function() {
assert[isNotFrozen](false);
}, 'expected false to not be frozen');

err(function() {
assert[isNotFrozen](undefined);
}, 'expected undefined to not be frozen');
});
});
});
29 changes: 28 additions & 1 deletion test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,33 @@ describe('expect', function () {
err(function() {
expect(frozenObject).to.not.be.frozen;
}, 'expected {} to not be frozen');
});

// Making sure ES6-like Object.isFrozen response is respected for all primitive types

expect(42).to.be.frozen;
expect(null).to.be.frozen;
expect('foo').to.be.frozen;
expect(false).to.be.frozen;
expect(undefined).to.be.frozen;

err(function() {
expect(42).to.not.be.frozen;
}, 'expected 42 to not be frozen');

err(function() {
expect(null).to.not.be.frozen;
}, 'expected null to not be frozen');

err(function() {
expect('foo').to.not.be.frozen;
}, 'expected \'foo\' to not be frozen');

err(function() {
expect(false).to.not.be.frozen;
}, 'expected false to not be frozen');

err(function() {
expect(undefined).to.not.be.frozen;
}, 'expected undefined to not be frozen');
});
});
18 changes: 18 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,5 +976,23 @@ describe('should', function() {
err(function() {
frozenObject.should.not.be.frozen;
}, 'expected {} to not be frozen');

// Making sure ES6-like Object.isFrozen response is respected for all primitive types

(42).should.be.frozen;
'foo'.should.be.frozen;
false.should.be.frozen;

err(function() {
(42).should.not.be.frozen;
}, 'expected 42 to not be frozen');

err(function() {
'foo'.should.not.be.frozen;
}, 'expected \'foo\' to not be frozen');

err(function() {
false.should.not.be.frozen;
}, 'expected false to not be frozen');
});
});

0 comments on commit eae67c0

Please sign in to comment.