Skip to content

Commit

Permalink
handle NaN properly in assertions
Browse files Browse the repository at this point in the history
Fixes: #498
  • Loading branch information
Sakthipriyan Vairamani committed Aug 19, 2015
1 parent 03d7a80 commit c8ad5e6
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ module.exports = function (chai, _) {
* ### .NaN
* Asserts that the target is `NaN`.
*
* expect('foo').to.be.NaN;
* expect(NaN).to.be.NaN;
* expect(4).not.to.be.NaN;
*
* @name NaN
Expand All @@ -344,7 +344,7 @@ module.exports = function (chai, _) {

Assertion.addProperty('NaN', function () {
this.assert(
isNaN(flag(this, 'object'))
_.isNaN(flag(this, 'object'))
, 'expected #{this} to be NaN'
, 'expected #{this} not to be NaN'
);
Expand Down
2 changes: 1 addition & 1 deletion lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ module.exports = function (chai, util) {
* ### .isNaN
* Asserts that value is NaN
*
* assert.isNaN('foo', 'foo is NaN');
* assert.isNaN(NaN, 'NaN is NaN');
*
* @name isNaN
* @param {Mixed} value
Expand Down
5 changes: 5 additions & 0 deletions lib/chai/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ exports.addChainableMethod = require('./addChainableMethod');

exports.overwriteChainableMethod = require('./overwriteChainableMethod');

/*!
* isNaN method
*/

exports.isNaN = require('./isNaN');
26 changes: 26 additions & 0 deletions lib/chai/utils/isNaN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*!
* Chai - isNaN utility
* Copyright(c) 2012-2015 Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
* MIT Licensed
*/

/**
* ### isNaN(value)
*
* Checks if the given value is NaN or not.
*
* utils.isNaN(NaN); // true
*
* @param {Value} The value which has to be checked if it is NaN
* @name isNaN
* @api private
*/

function isNaN(value) {
// Refer http://www.ecma-international.org/ecma-262/6.0/#sec-isnan-number
// section's NOTE.
return value !== value;
}

// If ECMAScript 6's Number.isNaN is present, prefer that.
module.exports = Number.isNaN || isNaN;
24 changes: 21 additions & 3 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ describe('assert', function () {
it('deepEqual', function() {
assert.deepEqual({tea: 'chai'}, {tea: 'chai'});

assert.deepEqual([NaN], [NaN]);
assert.deepEqual({tea: NaN}, {tea: NaN});

err(function () {
assert.deepEqual({tea: 'chai'}, {tea: 'black'});
}, "expected { tea: \'chai\' } to deeply equal { tea: \'black\' }");
Expand Down Expand Up @@ -302,7 +305,19 @@ describe('assert', function () {
});

it('isNaN', function() {
assert.isNaN('hello');
assert.isNaN(NaN);

err(function (){
assert.isNaN(Infinity);
}, "expected Infinity to be NaN");

err(function (){
assert.isNaN(undefined);
}, "expected undefined to be NaN");

err(function (){
assert.isNaN({});
}, "expected {} to be NaN");

err(function (){
assert.isNaN(4);
Expand All @@ -311,10 +326,13 @@ describe('assert', function () {

it('isNotNaN', function() {
assert.isNotNaN(4);
assert.isNotNaN(Infinity);
assert.isNotNaN(undefined);
assert.isNotNaN({});

err(function (){
assert.isNotNaN('hello');
}, "expected 'hello' not to be NaN");
assert.isNotNaN(NaN);
}, "expected NaN not to be NaN");
});

it('isUndefined', function() {
Expand Down
31 changes: 25 additions & 6 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,22 +426,41 @@ describe('expect', function () {

it('NaN', function() {
expect(NaN).to.be.NaN;
expect('foo').to.be.NaN;
expect({}).to.be.NaN;

expect(undefined).not.to.be.NaN;
expect(Infinity).not.to.be.NaN;
expect('foo').not.to.be.NaN;
expect({}).not.to.be.NaN;
expect(4).not.to.be.NaN;
expect([]).not.to.be.NaN;

err(function(){
expect(NaN).not.to.be.NaN;
}, "expected NaN not to be NaN");

err(function(){
expect(undefined).to.be.NaN;
}, "expected undefined to be NaN");

err(function(){
expect(Infinity).to.be.NaN;
}, "expected Infinity to be NaN");

err(function(){
expect('foo').to.be.NaN;
}, "expected 'foo' to be NaN");

err(function(){
expect({}).to.be.NaN;
}, "expected {} to be NaN");

err(function(){
expect(4).to.be.NaN;
}, "expected 4 to be NaN");

err(function(){
expect([]).to.be.NaN;
}, "expected [] to be NaN");

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

it('property(name)', function(){
Expand Down
30 changes: 28 additions & 2 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,38 @@ describe('should', function() {
});

it('NaN', function(){
'foo'.should.be.NaN;
NaN.should.be.NaN;

Infinity.should.not.be.NaN;
'foo'.should.not.be.NaN;
({}).should.not.be.NaN;
should.not.equal(undefined, NaN);
(4).should.not.be.NaN;

err(function(){
NaN.should.not.be.NaN;
}, "expected NaN not to be NaN");

err(function(){
Infinity.should.be.NaN;
}, "expected Infinity to be NaN");

err(function(){
'foo'.should.be.NaN;
}, "expected 'foo' to be NaN");

err(function(){
({}).should.be.NaN;
}, "expected {} to be NaN");

err(function(){
should.equal(undefined, NaN);
}, "expected undefined to equal NaN");

err(function(){
(4).should.be.NaN;
}, "expected 4 to be NaN")
}, "expected 4 to be NaN");

});

it('undefined', function(){
Expand Down

0 comments on commit c8ad5e6

Please sign in to comment.