Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Explicitly check `null` and `undefined` values in exports.type

Max Goodman0d16e02585ad
diff --git a/lib/utils.js b/lib/utils.js
index 5c292479..5eb2b92d 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -372,7 +372,11 @@ var emptyRepresentation = function emptyRepresentation(value, type) {
* @returns {string}
*/
exports.type = function type(value) {
- if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
+ if (value === undefined) {
+ return 'undefined';
+ } else if (value === null) {
+ return 'null';
+ } else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
return 'buffer';
}
return Object.prototype.toString.call(value)
diff --git a/test/acceptance/utils.js b/test/acceptance/utils.js
index fa260bbf..8b861745 100644
--- a/test/acceptance/utils.js
+++ b/test/acceptance/utils.js
@@ -348,6 +348,26 @@ describe('lib/utils', function () {
type(global).should.equal('global');
type(true).should.equal('boolean');
});
+
+ describe('when toString on null or undefined stringifies window', function () {
+ var toString = Object.prototype.toString;
+
+ beforeEach(function () {
+ // some JS engines such as PhantomJS 1.x exhibit this behavior
+ Object.prototype.toString = function () {
+ return '[object DOMWindow]';
+ };
+ });
+
+ it('should recognize null and undefined', function () {
+ type(null).should.equal('null');
+ type(undefined).should.equal('undefined');
+ });
+
+ afterEach(function () {
+ Object.prototype.toString = toString;
+ });
+ });
});
describe('lookupFiles', function () {