jest-matchers
Advanced tools
Comparing version 18.1.0 to 18.5.0-alpha.7da3df39
@@ -35,4 +35,5 @@ /** | ||
require('./jasmine-utils');const any = _require.any,anything = _require.anything,arrayContaining = _require.arrayContaining,objectContaining = _require.objectContaining,stringMatching = _require.stringMatching; | ||
require('./asymmetric-matchers');const any = _require.any,anything = _require.anything,arrayContaining = _require.arrayContaining,objectContaining = _require.objectContaining,stringContaining = _require.stringContaining,stringMatching = _require.stringMatching; | ||
const GLOBAL_STATE = Symbol.for('$$jest-matchers-object'); | ||
@@ -151,2 +152,3 @@ | ||
expect.arrayContaining = arrayContaining; | ||
expect.stringContaining = stringContaining; | ||
expect.stringMatching = stringMatching; | ||
@@ -171,3 +173,3 @@ | ||
' {message?: string | function, pass: boolean}\n' + | ||
`'${ utils.stringify(result) }' was returned`); | ||
`'${utils.stringify(result)}' was returned`); | ||
@@ -174,0 +176,0 @@ } |
@@ -28,4 +28,2 @@ /* | ||
const prettyFormat = require('pretty-format'); | ||
// Extracted out of jasmine 2.5.2 | ||
@@ -251,18 +249,2 @@ function equals(a, b, customTesters) { | ||
function isFunction(obj) { | ||
return typeof obj === 'function'; | ||
} | ||
function isObjectConstructor(ctor) { | ||
// aCtor instanceof aCtor is true for the Object and Function | ||
// constructors (since a constructor is-a Function and a function is-a | ||
// Object). We don't just compare ctor === Object because the constructor | ||
// might come from a different frame with different globals. | ||
return isFunction(ctor) && ctor instanceof ctor; | ||
} | ||
function isUndefined(obj) { | ||
return obj === void 0; | ||
} | ||
function isA(typeName, value) { | ||
@@ -281,100 +263,10 @@ return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; | ||
var matches = func.toString().match(/^\s*function\s*(\w*)\s*\(/); | ||
const matches = func.toString().match(/^\s*function\s*(\w*)\s*\(/); | ||
return matches ? matches[1] : '<anonymous>'; | ||
} | ||
function Any(expectedObject) { | ||
if (typeof expectedObject === 'undefined') { | ||
throw new TypeError( | ||
'jasmine.any() expects to be passed a constructor function. ' + | ||
'Please pass one or use jasmine.anything() to match any object.'); | ||
} | ||
this.expectedObject = expectedObject; | ||
function isUndefined(obj) { | ||
return obj === void 0; | ||
} | ||
function any(expectedObject) { | ||
return new Any(expectedObject); | ||
} | ||
Any.prototype.asymmetricMatch = function (other) { | ||
if (this.expectedObject == String) { | ||
return typeof other == 'string' || other instanceof String; | ||
} | ||
if (this.expectedObject == Number) { | ||
return typeof other == 'number' || other instanceof Number; | ||
} | ||
if (this.expectedObject == Function) { | ||
return typeof other == 'function' || other instanceof Function; | ||
} | ||
if (this.expectedObject == Object) { | ||
return typeof other == 'object'; | ||
} | ||
if (this.expectedObject == Boolean) { | ||
return typeof other == 'boolean'; | ||
} | ||
return other instanceof this.expectedObject; | ||
}; | ||
Any.prototype.jasmineToString = function () { | ||
return '<jasmine.any(' + fnNameFor(this.expectedObject) + ')>'; | ||
}; | ||
function Anything() {} | ||
function anything() { | ||
return new Anything(); | ||
} | ||
Anything.prototype.asymmetricMatch = function (other) { | ||
return !isUndefined(other) && other !== null; | ||
}; | ||
Anything.prototype.jasmineToString = function () { | ||
return '<jasmine.anything>'; | ||
}; | ||
function ArrayContaining(sample) { | ||
this.sample = sample; | ||
} | ||
function arrayContaining(sample) { | ||
return new ArrayContaining(sample); | ||
} | ||
ArrayContaining.prototype.asymmetricMatch = function (other) { | ||
var className = Object.prototype.toString.call(this.sample); | ||
if (className !== '[object Array]') {throw new Error('You must provide an array to arrayContaining, not \'' + this.sample + '\'.');} | ||
for (var i = 0; i < this.sample.length; i++) { | ||
var item = this.sample[i]; | ||
if (!contains(other, item)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
ArrayContaining.prototype.jasmineToString = function () { | ||
return '<jasmine.arrayContaining(' + prettyFormat(this.sample) + ')>'; | ||
}; | ||
function ObjectContaining(sample) { | ||
this.sample = sample; | ||
} | ||
function objectContaining(sample) { | ||
return new ObjectContaining(sample); | ||
} | ||
function getPrototype(obj) { | ||
@@ -404,46 +296,8 @@ if (Object.getPrototypeOf) { | ||
ObjectContaining.prototype.asymmetricMatch = function (other) { | ||
if (typeof this.sample !== 'object') {throw new Error('You must provide an object to objectContaining, not \'' + this.sample + '\'.');} | ||
for (var property in this.sample) { | ||
if (!hasProperty(other, property) || | ||
!equals(this.sample[property], other[property])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
ObjectContaining.prototype.jasmineToString = function () { | ||
return '<jasmine.objectContaining(' + prettyFormat(this.sample) + ')>'; | ||
}; | ||
function StringMatching(expected) { | ||
if (!isA('String', expected) && !isA('RegExp', expected)) { | ||
throw new Error('Expected is not a String or a RegExp'); | ||
} | ||
this.regexp = new RegExp(expected); | ||
} | ||
function stringMatching(expected) { | ||
return new StringMatching(expected); | ||
} | ||
StringMatching.prototype.asymmetricMatch = function (other) { | ||
return this.regexp.test(other); | ||
}; | ||
StringMatching.prototype.jasmineToString = function () { | ||
return '<jasmine.stringMatching(' + this.regexp + ')>'; | ||
}; | ||
module.exports = { | ||
any, | ||
anything, | ||
arrayContaining, | ||
contains, | ||
equals, | ||
objectContaining, | ||
stringMatching }; | ||
fnNameFor, | ||
hasProperty, | ||
isA, | ||
isUndefined }; |
@@ -19,7 +19,7 @@ /** | ||
const diff = require('jest-diff');var _require = | ||
require('jest-util');const escapeStrForRegex = _require.escapeStrForRegex;var _require2 = | ||
require('./utils');const getPath = _require2.getPath;var _require3 = | ||
require('jest-regex-util');const escapeStrForRegex = _require.escapeStrForRegex;var _require2 = | ||
require('./utils');const getObjectSubset = _require2.getObjectSubset,getPath = _require2.getPath;var _require3 = | ||
@@ -32,2 +32,5 @@ | ||
require('jest-matcher-utils');const EXPECTED_COLOR = _require3.EXPECTED_COLOR,RECEIVED_COLOR = _require3.RECEIVED_COLOR,ensureNoExpected = _require3.ensureNoExpected,ensureNumbers = _require3.ensureNumbers,getType = _require3.getType,matcherHint = _require3.matcherHint,printReceived = _require3.printReceived,printExpected = _require3.printExpected,printWithType = _require3.printWithType;var _require4 = | ||
@@ -83,3 +86,14 @@ | ||
}; | ||
const isObjectWithKeys = a => a !== null && typeof a === 'object' && | ||
!(a instanceof Array) && !(a instanceof Date); | ||
const subsetEquality = (object, subset) => { | ||
if (!isObjectWithKeys(object) || !isObjectWithKeys(subset)) { | ||
return undefined; | ||
} | ||
return Object.keys(subset).every(key => | ||
object.hasOwnProperty(key) && | ||
equals(object[key], subset[key], [iterableEquality, subsetEquality])); | ||
}; | ||
const matchers = { | ||
@@ -92,5 +106,5 @@ toBe(received, expected) { | ||
`Expected value to not be (using ===):\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` : | ||
` ${printReceived(received)}` : | ||
() => { | ||
@@ -102,6 +116,6 @@ const diffString = diff(expected, received, { | ||
`Expected value to be (using ===):\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` + ( | ||
diffString ? `\n\nDifference:\n\n${ diffString }` : ''); | ||
` ${printReceived(received)}` + ( | ||
diffString ? `\n\nDifference:\n\n${diffString}` : ''); | ||
}; | ||
@@ -124,11 +138,11 @@ | ||
() => matcherHint('.not.toBeCloseTo', 'received', 'expected, precision') + '\n\n' + | ||
`Expected value not to be close to (with ${ printExpected(precision) }-digit precision):\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received: \n` + | ||
` ${ printReceived(actual) }` : | ||
`Expected value not to be close to (with ${printExpected(precision)}-digit precision):\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeCloseTo', 'received', 'expected, precision') + '\n\n' + | ||
`Expected value to be close to (with ${ printExpected(precision) }-digit precision):\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received: \n` + | ||
` ${ printReceived(actual) }`; | ||
`Expected value to be close to (with ${printExpected(precision)}-digit precision):\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${printReceived(actual)}`; | ||
@@ -144,6 +158,6 @@ return { message, pass }; | ||
`Expected value not to be defined, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeDefined', 'received', '') + '\n\n' + | ||
`Expected value to be defined, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -158,6 +172,6 @@ }, | ||
`Expected value not to be falsy, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeFalsy', 'received', '') + '\n\n' + | ||
`Expected value to be falsy, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -172,10 +186,10 @@ }, | ||
`Expected value not to be greater than:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeGreaterThan') + '\n\n' + | ||
`Expected value to be greater than:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -190,10 +204,10 @@ }, | ||
`Expected value not to be greater than or equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeGreaterThanOrEqual') + '\n\n' + | ||
`Expected value to be greater than or equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -209,3 +223,3 @@ }, | ||
`Expected constructor to be a function. Instead got:\n` + | ||
` ${ printExpected(constType) }`); | ||
` ${printExpected(constType)}`); | ||
@@ -218,12 +232,12 @@ } | ||
`Expected value not to be an instance of:\n` + | ||
` ${ printExpected(constructor.name || constructor) }\n` + | ||
` ${printExpected(constructor.name || constructor)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` : | ||
` ${printReceived(received)}\n` : | ||
() => matcherHint('.toBeInstanceOf', 'value', 'constructor') + '\n\n' + | ||
`Expected value to be an instance of:\n` + | ||
` ${ printExpected(constructor.name || constructor) }\n` + | ||
` ${printExpected(constructor.name || constructor)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + | ||
` ${printReceived(received)}\n` + | ||
`Constructor:\n` + | ||
` ${ printReceived(received.constructor && received.constructor.name) }`; | ||
` ${printReceived(received.constructor && received.constructor.name)}`; | ||
@@ -239,10 +253,10 @@ return { message, pass }; | ||
`Expected value not to be less than:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeLessThan') + '\n\n' + | ||
`Expected value to be less than:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -257,10 +271,10 @@ }, | ||
`Expected value not to be less than or equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeLessThanOrEqual') + '\n\n' + | ||
`Expected value to be less than or equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -275,6 +289,6 @@ }, | ||
`Expected value not to be NaN, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeNaN', 'received', '') + '\n\n' + | ||
`Expected value to be NaN, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -289,6 +303,6 @@ }, | ||
`Expected value not to be null, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeNull', 'received', '') + '\n\n' + | ||
`Expected value to be null, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -303,6 +317,6 @@ }, | ||
`Expected value not to be truthy, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeTruthy', 'received', '') + '\n\n' + | ||
`Expected value to be truthy, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
return { message, pass }; | ||
@@ -317,6 +331,6 @@ }, | ||
`Expected value not to be undefined, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
` ${printReceived(actual)}` : | ||
() => matcherHint('.toBeUndefined', 'received', '') + '\n\n' + | ||
`Expected value to be undefined, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
` ${printReceived(actual)}`; | ||
@@ -340,3 +354,3 @@ return { message, pass }; | ||
matcherHint('[.not].toContainEqual', 'collection', 'value') + '\n\n' + | ||
`Expected ${ RECEIVED_COLOR('collection') } to be an array-like structure.\n` + | ||
`Expected ${RECEIVED_COLOR('collection')} to be an array-like structure.\n` + | ||
printWithType('Received', collection, printReceived)); | ||
@@ -351,11 +365,11 @@ | ||
() => matcherHint('.not.toContain', collectionType, 'value') + '\n\n' + | ||
`Expected ${ collectionType }:\n` + | ||
` ${ printReceived(collection) }\n` + | ||
`Expected ${collectionType}:\n` + | ||
` ${printReceived(collection)}\n` + | ||
`Not to contain value:\n` + | ||
` ${ printExpected(value) }\n` : | ||
` ${printExpected(value)}\n` : | ||
() => matcherHint('.toContain', collectionType, 'value') + '\n\n' + | ||
`Expected ${ collectionType }:\n` + | ||
` ${ printReceived(collection) }\n` + | ||
`Expected ${collectionType}:\n` + | ||
` ${printReceived(collection)}\n` + | ||
`To contain value:\n` + | ||
` ${ printExpected(value) }`; | ||
` ${printExpected(value)}`; | ||
@@ -376,3 +390,3 @@ return { message, pass }; | ||
matcherHint('[.not].toContainEqual', 'collection', 'value') + '\n\n' + | ||
`Expected ${ RECEIVED_COLOR('collection') } to be an array-like structure.\n` + | ||
`Expected ${RECEIVED_COLOR('collection')} to be an array-like structure.\n` + | ||
printWithType('Received', collection, printReceived)); | ||
@@ -387,11 +401,11 @@ | ||
() => matcherHint('.not.toContainEqual', collectionType, 'value') + '\n\n' + | ||
`Expected ${ collectionType }:\n` + | ||
` ${ printReceived(collection) }\n` + | ||
`Expected ${collectionType}:\n` + | ||
` ${printReceived(collection)}\n` + | ||
`Not to contain a value equal to:\n` + | ||
` ${ printExpected(value) }\n` : | ||
` ${printExpected(value)}\n` : | ||
() => matcherHint('.toContainEqual', collectionType, 'value') + '\n\n' + | ||
`Expected ${ collectionType }:\n` + | ||
` ${ printReceived(collection) }\n` + | ||
`Expected ${collectionType}:\n` + | ||
` ${printReceived(collection)}\n` + | ||
`To contain a value equal to:\n` + | ||
` ${ printExpected(value) }`; | ||
` ${printExpected(value)}`; | ||
@@ -407,5 +421,5 @@ return { message, pass }; | ||
`Expected value to not equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` : | ||
` ${printReceived(received)}` : | ||
() => { | ||
@@ -417,6 +431,6 @@ const diffString = diff(expected, received, { | ||
`Expected value to equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
` ${printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` + ( | ||
diffString ? `\n\nDifference:\n\n${ diffString }` : ''); | ||
` ${printReceived(received)}` + ( | ||
diffString ? `\n\nDifference:\n\n${diffString}` : ''); | ||
}; | ||
@@ -439,6 +453,6 @@ | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + ( | ||
` ${printReceived(received)}\n` + ( | ||
received ? | ||
`received.length:\n ${ printReceived(received.length) }` : | ||
`received.length:\n ${printReceived(received.length)}` : | ||
'')); | ||
@@ -453,14 +467,14 @@ | ||
`Expected value to not have length:\n` + | ||
` ${ printExpected(length) }\n` + | ||
` ${printExpected(length)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + | ||
` ${printReceived(received)}\n` + | ||
`received.length:\n` + | ||
` ${ printReceived(received.length) }` : | ||
` ${printReceived(received.length)}` : | ||
() => matcherHint('.toHaveLength', 'received', 'length') + '\n\n' + | ||
`Expected value to have length:\n` + | ||
` ${ printExpected(length) }\n` + | ||
` ${printExpected(length)}\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + | ||
` ${printReceived(received)}\n` + | ||
`received.length:\n` + | ||
` ${ printReceived(received.length) }`; | ||
` ${printReceived(received.length)}`; | ||
@@ -476,4 +490,4 @@ return { message, pass }; | ||
matcherHint('[.not].toHaveProperty', 'object', 'path', { secondArgument: valuePassed ? 'value' : null }) + '\n\n' + | ||
`Expected ${ RECEIVED_COLOR('object') } to be an object. Received:\n` + | ||
` ${ getType(object) }: ${ printReceived(object) }`); | ||
`Expected ${RECEIVED_COLOR('object')} to be an object. Received:\n` + | ||
` ${getType(object)}: ${printReceived(object)}`); | ||
@@ -485,4 +499,4 @@ } | ||
matcherHint('[.not].toHaveProperty', 'object', 'path', { secondArgument: valuePassed ? 'value' : null }) + '\n\n' + | ||
`Expected ${ EXPECTED_COLOR('path') } to be a string. Received:\n` + | ||
` ${ getType(propPath) }: ${ printReceived(propPath) }`); | ||
`Expected ${EXPECTED_COLOR('path')} to be a string. Received:\n` + | ||
` ${getType(propPath)}: ${printReceived(propPath)}`); | ||
@@ -516,14 +530,14 @@ } | ||
`Expected the object:\n` + | ||
` ${ printReceived(object) }\n` + | ||
` ${printReceived(object)}\n` + | ||
`Not to have a nested property:\n` + | ||
` ${ printExpected(propPath) }\n` + ( | ||
valuePassed ? `With a value of:\n ${ printExpected(value) }\n` : '') : | ||
` ${printExpected(propPath)}\n` + ( | ||
valuePassed ? `With a value of:\n ${printExpected(value)}\n` : '') : | ||
matcherHint('.toHaveProperty', 'object', 'path', { secondArgument: valuePassed ? 'value' : null }) + '\n\n' + | ||
`Expected the object:\n` + | ||
` ${ printReceived(object) }\n` + | ||
` ${printReceived(object)}\n` + | ||
`To have a nested property:\n` + | ||
` ${ printExpected(propPath) }\n` + ( | ||
valuePassed ? `With a value of:\n ${ printExpected(value) }\n` : '') + ( | ||
traversedPath ? `Received:\n ${ RECEIVED_COLOR('object') }.${ traversedPath }: ${ printReceived(lastTraversedObject) }` : '') + ( | ||
diffString ? `\nDifference:\n\n${ diffString }` : ''); | ||
` ${printExpected(propPath)}\n` + ( | ||
valuePassed ? `With a value of:\n ${printExpected(value)}\n` : '') + ( | ||
traversedPath ? `Received:\n ${RECEIVED_COLOR('object')}.${traversedPath}: ${printReceived(lastTraversedObject)}` : '') + ( | ||
diffString ? `\nDifference:\n\n${diffString}` : ''); | ||
if (pass === undefined) { | ||
@@ -540,3 +554,3 @@ throw new Error('pass must be initialized'); | ||
matcherHint('[.not].toMatch', 'string', 'expected') + '\n\n' + | ||
`${ RECEIVED_COLOR('string') } value must be a string.\n` + | ||
`${RECEIVED_COLOR('string')} value must be a string.\n` + | ||
printWithType('Received', received, printReceived)); | ||
@@ -549,3 +563,3 @@ | ||
matcherHint('[.not].toMatch', 'string', 'expected') + '\n\n' + | ||
`${ EXPECTED_COLOR('expected') } value must be a string or a regular expression.\n` + | ||
`${EXPECTED_COLOR('expected')} value must be a string or a regular expression.\n` + | ||
printWithType('Expected', expected, printExpected)); | ||
@@ -563,10 +577,10 @@ | ||
`\n\nExpected value not to match:\n` + | ||
` ${ printExpected(expected) }` + | ||
` ${printExpected(expected)}` + | ||
`\nReceived:\n` + | ||
` ${ printReceived(received) }` : | ||
` ${printReceived(received)}` : | ||
() => matcherHint('.toMatch') + | ||
`\n\nExpected value to match:\n` + | ||
` ${ printExpected(expected) }` + | ||
` ${printExpected(expected)}` + | ||
`\nReceived:\n` + | ||
` ${ printReceived(received) }`; | ||
` ${printReceived(received)}`; | ||
@@ -581,3 +595,3 @@ return { message, pass }; | ||
matcherHint('[.not].toMatchObject', 'object', 'expected') + '\n\n' + | ||
`${ RECEIVED_COLOR('received') } value must be an object.\n` + | ||
`${RECEIVED_COLOR('received')} value must be an object.\n` + | ||
printWithType('Received', receivedObject, printReceived)); | ||
@@ -590,3 +604,3 @@ | ||
matcherHint('[.not].toMatchObject', 'object', 'expected') + '\n\n' + | ||
`${ EXPECTED_COLOR('expected') } value must be an object.\n` + | ||
`${EXPECTED_COLOR('expected')} value must be an object.\n` + | ||
printWithType('Expected', expectedObject, printExpected)); | ||
@@ -596,96 +610,12 @@ | ||
const compare = (expected, received) => { | ||
const pass = equals(receivedObject, expectedObject, [iterableEquality, subsetEquality]); | ||
if (typeof received !== typeof expected) { | ||
return false; | ||
} | ||
if (typeof expected !== 'object' || expected === null) { | ||
return expected === received; | ||
} | ||
if (Array.isArray(expected)) { | ||
if (!Array.isArray(received)) { | ||
return false; | ||
} | ||
if (expected.length !== received.length) { | ||
return false; | ||
} | ||
return expected.every((exp, i) => compare(exp, received[i])); | ||
} | ||
if (expected instanceof Date && received instanceof Date) { | ||
return expected.getTime() === received.getTime(); | ||
} | ||
return Object.keys(expected).every(key => { | ||
if (!received.hasOwnProperty(key)) { | ||
return false; | ||
} | ||
const exp = expected[key]; | ||
const act = received[key]; | ||
if (typeof exp === 'object' && exp !== null && act !== null) { | ||
return compare(exp, act); | ||
} | ||
return act === exp; | ||
}); | ||
}; | ||
// Strip properties form received object that are not present in the expected | ||
// object. We need it to print the diff without adding a lot of unrelated noise. | ||
const findMatchObject = (expected, received) => { | ||
if (Array.isArray(received)) { | ||
if (!Array.isArray(expected)) { | ||
return received; | ||
} | ||
if (expected.length !== received.length) { | ||
return received; | ||
} | ||
return expected.map((exp, i) => findMatchObject(exp, received[i])); | ||
} else if (received instanceof Date) { | ||
return received; | ||
} else if (typeof received === 'object' && received !== null && typeof expected === 'object' && expected !== null) { | ||
const matchedObject = {}; | ||
let match = false; | ||
Object.keys(expected).forEach(key => { | ||
if (received.hasOwnProperty(key)) { | ||
match = true; | ||
const exp = expected[key]; | ||
const act = received[key]; | ||
if (typeof exp === 'object' && exp !== null) { | ||
matchedObject[key] = findMatchObject(exp, act); | ||
} else { | ||
matchedObject[key] = act; | ||
} | ||
} | ||
}); | ||
if (match) { | ||
return matchedObject; | ||
} else { | ||
return received; | ||
} | ||
} else { | ||
return received; | ||
} | ||
}; | ||
const pass = compare(expectedObject, receivedObject); | ||
const message = pass ? | ||
() => matcherHint('.not.toMatchObject') + | ||
`\n\nExpected value not to match object:\n` + | ||
` ${ printExpected(expectedObject) }` + | ||
` ${printExpected(expectedObject)}` + | ||
`\nReceived:\n` + | ||
` ${ printReceived(receivedObject) }` : | ||
` ${printReceived(receivedObject)}` : | ||
() => { | ||
const diffString = diff(expectedObject, findMatchObject(expectedObject, receivedObject), { | ||
const diffString = diff(expectedObject, getObjectSubset(receivedObject, expectedObject), { | ||
expand: this.expand }); | ||
@@ -695,6 +625,6 @@ | ||
`\n\nExpected value to match object:\n` + | ||
` ${ printExpected(expectedObject) }` + | ||
` ${printExpected(expectedObject)}` + | ||
`\nReceived:\n` + | ||
` ${ printReceived(receivedObject) }` + ( | ||
diffString ? `\nDifference:\n${ diffString }` : ''); | ||
` ${printReceived(receivedObject)}` + ( | ||
diffString ? `\nDifference:\n${diffString}` : ''); | ||
}; | ||
@@ -701,0 +631,0 @@ |
@@ -53,6 +53,6 @@ /** | ||
'\n\n' + | ||
`Expected ${ type } not to be called ` + | ||
`Expected ${type} not to be called ` + | ||
formatReceivedCalls(calls, CALL_PRINT_LIMIT, { sameSentence: true }) : | ||
() => matcherHint(matcherName, RECEIVED_NAME[type], '') + '\n\n' + | ||
`Expected ${ type } to have been called.`; | ||
`Expected ${type} to have been called.`; | ||
@@ -78,7 +78,7 @@ return { message, pass }; | ||
() => matcherHint('.not' + matcherName, RECEIVED_NAME[type]) + '\n\n' + | ||
`Expected ${ type } not to have been called with:\n` + | ||
` ${ printExpected(expected) }` : | ||
`Expected ${type} not to have been called with:\n` + | ||
` ${printExpected(expected)}` : | ||
() => matcherHint(matcherName, RECEIVED_NAME[type]) + '\n\n' + | ||
`Expected ${ type } to have been called with:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Expected ${type} to have been called with:\n` + | ||
` ${printExpected(expected)}\n` + | ||
formatReceivedCalls(calls, CALL_PRINT_LIMIT); | ||
@@ -105,7 +105,7 @@ | ||
() => matcherHint('.not' + matcherName, RECEIVED_NAME[type]) + '\n\n' + | ||
`Expected ${ type } to not have been last called with:\n` + | ||
` ${ printExpected(expected) }` : | ||
`Expected ${type} to not have been last called with:\n` + | ||
` ${printExpected(expected)}` : | ||
() => matcherHint(matcherName, RECEIVED_NAME[type]) + '\n\n' + | ||
`Expected ${ type } to have been last called with:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Expected ${type} to have been last called with:\n` + | ||
` ${printExpected(expected)}\n` + | ||
formatReceivedCalls(calls, LAST_CALL_PRINT_LIMIT, { isLast: true }); | ||
@@ -140,10 +140,10 @@ | ||
`\n\n` + | ||
`Expected ${ type } not to be called ` + | ||
`${ EXPECTED_COLOR(pluralize('time', expected)) }, but it was` + | ||
` called exactly ${ RECEIVED_COLOR(pluralize('time', count)) }.` : | ||
`Expected ${type} not to be called ` + | ||
`${EXPECTED_COLOR(pluralize('time', expected))}, but it was` + | ||
` called exactly ${RECEIVED_COLOR(pluralize('time', count))}.` : | ||
() => matcherHint(matcherName, RECEIVED_NAME[type], String(expected)) + | ||
'\n\n' + | ||
`Expected ${ type } to have been called ` + | ||
`${ EXPECTED_COLOR(pluralize('time', expected)) },` + | ||
` but it was called ${ RECEIVED_COLOR(pluralize('time', count)) }.`; | ||
`Expected ${type} to have been called ` + | ||
`${EXPECTED_COLOR(pluralize('time', expected))},` + | ||
` but it was called ${RECEIVED_COLOR(pluralize('time', count))}.`; | ||
@@ -167,3 +167,3 @@ return { message, pass }; | ||
matcherHint('[.not]' + matcherName, 'jest.fn()', '') + '\n\n' + | ||
`${ RECEIVED_COLOR('jest.fn()') } value must be a mock function or spy.\n` + | ||
`${RECEIVED_COLOR('jest.fn()')} value must be a mock function or spy.\n` + | ||
printWithType('Received', mockOrSpy, printReceived)); | ||
@@ -185,3 +185,3 @@ | ||
return ( | ||
`${ but } it was ${ options && options.isLast ? 'last ' : '' }called ` + | ||
`${but} it was ${options && options.isLast ? 'last ' : ''}called ` + | ||
`with:\n ` + printedCalls + ( | ||
@@ -194,3 +194,3 @@ count > 0 ? | ||
} else { | ||
return `But it was ${ RECEIVED_COLOR('not called') }.`; | ||
return `But it was ${RECEIVED_COLOR('not called')}.`; | ||
} | ||
@@ -197,0 +197,0 @@ }; |
@@ -18,6 +18,7 @@ /** | ||
require('jest-regex-util');const escapeStrForRegex = _require.escapeStrForRegex;var _require2 = | ||
require('jest-util');const escapeStrForRegex = _require.escapeStrForRegex,formatStackTrace = _require.formatStackTrace,separateMessageFromStack = _require.separateMessageFromStack;var _require2 = | ||
require('jest-message-util');const formatStackTrace = _require2.formatStackTrace,separateMessageFromStack = _require2.separateMessageFromStack;var _require3 = | ||
@@ -28,7 +29,10 @@ | ||
require('jest-matcher-utils');const RECEIVED_COLOR = _require2.RECEIVED_COLOR,getType = _require2.getType,matcherHint = _require2.matcherHint,printExpected = _require2.printExpected,printWithType = _require2.printWithType;var _require3 = | ||
require('./jasmine-utils');const equals = _require3.equals; | ||
require('jest-matcher-utils');const RECEIVED_BG = _require3.RECEIVED_BG,RECEIVED_COLOR = _require3.RECEIVED_COLOR,getType = _require3.getType,highlightTrailingWhitespace = _require3.highlightTrailingWhitespace,matcherHint = _require3.matcherHint,printExpected = _require3.printExpected,printWithType = _require3.printWithType;var _require4 = | ||
require('./jasmine-utils');const equals = _require4.equals; | ||
const createMatcher = matcherName => | ||
@@ -71,3 +75,3 @@ (actual, expected) => { | ||
'Unexpected argument passed.\nExpected: ' + | ||
`${ printExpected('string') }, ${ printExpected('Error (type)') } or ${ printExpected('regexp') }.\n` + | ||
`${printExpected('string')}, ${printExpected('Error (type)')} or ${printExpected('regexp')}.\n` + | ||
printWithType('Got', String(expected), printExpected)); | ||
@@ -97,7 +101,7 @@ | ||
`Expected the function not to throw an error matching:\n` + | ||
` ${ printExpected(value) }\n` + | ||
` ${printExpected(value)}\n` + | ||
printActualErrorMessage(error) : | ||
() => matcherHint(name, 'function', getType(value)) + '\n\n' + | ||
`Expected the function to throw an error matching:\n` + | ||
` ${ printExpected(value) }\n` + | ||
` ${printExpected(value)}\n` + | ||
printActualErrorMessage(error); | ||
@@ -121,7 +125,7 @@ | ||
`Expected the function not to throw an error matching:\n` + | ||
` ${ printExpected(expectedError) }\n` + | ||
` ${printExpected(expectedError)}\n` + | ||
printActualErrorMessage(error) : | ||
() => matcherHint(name, 'function', 'error') + '\n\n' + | ||
`Expected the function to throw an error matching:\n` + | ||
` ${ printExpected(expectedError) }\n` + | ||
` ${printExpected(expectedError)}\n` + | ||
printActualErrorMessage(error); | ||
@@ -141,7 +145,7 @@ | ||
`Expected the function not to throw an error of type:\n` + | ||
` ${ printExpected(ErrorClass.name) }\n` + | ||
` ${printExpected(ErrorClass.name)}\n` + | ||
printActualErrorMessage(error) : | ||
() => matcherHint(name, 'function', 'type') + '\n\n' + | ||
`Expected the function to throw an error of type:\n` + | ||
` ${ printExpected(ErrorClass.name) }\n` + | ||
` ${printExpected(ErrorClass.name)}\n` + | ||
printActualErrorMessage(error); | ||
@@ -158,6 +162,8 @@ | ||
RECEIVED_COLOR( | ||
' ' + message + formatStackTrace(stack, { | ||
' ' + | ||
highlightTrailingWhitespace(message, RECEIVED_BG) + | ||
formatStackTrace(stack, { | ||
noStackTrace: false, | ||
rootDir: process.cwd(), | ||
testRegex: '' }))); | ||
testMatch: [] }))); | ||
@@ -164,0 +170,0 @@ |
@@ -10,3 +10,2 @@ /** | ||
*/ | ||
/* eslint-disable max-len */ | ||
@@ -22,3 +21,6 @@ 'use strict'; | ||
const getPath = (object, propertyPath) => { | ||
const getPath = ( | ||
object, | ||
propertyPath) => | ||
{ | ||
if (!Array.isArray(propertyPath)) { | ||
@@ -34,5 +36,5 @@ propertyPath = propertyPath.split('.'); | ||
if (!lastProp && (newObject === null || newObject === undefined)) { | ||
// This is not the last prop in the chain. If we keep recursing it will hit a | ||
// `can't access property X of undefined | null`. At this point we know that the | ||
// chain broken and we return right away. | ||
// This is not the last prop in the chain. If we keep recursing it will | ||
// hit a `can't access property X of undefined | null`. At this point we | ||
// know that the chain broken and we return right away. | ||
return { | ||
@@ -65,4 +67,30 @@ hasEndProp: false, | ||
// Strip properties from object that are not present in the subset. Useful for | ||
// printing the diff for toMatchObject() without adding unrelated noise. | ||
const getObjectSubset = (object, subset) => { | ||
if (Array.isArray(object)) { | ||
if (Array.isArray(subset) && subset.length === object.length) { | ||
return subset.map((sub, i) => getObjectSubset(object[i], sub)); | ||
} | ||
} else if (object instanceof Date) { | ||
return object; | ||
} else if ( | ||
typeof object === 'object' && object !== null && | ||
typeof subset === 'object' && subset !== null) | ||
{ | ||
const trimmed = {}; | ||
Object.keys(subset). | ||
filter(key => object.hasOwnProperty(key)). | ||
forEach(key => trimmed[key] = getObjectSubset(object[key], subset[key])); | ||
if (Object.keys(trimmed).length > 0) { | ||
return trimmed; | ||
} | ||
} | ||
return object; | ||
}; | ||
module.exports = { | ||
getObjectSubset, | ||
getPath }; |
{ | ||
"name": "jest-matchers", | ||
"version": "18.1.0", | ||
"version": "18.5.0-alpha.7da3df39", | ||
"repository": { | ||
@@ -11,10 +11,7 @@ "type": "git", | ||
"dependencies": { | ||
"jest-diff": "^18.1.0", | ||
"jest-matcher-utils": "^18.1.0", | ||
"jest-util": "^18.1.0", | ||
"pretty-format": "^18.1.0" | ||
}, | ||
"scripts": { | ||
"test": "../../packages/jest-cli/bin/jest.js" | ||
"jest-diff": "^18.5.0-alpha.7da3df39", | ||
"jest-matcher-utils": "^18.5.0-alpha.7da3df39", | ||
"jest-message-util": "^18.5.0-alpha.7da3df39", | ||
"jest-regex-util": "^18.5.0-alpha.7da3df39" | ||
} | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
52298
9
1393
2
+ Addedansi-styles@3.2.1(transitive)
+ Addedarr-diff@2.0.0(transitive)
+ Addedarr-flatten@1.1.0(transitive)
+ Addedarray-unique@0.2.1(transitive)
+ Addedbraces@1.8.5(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedexpand-brackets@0.1.5(transitive)
+ Addedexpand-range@1.8.2(transitive)
+ Addedextglob@0.3.2(transitive)
+ Addedfilename-regex@2.0.1(transitive)
+ Addedfill-range@2.2.4(transitive)
+ Addedfor-in@1.0.2(transitive)
+ Addedfor-own@0.1.5(transitive)
+ Addedglob-base@0.3.0(transitive)
+ Addedglob-parent@2.0.0(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-dotfile@1.0.3(transitive)
+ Addedis-equal-shallow@0.1.3(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedis-extglob@1.0.0(transitive)
+ Addedis-glob@2.0.1(transitive)
+ Addedis-number@2.1.04.0.0(transitive)
+ Addedis-posix-bracket@0.1.1(transitive)
+ Addedis-primitive@2.0.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@2.1.0(transitive)
+ Addedjest-diff@18.5.0-alpha.7da3df39(transitive)
+ Addedjest-matcher-utils@18.5.0-alpha.7da3df39(transitive)
+ Addedjest-message-util@18.5.0-alpha.7da3df39(transitive)
+ Addedjest-regex-util@18.5.0-alpha.7da3df39(transitive)
+ Addedkind-of@3.2.26.0.3(transitive)
+ Addedmath-random@1.0.4(transitive)
+ Addedmicromatch@2.3.11(transitive)
+ Addednormalize-path@2.1.1(transitive)
+ Addedobject.omit@2.0.1(transitive)
+ Addedparse-glob@3.0.4(transitive)
+ Addedpreserve@0.2.0(transitive)
+ Addedpretty-format@18.5.0-alpha.7da3df39(transitive)
+ Addedrandomatic@3.1.1(transitive)
+ Addedregex-cache@0.4.4(transitive)
+ Addedremove-trailing-separator@1.1.0(transitive)
+ Addedrepeat-element@1.1.4(transitive)
+ Addedrepeat-string@1.6.1(transitive)
- Removedjest-util@^18.1.0
- Removedpretty-format@^18.1.0
- Removedgraceful-fs@4.2.11(transitive)
- Removedjest-diff@18.1.0(transitive)
- Removedjest-file-exists@17.0.0(transitive)
- Removedjest-matcher-utils@18.1.0(transitive)
- Removedjest-mock@18.0.0(transitive)
- Removedjest-util@18.1.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedpretty-format@18.1.0(transitive)