Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

unexpected

Package Overview
Dependencies
Maintainers
2
Versions
330
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unexpected - npm Package Compare versions

Comparing version 3.0.1 to 3.1.1

lib/unexpected-types.js

1

lib/index.js

@@ -29,2 +29,3 @@ /*global console, __dirname, weknowhow*/

'unexpected-core.js',
'unexpected-types.js',
'unexpected-assertions.js',

@@ -31,0 +32,0 @@ 'unexpected-module.js'

32

lib/unexpected-assertions.js

@@ -103,9 +103,23 @@ /*global namespace*/

// TODO the not flag does not make a lot of sense in this case
forEach(getKeys(properties), function (property) {
if (this.flags.not) {
if (this.flags.not) {
forEach(getKeys(properties), function (property) {
expect(subject, 'not to have [own] property', property);
} else {
expect(subject, 'to have [own] property', property, properties[property]);
});
} else {
try {
forEach(getKeys(properties), function (property) {
expect(subject, 'to have [own] property', property, properties[property]);
});
} catch (e) {
e.expected = expect.sanitize(properties);
for (var propertyName in subject) {
if ((!this.flags.own || subject.hasOwnProperty(propertyName)) && !(propertyName in properties)) {
e.expected[propertyName] = expect.sanitize(subject[propertyName]);
}
}
e.actual = expect.sanitize(subject);
e.showDiff = true;
throw e;
}
}, this);
}
} else {

@@ -219,7 +233,7 @@ throw new Error("Assertion '" + this.testDescription + "' only supports " +

try {
expect(this.equal(value, subject), '[not] to be true');
expect(expect.equal(value, subject), '[not] to be true');
} catch (e) {
if (!this.flags.not) {
e.expected = value;
e.actual = subject;
e.expected = expect.sanitize(value);
e.actual = expect.sanitize(subject);
// Explicitly tell mocha to stringify and diff arrays

@@ -302,3 +316,3 @@ // and objects, but only when the types are identical

if (errors.length > 0) {
var objectString = this.inspect(subject);
var objectString = expect.inspect(subject);
var prefix = /\n/.test(objectString) ? '\n' : ' ';

@@ -305,0 +319,0 @@ var message = 'failed expectation in' + prefix + objectString + ':\n' +

/*global namespace*/
(function () {
var inspect = namespace.inspect;
var equal = namespace.equal;
var shim = namespace.shim;

@@ -19,5 +16,10 @@ var bind = shim.bind;

var levenshteinDistance = utils.levenshteinDistance;
var isArray = utils.isArray;
function Assertion(subject, testDescription, flags, args) {
function Assertion(expect, subject, testDescription, flags, args) {
this.expect = expect;
this.obj = subject; // deprecated
this.equal = bind(expect.equal, expect); // deprecated
this.eql = this.equal; // deprecated
this.inspect = bind(expect.inspect, expect); // deprecated
this.subject = subject;

@@ -31,4 +33,5 @@ this.testDescription = testDescription;

Assertion.prototype.standardErrorMessage = function () {
var expect = this.expect;
var argsString = map(this.args, function (arg) {
return inspect(arg);
return expect.inspect(arg);
}).join(', ');

@@ -41,3 +44,3 @@

return 'expected ' +
inspect(this.subject) +
expect.inspect(this.subject) +
' ' + this.testDescription +

@@ -61,10 +64,19 @@ argsString;

function Unexpected(assertions) {
function Unexpected(assertions, types) {
this.assertions = assertions || {};
this.types = types || [];
}
Unexpected.prototype.equal = function (actual, expected) {
return namespace.equal(actual, expected, this.types);
};
Unexpected.prototype.inspect = function (obj) {
return namespace.inspect(obj, false, 2, this.types);
};
Unexpected.prototype.format = function (message, args) {
args = map(args, function (arg) {
return inspect(arg);
});
return this.inspect(arg);
}, this);
message = message.replace(/\{(\d+)\}/g, function (match, n) {

@@ -116,2 +128,8 @@ return args[n] || match;

Unexpected.prototype.addType = function (obj) {
this.types.unshift(obj);
return this.expect;
};
Unexpected.prototype.installPlugin = function (plugin) {

@@ -127,2 +145,41 @@ if (typeof plugin !== 'function') {

Unexpected.prototype.sanitize = function (obj, stack) {
stack = stack || [];
var i;
for (i = 0 ; i < stack.length ; i += 1) {
if (stack[i] === obj) {
return obj;
}
}
var sanitized;
for (i = 0 ; i < this.types.length ; i += 1) {
if (this.types[i].identify(obj)) {
stack.push(obj);
sanitized = this.sanitize(this.types[i].toJSON(obj), stack);
stack.pop();
return sanitized;
}
}
if (isArray(obj)) {
stack.push(obj);
sanitized = map(obj, function (item) {
return this.sanitize(item, stack);
}, this);
stack.pop();
} else if (typeof obj === 'object' && obj) {
stack.push(obj);
sanitized = {};
forEach(getKeys(obj).sort(), function (key) {
sanitized[key] = this.sanitize(obj[key], stack);
}, this);
stack.pop();
} else {
sanitized = obj;
}
return sanitized;
};
function handleNestedExpects(e, assertion) {

@@ -145,4 +202,8 @@ switch (assertion.errorMode) {

var expect = bind(expectFunction, unexpected);
expect.equal = bind(unexpected.equal, unexpected);
expect.sanitize = bind(unexpected.sanitize, unexpected);
expect.inspect = bind(unexpected.inspect, unexpected);
expect.fail = bind(unexpected.fail, unexpected);
expect.addAssertion = bind(unexpected.addAssertion, unexpected);
expect.addType = bind(unexpected.addType, unexpected);
expect.clone = bind(unexpected.clone, unexpected);

@@ -196,5 +257,11 @@ expect.toString = bind(unexpected.toString, unexpected);

// Not sure this is the right way to go about this:
wrappedExpect.equal = this.equal;
wrappedExpect.types = this.types;
wrappedExpect.sanitize = this.sanitize;
wrappedExpect.inspect = this.inspect;
var args = Array.prototype.slice.call(arguments, 2);
args.unshift(wrappedExpect, subject);
var assertion = new Assertion(subject, testDescriptionString, flags, args.slice(2));
var assertion = new Assertion(wrappedExpect, subject, testDescriptionString, flags, args.slice(2));
var handler = assertionRule.handler;

@@ -225,3 +292,3 @@ try {

Unexpected.prototype.clone = function () {
var unexpected = new Unexpected(extend({}, this.assertions));
var unexpected = new Unexpected(extend({}, this.assertions), [].concat(this.types));
return makeExpectFunction(unexpected);

@@ -379,8 +446,3 @@ };

Assertion.prototype.inspect = inspect;
Assertion.prototype.eql = equal; // Deprecated
Assertion.prototype.equal = equal;
namespace.expect = Unexpected.create();
}());

@@ -7,3 +7,2 @@ /*global namespace*/

var utils = namespace.utils;
var isRegExp = utils.isRegExp;
var isArguments = utils.isArguments;

@@ -17,16 +16,15 @@ var isUndefinedOrNull = utils.isUndefinedOrNull;

*/
function equal(actual, expected) {
function equal(actual, expected, types) {
var matchingCustomType = utils.findFirst(types || [], function (type) {
return type.identify(actual) && type.identify(expected);
});
if (matchingCustomType) {
return matchingCustomType.equal(actual, expected, types);
}
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if ('undefined' !== typeof Buffer &&
Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
if (actual.length !== expected.length) return false;
for (var i = 0; i < actual.length; i += 1) {
if (actual[i] !== expected[i]) return false;
}
return true;
// 7.2. If the expected value is a Date object, the actual value is

@@ -49,7 +47,7 @@ // equivalent if it is also a Date object that refers to the same time.

} else {
return objEquiv(actual, expected);
return objEquiv(actual, expected, types);
}
}
function objEquiv(a, b) {
function objEquiv(a, b, types) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))

@@ -61,8 +59,2 @@ return false;

// Converting to array solves the problem.
if (isRegExp(a)) {
if (!isRegExp(b)) {
return false;
}
return a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline;
}
if (isArguments(a)) {

@@ -74,3 +66,3 @@ if (!isArguments(b)) {

b = Array.prototype.slice.call(b);
return equal(a, b);
return equal(a, b, types);
}

@@ -99,3 +91,3 @@ var ka, kb, key, i;

key = ka[i];
if (!equal(a[key], b[key]))
if (!equal(a[key], b[key], types))
return false;

@@ -102,0 +94,0 @@ }

@@ -27,3 +27,3 @@ /*global namespace*/

*/
var inspect = function (obj, showHidden, depth) {
var inspect = function (obj, showHidden, depth, types) {
var seen = [];

@@ -36,3 +36,10 @@

function format(value, recurseTimes) {
var matchingCustomType = utils.findFirst(types || [], function (type) {
return type.identify(value);
});
if (matchingCustomType) {
return matchingCustomType.inspect(value);
}
// Provide a hook for user-specified inspect functions.

@@ -39,0 +46,0 @@ // Check that value is an object with an inspect function on it

@@ -140,2 +140,12 @@ /*global namespace*/

}
},
findFirst: function (arr, predicate, thisObj) {
var scope = thisObj || null;
for (var i = 0 ; i < arr.length ; i += 1) {
if (predicate.call(scope, arr[i], i, arr)) {
return arr[i];
}
}
return null;
}

@@ -142,0 +152,0 @@ };

{
"name": "unexpected",
"version": "3.0.1",
"version": "3.1.1",
"author": "Sune Sloth Simonsen <sune@we-knowhow.dk>",

@@ -5,0 +5,0 @@ "keywords": [

@@ -422,2 +422,3 @@ /*global describe, it, expect, beforeEach, setTimeout*/

});
it('asserts presence of a list of own properties', function () {

@@ -487,2 +488,12 @@ expect({a: 'foo', b: 'bar'}, 'to have own properties', ['a', 'b']);

it('should add showDiff:true and diffable actual and expected properties to the error instance', function () {
expect(function () {
expect({a: 123, b: 456, c: 789}, 'to have properties', {a: 123, b: 987});
}, 'to throw', function (e) {
expect(e.showDiff, 'to be true');
expect(e.actual, 'to equal', {a: 123, b: 456, c: 789});
expect(e.expected, 'to equal', {a: 123, b: 987, c: 789});
});
});
it('throws when the assertion fails', function () {

@@ -1328,2 +1339,50 @@ expect(function () {

});
describe('addType', function () {
function box(value) {
return {
isBox: true,
value: value
};
}
var clonedExpect;
beforeEach(function () {
clonedExpect = expect.clone();
clonedExpect.addType({
identify: function (obj) {
return obj && typeof obj === 'object' && obj.isBox;
},
equal: function (a, b) {
return expect.equal(a.value, b.value);
},
inspect: function (obj) {
return '[Box: ' + clonedExpect.inspect(obj.value) + ']';
},
toJSON: function (obj) {
return {
$box: obj.value
};
}
});
});
it('should use the equal defined by the type', function () {
clonedExpect(box(123), 'to equal', box(123));
clonedExpect(box(123), 'not to equal', box(321));
});
it('should call toJSON recursively in case of a mismatch', function () {
expect(function () {
clonedExpect(box(box(123)), 'to equal', box(box(456)));
}, 'to throw', function (err) {
expect(err, 'to have properties', {
showDiff: true,
actual: {$box: {$box: 123}},
expected: {$box: {$box: 456}}
});
expect(err.message, 'to equal', "expected [Box: [Box: 123]] to equal [Box: [Box: 456]]");
});
});
});
});

@@ -209,2 +209,12 @@ (function () {

}
},
findFirst: function (arr, predicate, thisObj) {
var scope = thisObj || null;
for (var i = 0 ; i < arr.length ; i += 1) {
if (predicate.call(scope, arr[i], i, arr)) {
return arr[i];
}
}
return null;
}

@@ -221,3 +231,2 @@ };

var utils = namespace.utils;
var isRegExp = utils.isRegExp;
var isArguments = utils.isArguments;

@@ -231,16 +240,15 @@ var isUndefinedOrNull = utils.isUndefinedOrNull;

*/
function equal(actual, expected) {
function equal(actual, expected, types) {
var matchingCustomType = utils.findFirst(types || [], function (type) {
return type.identify(actual) && type.identify(expected);
});
if (matchingCustomType) {
return matchingCustomType.equal(actual, expected, types);
}
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if ('undefined' !== typeof Buffer &&
Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
if (actual.length !== expected.length) return false;
for (var i = 0; i < actual.length; i += 1) {
if (actual[i] !== expected[i]) return false;
}
return true;
// 7.2. If the expected value is a Date object, the actual value is

@@ -263,7 +271,7 @@ // equivalent if it is also a Date object that refers to the same time.

} else {
return objEquiv(actual, expected);
return objEquiv(actual, expected, types);
}
}
function objEquiv(a, b) {
function objEquiv(a, b, types) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))

@@ -275,8 +283,2 @@ return false;

// Converting to array solves the problem.
if (isRegExp(a)) {
if (!isRegExp(b)) {
return false;
}
return a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline;
}
if (isArguments(a)) {

@@ -288,3 +290,3 @@ if (!isArguments(b)) {

b = Array.prototype.slice.call(b);
return equal(a, b);
return equal(a, b, types);
}

@@ -313,3 +315,3 @@ var ka, kb, key, i;

key = ka[i];
if (!equal(a[key], b[key]))
if (!equal(a[key], b[key], types))
return false;

@@ -348,3 +350,3 @@ }

*/
var inspect = function (obj, showHidden, depth) {
var inspect = function (obj, showHidden, depth, types) {
var seen = [];

@@ -357,3 +359,10 @@

function format(value, recurseTimes) {
var matchingCustomType = utils.findFirst(types || [], function (type) {
return type.identify(value);
});
if (matchingCustomType) {
return matchingCustomType.inspect(value);
}
// Provide a hook for user-specified inspect functions.

@@ -548,5 +557,2 @@ // Check that value is an object with an inspect function on it

(function () {
var inspect = namespace.inspect;
var equal = namespace.equal;
var shim = namespace.shim;

@@ -565,5 +571,10 @@ var bind = shim.bind;

var levenshteinDistance = utils.levenshteinDistance;
var isArray = utils.isArray;
function Assertion(subject, testDescription, flags, args) {
function Assertion(expect, subject, testDescription, flags, args) {
this.expect = expect;
this.obj = subject; // deprecated
this.equal = bind(expect.equal, expect); // deprecated
this.eql = this.equal; // deprecated
this.inspect = bind(expect.inspect, expect); // deprecated
this.subject = subject;

@@ -577,4 +588,5 @@ this.testDescription = testDescription;

Assertion.prototype.standardErrorMessage = function () {
var expect = this.expect;
var argsString = map(this.args, function (arg) {
return inspect(arg);
return expect.inspect(arg);
}).join(', ');

@@ -587,3 +599,3 @@

return 'expected ' +
inspect(this.subject) +
expect.inspect(this.subject) +
' ' + this.testDescription +

@@ -607,10 +619,19 @@ argsString;

function Unexpected(assertions) {
function Unexpected(assertions, types) {
this.assertions = assertions || {};
this.types = types || [];
}
Unexpected.prototype.equal = function (actual, expected) {
return namespace.equal(actual, expected, this.types);
};
Unexpected.prototype.inspect = function (obj) {
return namespace.inspect(obj, false, 2, this.types);
};
Unexpected.prototype.format = function (message, args) {
args = map(args, function (arg) {
return inspect(arg);
});
return this.inspect(arg);
}, this);
message = message.replace(/\{(\d+)\}/g, function (match, n) {

@@ -662,2 +683,8 @@ return args[n] || match;

Unexpected.prototype.addType = function (obj) {
this.types.unshift(obj);
return this.expect;
};
Unexpected.prototype.installPlugin = function (plugin) {

@@ -673,2 +700,41 @@ if (typeof plugin !== 'function') {

Unexpected.prototype.sanitize = function (obj, stack) {
stack = stack || [];
var i;
for (i = 0 ; i < stack.length ; i += 1) {
if (stack[i] === obj) {
return obj;
}
}
var sanitized;
for (i = 0 ; i < this.types.length ; i += 1) {
if (this.types[i].identify(obj)) {
stack.push(obj);
sanitized = this.sanitize(this.types[i].toJSON(obj), stack);
stack.pop();
return sanitized;
}
}
if (isArray(obj)) {
stack.push(obj);
sanitized = map(obj, function (item) {
return this.sanitize(item, stack);
}, this);
stack.pop();
} else if (typeof obj === 'object' && obj) {
stack.push(obj);
sanitized = {};
forEach(getKeys(obj).sort(), function (key) {
sanitized[key] = this.sanitize(obj[key], stack);
}, this);
stack.pop();
} else {
sanitized = obj;
}
return sanitized;
};
function handleNestedExpects(e, assertion) {

@@ -691,4 +757,8 @@ switch (assertion.errorMode) {

var expect = bind(expectFunction, unexpected);
expect.equal = bind(unexpected.equal, unexpected);
expect.sanitize = bind(unexpected.sanitize, unexpected);
expect.inspect = bind(unexpected.inspect, unexpected);
expect.fail = bind(unexpected.fail, unexpected);
expect.addAssertion = bind(unexpected.addAssertion, unexpected);
expect.addType = bind(unexpected.addType, unexpected);
expect.clone = bind(unexpected.clone, unexpected);

@@ -742,5 +812,11 @@ expect.toString = bind(unexpected.toString, unexpected);

// Not sure this is the right way to go about this:
wrappedExpect.equal = this.equal;
wrappedExpect.types = this.types;
wrappedExpect.sanitize = this.sanitize;
wrappedExpect.inspect = this.inspect;
var args = Array.prototype.slice.call(arguments, 2);
args.unshift(wrappedExpect, subject);
var assertion = new Assertion(subject, testDescriptionString, flags, args.slice(2));
var assertion = new Assertion(wrappedExpect, subject, testDescriptionString, flags, args.slice(2));
var handler = assertionRule.handler;

@@ -771,3 +847,3 @@ try {

Unexpected.prototype.clone = function () {
var unexpected = new Unexpected(extend({}, this.assertions));
var unexpected = new Unexpected(extend({}, this.assertions), [].concat(this.types));
return makeExpectFunction(unexpected);

@@ -925,7 +1001,2 @@ };

Assertion.prototype.inspect = inspect;
Assertion.prototype.eql = equal; // Deprecated
Assertion.prototype.equal = equal;
namespace.expect = Unexpected.create();

@@ -1035,9 +1106,23 @@ }());

// TODO the not flag does not make a lot of sense in this case
forEach(getKeys(properties), function (property) {
if (this.flags.not) {
if (this.flags.not) {
forEach(getKeys(properties), function (property) {
expect(subject, 'not to have [own] property', property);
} else {
expect(subject, 'to have [own] property', property, properties[property]);
});
} else {
try {
forEach(getKeys(properties), function (property) {
expect(subject, 'to have [own] property', property, properties[property]);
});
} catch (e) {
e.expected = expect.sanitize(properties);
for (var propertyName in subject) {
if ((!this.flags.own || subject.hasOwnProperty(propertyName)) && !(propertyName in properties)) {
e.expected[propertyName] = expect.sanitize(subject[propertyName]);
}
}
e.actual = expect.sanitize(subject);
e.showDiff = true;
throw e;
}
}, this);
}
} else {

@@ -1151,7 +1236,7 @@ throw new Error("Assertion '" + this.testDescription + "' only supports " +

try {
expect(this.equal(value, subject), '[not] to be true');
expect(expect.equal(value, subject), '[not] to be true');
} catch (e) {
if (!this.flags.not) {
e.expected = value;
e.actual = subject;
e.expected = expect.sanitize(value);
e.actual = expect.sanitize(subject);
// Explicitly tell mocha to stringify and diff arrays

@@ -1234,3 +1319,3 @@ // and objects, but only when the types are identical

if (errors.length > 0) {
var objectString = this.inspect(subject);
var objectString = expect.inspect(subject);
var prefix = /\n/.test(objectString) ? '\n' : ' ';

@@ -1314,2 +1399,82 @@ var message = 'failed expectation in' + prefix + objectString + ':\n' +

(function () {
var expect = namespace.expect;
var utils = namespace.utils;
var isRegExp = utils.isRegExp;
expect.addType({
identify: function (obj) {
return Object.prototype.toString.call(obj) === '[object Date]';
},
equal: function (a, b) {
return a.getTime() === b.getTime();
},
inspect: function (date) {
return '[Date ' + date.toUTCString() + ']';
},
toJSON: function (date) {
return {
$date: this.inspect(date)
};
}
});
expect.addType({
identify: function (f) {
return typeof f === 'function';
},
equal: function (a, b) {
return a === b || a.toString() === b.toString();
},
inspect: function (f) {
var n = f.name ? ': ' + f.name : '';
return '[Function' + n + ']';
},
toJSON: function (f) {
return {
$function: this.inspect(f)
};
}
});
expect.addType({
identify: isRegExp,
equal: function (a, b) {
return a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline;
},
inspect: function (regExp) {
return '' + regExp;
},
toJSON: function (regExp) {
return {
$regExp: this.inspect(regExp)
};
}
});
if (typeof Buffer !== 'undefined') {
expect.addType({
identify: Buffer.isBuffer,
equal: function (a, b) {
if (a.length !== b.length) return false;
for (var i = 0; i < a.length; i += 1) {
if (a[i] !== b[i]) return false;
}
return true;
},
inspect: function (buffer) {
return buffer.toString();
},
toJSON: function (buffer) {
return {
$buffer: Array.prototype.slice.call(buffer)
};
}
});
}
}());
(function () {
var global = this;

@@ -1316,0 +1481,0 @@ var expect = namespace.expect;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc