Skip to content

Commit 520e54b

Browse files
thefourtheyemeeber
authored andcommitted
handle NaN properly in assertions
Fixes: chaijs#498
1 parent 9c5dbcf commit 520e54b

File tree

7 files changed

+109
-14
lines changed

7 files changed

+109
-14
lines changed

lib/chai/core/assertions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ module.exports = function (chai, _) {
446446
* ### .NaN
447447
* Asserts that the target is `NaN`.
448448
*
449-
* expect('foo').to.be.NaN;
449+
* expect(NaN).to.be.NaN;
450450
* expect(4).not.to.be.NaN;
451451
*
452452
* @name NaN
@@ -456,7 +456,7 @@ module.exports = function (chai, _) {
456456

457457
Assertion.addProperty('NaN', function () {
458458
this.assert(
459-
isNaN(flag(this, 'object'))
459+
_.isNaN(flag(this, 'object'))
460460
, 'expected #{this} to be NaN'
461461
, 'expected #{this} not to be NaN'
462462
);

lib/chai/interface/assert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ module.exports = function (chai, util) {
431431
* ### .isNaN
432432
* Asserts that value is NaN
433433
*
434-
* assert.isNaN('foo', 'foo is NaN');
434+
* assert.isNaN(NaN, 'NaN is NaN');
435435
*
436436
* @name isNaN
437437
* @param {Mixed} value

lib/chai/utils/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,9 @@ exports.checkError = require('check-error');
158158
*/
159159

160160
exports.proxify = require('./proxify');
161+
162+
/*!
163+
* isNaN method
164+
*/
165+
166+
exports.isNaN = require('./isNaN');

lib/chai/utils/isNaN.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*!
2+
* Chai - isNaN utility
3+
* Copyright(c) 2012-2015 Sakthipriyan Vairamani <[email protected]>
4+
* MIT Licensed
5+
*/
6+
7+
/**
8+
* ### isNaN(value)
9+
*
10+
* Checks if the given value is NaN or not.
11+
*
12+
* utils.isNaN(NaN); // true
13+
*
14+
* @param {Value} The value which has to be checked if it is NaN
15+
* @name isNaN
16+
* @api private
17+
*/
18+
19+
function isNaN(value) {
20+
// Refer http://www.ecma-international.org/ecma-262/6.0/#sec-isnan-number
21+
// section's NOTE.
22+
return value !== value;
23+
}
24+
25+
// If ECMAScript 6's Number.isNaN is present, prefer that.
26+
module.exports = Number.isNaN || isNaN;

test/assert.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ describe('assert', function () {
233233
assert.deepEqual({tea: 'chai'}, {tea: 'chai'});
234234
assert.deepStrictEqual({tea: 'chai'}, {tea: 'chai'}); // Alias of deepEqual
235235

236+
assert.deepEqual([NaN], [NaN]);
237+
assert.deepEqual({tea: NaN}, {tea: NaN});
238+
236239
err(function () {
237240
assert.deepEqual({tea: 'chai'}, {tea: 'black'});
238241
}, "expected { tea: \'chai\' } to deeply equal { tea: \'black\' }");
@@ -329,7 +332,19 @@ describe('assert', function () {
329332
});
330333

331334
it('isNaN', function() {
332-
assert.isNaN('hello');
335+
assert.isNaN(NaN);
336+
337+
err(function (){
338+
assert.isNaN(Infinity);
339+
}, "expected Infinity to be NaN");
340+
341+
err(function (){
342+
assert.isNaN(undefined);
343+
}, "expected undefined to be NaN");
344+
345+
err(function (){
346+
assert.isNaN({});
347+
}, "expected {} to be NaN");
333348

334349
err(function (){
335350
assert.isNaN(4);
@@ -338,10 +353,13 @@ describe('assert', function () {
338353

339354
it('isNotNaN', function() {
340355
assert.isNotNaN(4);
356+
assert.isNotNaN(Infinity);
357+
assert.isNotNaN(undefined);
358+
assert.isNotNaN({});
341359

342360
err(function (){
343-
assert.isNotNaN('hello');
344-
}, "expected 'hello' not to be NaN");
361+
assert.isNotNaN(NaN);
362+
}, "expected NaN not to be NaN");
345363
});
346364

347365
it('exists', function() {

test/expect.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -500,22 +500,41 @@ describe('expect', function () {
500500

501501
it('NaN', function() {
502502
expect(NaN).to.be.NaN;
503-
expect('foo').to.be.NaN;
504-
expect({}).to.be.NaN;
503+
504+
expect(undefined).not.to.be.NaN;
505+
expect(Infinity).not.to.be.NaN;
506+
expect('foo').not.to.be.NaN;
507+
expect({}).not.to.be.NaN;
505508
expect(4).not.to.be.NaN;
506509
expect([]).not.to.be.NaN;
507510

511+
err(function(){
512+
expect(NaN).not.to.be.NaN;
513+
}, "expected NaN not to be NaN");
514+
515+
err(function(){
516+
expect(undefined).to.be.NaN;
517+
}, "expected undefined to be NaN");
518+
519+
err(function(){
520+
expect(Infinity).to.be.NaN;
521+
}, "expected Infinity to be NaN");
522+
523+
err(function(){
524+
expect('foo').to.be.NaN;
525+
}, "expected 'foo' to be NaN");
526+
527+
err(function(){
528+
expect({}).to.be.NaN;
529+
}, "expected {} to be NaN");
530+
508531
err(function(){
509532
expect(4).to.be.NaN;
510533
}, "expected 4 to be NaN");
511534

512535
err(function(){
513536
expect([]).to.be.NaN;
514537
}, "expected [] to be NaN");
515-
516-
err(function(){
517-
expect('foo').not.to.be.NaN;
518-
}, "expected 'foo' not to be NaN");
519538
});
520539

521540
it('finite', function() {

test/should.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,38 @@ describe('should', function() {
154154
});
155155

156156
it('NaN', function(){
157-
'foo'.should.be.NaN;
157+
NaN.should.be.NaN;
158+
159+
Infinity.should.not.be.NaN;
160+
'foo'.should.not.be.NaN;
161+
({}).should.not.be.NaN;
162+
should.not.equal(undefined, NaN);
158163
(4).should.not.be.NaN;
159164

165+
err(function(){
166+
NaN.should.not.be.NaN;
167+
}, "expected NaN not to be NaN");
168+
169+
err(function(){
170+
Infinity.should.be.NaN;
171+
}, "expected Infinity to be NaN");
172+
173+
err(function(){
174+
'foo'.should.be.NaN;
175+
}, "expected 'foo' to be NaN");
176+
177+
err(function(){
178+
({}).should.be.NaN;
179+
}, "expected {} to be NaN");
180+
181+
err(function(){
182+
should.equal(undefined, NaN);
183+
}, "expected undefined to equal NaN");
184+
160185
err(function(){
161186
(4).should.be.NaN;
162-
}, "expected 4 to be NaN")
187+
}, "expected 4 to be NaN");
188+
163189
});
164190

165191
it('undefined', function(){

0 commit comments

Comments
 (0)