jest-matchers
Advanced tools
Comparing version 17.0.2 to 17.0.3
@@ -36,2 +36,10 @@ /** | ||
const IteratorSymbol = Symbol.iterator; | ||
@@ -301,10 +309,22 @@ const equals = global.jasmine.matchersUtil.equals; | ||
const collectionType = getType(collection); | ||
if (!Array.isArray(collection) && typeof collection !== 'string') { | ||
throw new Error( | ||
`.toContain() only works with arrays and strings.\n` + | ||
printWithType('Received', collection, printReceived)); | ||
let converted = null; | ||
if (Array.isArray(collection) || typeof collection === 'string') { | ||
// strings have `indexOf` so we don't need to convert | ||
// arrays have `indexOf` and we don't want to make a copy | ||
converted = collection; | ||
} else { | ||
try { | ||
converted = Array.from(collection); | ||
} catch (e) { | ||
throw new Error( | ||
matcherHint('[.not].toContainEqual', 'collection', 'value') + '\n\n' + | ||
`Expected ${ RECEIVED_COLOR('collection') } to be an array-like structure.\n` + | ||
printWithType('Received', collection, printReceived)); | ||
} | ||
} | ||
const pass = collection.indexOf(value) != -1; | ||
// At this point, we're either a string or an Array, | ||
// which was converted from an array-like structure. | ||
const pass = converted.indexOf(value) != -1; | ||
const message = pass ? | ||
@@ -327,11 +347,19 @@ () => matcherHint('.not.toContain', collectionType, 'value') + '\n\n' + | ||
const collectionType = getType(collection); | ||
if (!Array.isArray(collection)) { | ||
throw new Error( | ||
`.toContainEqual() only works with arrays.\n` + | ||
printWithType('Received', collection, printReceived)); | ||
let converted = null; | ||
if (Array.isArray(collection)) { | ||
converted = collection; | ||
} else { | ||
try { | ||
converted = Array.from(collection); | ||
} catch (e) { | ||
throw new Error( | ||
matcherHint('[.not].toContainEqual', 'collection', 'value') + '\n\n' + | ||
`Expected ${ RECEIVED_COLOR('collection') } to be an array-like structure.\n` + | ||
printWithType('Received', collection, printReceived)); | ||
} | ||
} | ||
const pass = | ||
collection.findIndex(item => equals(item, value, [iterableEquality])) !== -1; | ||
converted.findIndex(item => equals(item, value, [iterableEquality])) !== -1; | ||
const message = pass ? | ||
@@ -376,2 +404,40 @@ () => matcherHint('.not.toContainEqual', collectionType, 'value') + '\n\n' + | ||
toHaveLength(received, length) { | ||
if ( | ||
typeof received !== 'string' && ( | ||
!received || typeof received.length !== 'number')) | ||
{ | ||
throw new Error( | ||
matcherHint('[.not].toHaveLength', 'received', 'length') + '\n\n' + | ||
`Expected value to have a 'length' prorerty that is a number. ` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + ( | ||
received ? | ||
`received.length:\n ${ printReceived(received.length) }` : | ||
'')); | ||
} | ||
const pass = received.length === length; | ||
const message = pass ? | ||
() => matcherHint('.not.toHaveLength', 'received', 'length') + '\n\n' + | ||
`Expected value to not have length:\n` + | ||
` ${ printExpected(length) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + | ||
`received.length:\n` + | ||
` ${ printReceived(received.length) }` : | ||
() => matcherHint('.toHaveLength', 'received', 'length') + '\n\n' + | ||
`Expected value to have length:\n` + | ||
` ${ printExpected(length) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + | ||
`received.length:\n` + | ||
` ${ printReceived(received.length) }`; | ||
return { message, pass }; | ||
}, | ||
toMatch(received, expected) { | ||
@@ -378,0 +444,0 @@ if (typeof received !== 'string') { |
{ | ||
"name": "jest-matchers", | ||
"version": "17.0.2", | ||
"version": "17.0.3", | ||
"repository": { | ||
@@ -11,4 +11,4 @@ "type": "git", | ||
"dependencies": { | ||
"jest-diff": "^17.0.2", | ||
"jest-matcher-utils": "^17.0.1", | ||
"jest-diff": "^17.0.3", | ||
"jest-matcher-utils": "^17.0.3", | ||
"jest-util": "^17.0.2" | ||
@@ -15,0 +15,0 @@ }, |
30790
786
Updatedjest-diff@^17.0.3
Updatedjest-matcher-utils@^17.0.3