Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle NaN properly in assertions #508

Merged
merged 1 commit into from
Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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