jest-matchers
Advanced tools
@@ -23,5 +23,11 @@ /** | ||
const matchers = require('./matchers'); | ||
const spyMatchers = require('./spy-matchers'); | ||
const spyMatchers = require('./spyMatchers'); | ||
const toThrowMatchers = require('./toThrowMatchers');var _require = | ||
require('jest-matcher-utils');const stringify = _require.stringify; | ||
const GLOBAL_MATCHERS_OBJECT_SYMBOL = Symbol.for('$$jest-matchers-object'); | ||
class JestAssertionError extends Error {} | ||
if (!global[GLOBAL_MATCHERS_OBJECT_SYMBOL]) { | ||
@@ -61,2 +67,4 @@ Object.defineProperty( | ||
_validateResult(result); | ||
if (result.pass && isNot || !result.pass && !isNot) {// XOR | ||
@@ -71,3 +79,3 @@ let message = result.message; | ||
const error = new Error(message); | ||
const error = new JestAssertionError(message); | ||
// Remove this function from the stack trace frame. | ||
@@ -84,5 +92,25 @@ Error.captureStackTrace(error, throwingMatcher); | ||
const _validateResult = result => { | ||
if ( | ||
typeof result !== 'object' || | ||
typeof result.pass !== 'boolean' || | ||
!( | ||
typeof result.message === 'string' || | ||
typeof result.message === 'function')) | ||
{ | ||
throw new Error( | ||
'Unexpected return from a matcher function.\n' + | ||
'Matcher functions should ' + | ||
'return an object in the following format:\n' + | ||
' {message: string | function, pass: boolean}\n' + | ||
`'${ stringify(result) }' was returned`); | ||
} | ||
}; | ||
// add default jest matchers | ||
addMatchers(matchers); | ||
addMatchers(spyMatchers); | ||
addMatchers(toThrowMatchers); | ||
@@ -89,0 +117,0 @@ module.exports = { |
@@ -10,2 +10,3 @@ /** | ||
*/ | ||
/* eslint-disable max-len */ | ||
@@ -17,2 +18,3 @@ 'use strict'; | ||
const diff = require('jest-diff');var _require = | ||
require('jest-util');const escapeStrForRegex = _require.escapeStrForRegex;var _require2 = | ||
@@ -22,4 +24,43 @@ | ||
require('jest-matcher-utils');const stringify = _require.stringify;const ensureNoExpected = _require.ensureNoExpected;const ensureNumbers = _require.ensureNumbers; | ||
require('jest-matcher-utils');const ensureNoExpected = _require2.ensureNoExpected;const ensureNumbers = _require2.ensureNumbers;const highlight = _require2.highlight;const printExpected = _require2.printExpected; | ||
const equals = global.jasmine.matchersUtil.equals; | ||
const hasIterator = object => !!(object != null && object[Symbol.iterator]); | ||
const iterableEquality = (a, b) => { | ||
if ( | ||
typeof a !== 'object' || | ||
typeof b !== 'object' || | ||
Array.isArray(a) || | ||
Array.isArray(b) || | ||
!hasIterator(a) || | ||
!hasIterator(b)) | ||
{ | ||
return undefined; | ||
} | ||
if (a.constructor !== b.constructor) { | ||
return false; | ||
} | ||
const bIterator = b[Symbol.iterator](); | ||
for (const aValue of a) { | ||
const nextB = bIterator.next(); | ||
if ( | ||
nextB.done || | ||
!global.jasmine.matchersUtil.equals( | ||
aValue, | ||
nextB.value, | ||
[iterableEquality])) | ||
{ | ||
return false; | ||
} | ||
} | ||
if (!bIterator.next().done) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const matchers = { | ||
@@ -33,4 +74,4 @@ toBe(actual, expected) { | ||
message() { | ||
return `expected '${ stringify(actual) }' not to be` + | ||
` '${ stringify(expected) }' (using '!==')`; | ||
return `Received ${ highlight(actual) } but expected it not to ` + | ||
`be ${ printExpected(expected) } (using '!==').`; | ||
} }; | ||
@@ -42,6 +83,6 @@ | ||
message() { | ||
let diffString = '\n\n'; | ||
diffString += diff(expected, actual); | ||
return `expected '${ stringify(actual) }' to be` + | ||
` '${ stringify(expected) }' (using '===')${ diffString }`; | ||
const diffString = diff(expected, actual); | ||
return `Received ${ highlight(actual) } but expected ` + | ||
`${ printExpected(expected) } (using '===').` + ( | ||
diffString ? '\n\n' + diffString : ''); | ||
} }; | ||
@@ -52,2 +93,25 @@ | ||
toEqual(actual, expected) { | ||
const pass = equals(actual, expected, [iterableEquality]); | ||
if (pass) { | ||
return { | ||
pass, | ||
message() { | ||
return `Received ${ highlight(actual) } but expected it not ` | ||
`to equal ${ printExpected(expected) }.`; | ||
} }; | ||
} else { | ||
return { | ||
pass, | ||
message() { | ||
const diffString = diff(expected, actual); | ||
return `Received ${ highlight(actual) } but expected it to equal ` + | ||
`${ printExpected(expected) }.` + (diffString ? '\n\n' + diffString : ''); | ||
} }; | ||
} | ||
}, | ||
toBeTruthy(actual, expected) { | ||
@@ -57,4 +121,4 @@ ensureNoExpected(expected, 'toBeTruthy'); | ||
const message = pass ? | ||
() => `expected '${ stringify(actual) }' not to be truthy` : | ||
() => `expected '${ stringify(actual) }' to be truthy`; | ||
() => `Received ${ highlight(actual) } but expected it to be truthy.` : | ||
() => `Received ${ highlight(actual) } but expected it not to be truthy.`; | ||
@@ -68,4 +132,4 @@ return { message, pass }; | ||
const message = pass ? | ||
() => `expected '${ stringify(actual) }' not to be falsy` : | ||
() => `expected '${ stringify(actual) }' to be falsy`; | ||
() => `Received ${ highlight(actual) } but expected it not to be falsy.` : | ||
() => `Received ${ highlight(actual) } but expected it to be falsy.`; | ||
@@ -79,4 +143,4 @@ return { message, pass }; | ||
const message = pass ? | ||
() => `expected '${ stringify(actual) }' not to be NaN` : | ||
() => `expected '${ stringify(actual) }' to be NaN`; | ||
() => `Received ${ highlight(actual) } but expected it not to be NaN.` : | ||
() => `Received ${ highlight(actual) } but expected it to be NaN.`; | ||
@@ -90,4 +154,4 @@ return { message, pass }; | ||
const message = pass ? | ||
() => `expected '${ stringify(actual) }' not to be null` : | ||
() => `expected '${ stringify(actual) }' to be null`; | ||
() => `Received ${ highlight(actual) } but expected it not to be null.` : | ||
() => `Received ${ highlight(actual) } but expected it to be null.`; | ||
@@ -101,4 +165,4 @@ return { message, pass }; | ||
const message = pass ? | ||
() => `expected '${ stringify(actual) }' not to be defined` : | ||
() => `expected '${ stringify(actual) }' to be defined`; | ||
() => `Received ${ highlight(actual) } but expected it not to be defined.` : | ||
() => `Received ${ highlight(actual) } but expected it to be defined.`; | ||
@@ -112,4 +176,4 @@ return { message, pass }; | ||
const message = pass ? | ||
() => `expected '${ stringify(actual) }' not to be undefined` : | ||
() => `expected '${ stringify(actual) }' to be undefined`; | ||
() => `Received ${ highlight(actual) } but expected it not to be undefined.` : | ||
() => `Received ${ highlight(actual) } but expected it to be undefined.`; | ||
@@ -123,4 +187,4 @@ return { message, pass }; | ||
const message = pass ? | ||
`expected '${ actual }' not to be greater than '${ expected }' (using >)` : | ||
`expected '${ actual }' to be greater than '${ expected }' (using >)`; | ||
`Received ${ highlight(actual) } but expected it not to be greater than ${ printExpected(expected) } (using >.)` : | ||
`Received ${ highlight(actual) } but expected it to be greater than ${ printExpected(expected) } (using >).`; | ||
return { message, pass }; | ||
@@ -133,6 +197,4 @@ }, | ||
const message = pass ? | ||
`expected '${ actual }' not to be greater than or equal ` + | ||
`'${ expected }' (using >=)` : | ||
`expected '${ actual }' to be greater than or equal ` + | ||
`'${ expected }' (using >=)`; | ||
`Received ${ highlight(actual) } but expected it not to be greater than or equal ${ printExpected(expected) } (using >=).` : | ||
`Received ${ highlight(actual) } but expected it to be greater than or equal ${ printExpected(expected) } (using >=).`; | ||
return { message, pass }; | ||
@@ -145,4 +207,4 @@ }, | ||
const message = pass ? | ||
`expected '${ actual }' not to be less than '${ expected }' (using <)` : | ||
`expected '${ actual }' to be less than '${ expected }' (using <)`; | ||
`Received ${ highlight(actual) } but expected it not to be less than ${ printExpected(expected) } (using <).` : | ||
`Received ${ highlight(actual) } but expected it to be less than ${ printExpected(expected) } (using <).`; | ||
return { message, pass }; | ||
@@ -155,6 +217,4 @@ }, | ||
const message = pass ? | ||
`expected '${ actual }' not to be less than or equal ` + | ||
`'${ expected }' (using <=)` : | ||
`expected '${ actual }' to be less than or equal ` + | ||
`'${ expected }' (using <=)`; | ||
`Received ${ highlight(actual) } but expected it not to be less than or equal ${ printExpected(expected) } (using <=).` : | ||
`Received ${ highlight(actual) } but expected it to be less than or equal ${ printExpected(expected) } (using <=).`; | ||
return { message, pass }; | ||
@@ -166,5 +226,3 @@ }, | ||
throw new Error( | ||
'.toContain() works only on arrays and strings. ' + | ||
`'${ typeof actual }': ` + | ||
`'${ stringify(actual) }' was passed`); | ||
`.toContain() only works with arrays and strings. ${ typeof actual }: ${ highlight(actual) } was passed.`); | ||
@@ -175,6 +233,4 @@ } | ||
const message = pass ? | ||
() => `expected '${ stringify(actual) }' not to contain ` + | ||
`'${ stringify(expected) }'` : | ||
() => `expected '${ stringify(actual) }' to contain ` + | ||
`'${ stringify(expected) }'`; | ||
() => `Received ${ highlight(actual) } but expected it not to contain ${ printExpected(expected) }.` : | ||
() => `Received ${ highlight(actual) } but expected it to contain ${ printExpected(expected) }.`; | ||
@@ -188,6 +244,4 @@ return { message, pass }; | ||
const message = pass ? | ||
() => `expected '${ actual }' not to be close to '${ expected }'` + | ||
` with ${ precision }-digit precision` : | ||
() => `expected '${ actual }' to be close to '${ expected }'` + | ||
` with ${ precision }-digit precision`; | ||
() => `Received ${ highlight(actual) } but expected it not to be close to ${ printExpected(expected) } with ${ printExpected(precision) }-digit precision.` : | ||
() => `Received ${ highlight(actual) } but expected it to be close to ${ printExpected(expected) } with ${ printExpected(precision) }-digit precision.`; | ||
@@ -201,17 +255,19 @@ return { message, pass }; | ||
pass: false, | ||
message: `actual '${ actual }' is not a String` }; | ||
message: `Received ${ highlight(actual) } and expected it to be a string.` }; | ||
} | ||
if (!(expected instanceof RegExp) && !(typeof expected == 'string')) { | ||
const isString = typeof expected == 'string'; | ||
if (!(expected instanceof RegExp) && !isString) { | ||
return { | ||
pass: false, | ||
message: `expected '${ expected }' is not a String or a RegExp` }; | ||
message: `Expected ${ printExpected(expected) } to be a string or regular expression.` }; | ||
} | ||
const pass = new RegExp(expected).test(actual); | ||
const pass = new RegExp(isString ? escapeStrForRegex(expected) : expected). | ||
test(actual); | ||
const message = pass ? | ||
() => `expected '${ actual }' not to match '${ stringify(expected) }'` : | ||
() => `expected '${ actual }' to match '${ stringify(expected) }'`; | ||
() => `Received ${ highlight(actual) } but expected it not to match ${ printExpected(expected) }.` : | ||
() => `Received ${ highlight(actual) } but expected it to match ${ printExpected(expected) }.`; | ||
@@ -218,0 +274,0 @@ return { message, pass }; |
{ | ||
"name": "jest-matchers", | ||
"version": "14.2.2-alpha.22bd3c33", | ||
"version": "14.3.0-alpha.d13c163e", | ||
"repository": { | ||
@@ -11,4 +11,5 @@ "type": "git", | ||
"dependencies": { | ||
"jest-diff": "^14.2.2-alpha.22bd3c33", | ||
"jest-matcher-utils": "^14.2.2-alpha.22bd3c33" | ||
"jest-diff": "^14.3.0-alpha.d13c163e", | ||
"jest-matcher-utils": "^14.3.0-alpha.d13c163e", | ||
"jest-util": "^14.3.0-alpha.d13c163e" | ||
}, | ||
@@ -15,0 +16,0 @@ "devDependencies": { |
16821
59.47%7
16.67%427
58.15%3
50%