Skip to content

Commit

Permalink
Make sure TypeErrors thrown by frozen are caught
Browse files Browse the repository at this point in the history
  • Loading branch information
astorije committed Jul 26, 2015
1 parent bad5fa7 commit cffb96b
Showing 1 changed file with 15 additions and 2 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'
);
});

};

0 comments on commit cffb96b

Please sign in to comment.