jest-matchers
Advanced tools
@@ -23,4 +23,8 @@ /** | ||
require('jest-matcher-utils');const ensureNoExpected = _require2.ensureNoExpected;const ensureNumbers = _require2.ensureNumbers;const highlight = _require2.highlight;const printExpected = _require2.printExpected; | ||
require('jest-matcher-utils');const EXPECTED_COLOR = _require2.EXPECTED_COLOR;const RECEIVED_COLOR = _require2.RECEIVED_COLOR;const ensureNoExpected = _require2.ensureNoExpected;const ensureNumbers = _require2.ensureNumbers;const getType = _require2.getType;const matcherHint = _require2.matcherHint;const printReceived = _require2.printReceived;const printExpected = _require2.printExpected; | ||
const equals = global.jasmine.matchersUtil.equals; | ||
@@ -65,47 +69,44 @@ | ||
const matchers = { | ||
toBe(actual, expected) { | ||
const pass = actual === expected; | ||
toBe(received, expected) { | ||
const pass = received === expected; | ||
if (pass) { | ||
return { | ||
pass, | ||
message() { | ||
return `Received ${ highlight(actual) } but expected it not to ` + | ||
`be ${ printExpected(expected) } (using '!==').`; | ||
} }; | ||
const message = pass ? | ||
() => matcherHint('.not.toBe') + '\n\n' + | ||
`Expected value to not be (using ===):\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` : | ||
() => { | ||
const diffString = diff(expected, received); | ||
return matcherHint('.toBe') + '\n\n' + | ||
`Expected value to be (using ===):\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` + ( | ||
diffString ? `\n\nDifference:\n\n${ diffString }` : ''); | ||
}; | ||
} else { | ||
return { | ||
pass, | ||
message() { | ||
const diffString = diff(expected, actual); | ||
return `Received ${ highlight(actual) } but expected ` + | ||
`${ printExpected(expected) } (using '===').` + ( | ||
diffString ? '\n\n' + diffString : ''); | ||
} }; | ||
} | ||
return { message, pass }; | ||
}, | ||
toEqual(actual, expected) { | ||
const pass = equals(actual, expected, [iterableEquality]); | ||
toEqual(received, expected) { | ||
const pass = equals(received, expected, [iterableEquality]); | ||
if (pass) { | ||
return { | ||
pass, | ||
message() { | ||
return `Received ${ highlight(actual) } but expected it not ` | ||
`to equal ${ printExpected(expected) }.`; | ||
} }; | ||
const message = pass ? | ||
() => matcherHint('.not.toEqual') + '\n\n' + | ||
`Expected value to not equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` : | ||
() => { | ||
const diffString = diff(expected, received); | ||
return matcherHint('.toEqual') + '\n\n' + | ||
`Expected value to equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` + ( | ||
diffString ? `\n\nDifference:\n\n${ diffString }` : ''); | ||
}; | ||
} else { | ||
return { | ||
pass, | ||
message() { | ||
const diffString = diff(expected, actual); | ||
return `Received ${ highlight(actual) } but expected it to equal ` + | ||
`${ printExpected(expected) }.` + (diffString ? '\n\n' + diffString : ''); | ||
} }; | ||
} | ||
return { message, pass }; | ||
}, | ||
@@ -117,5 +118,8 @@ | ||
const message = pass ? | ||
() => `Received ${ highlight(actual) } but expected it to be truthy.` : | ||
() => `Received ${ highlight(actual) } but expected it not to be truthy.`; | ||
matcherHint('.not.toBeTruthy', 'received', '') + '\n\n' + | ||
`Expected value not to be truthy, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeTruthy', 'received', '') + '\n\n' + | ||
`Expected value to be truthy, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -128,5 +132,8 @@ }, | ||
const message = pass ? | ||
() => `Received ${ highlight(actual) } but expected it not to be falsy.` : | ||
() => `Received ${ highlight(actual) } but expected it to be falsy.`; | ||
matcherHint('.not.toBeFalsy', 'received', '') + '\n\n' + | ||
`Expected value not to be falsy, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeFalsy', 'received', '') + '\n\n' + | ||
`Expected value to be falsy, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -139,5 +146,8 @@ }, | ||
const message = pass ? | ||
() => `Received ${ highlight(actual) } but expected it not to be NaN.` : | ||
() => `Received ${ highlight(actual) } but expected it to be NaN.`; | ||
matcherHint('.not.toBeNaN', 'received', '') + '\n\n' + | ||
`Expected value not to be NaN, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeNaN', 'received', '') + '\n\n' + | ||
`Expected value to be NaN, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -150,5 +160,8 @@ }, | ||
const message = pass ? | ||
() => `Received ${ highlight(actual) } but expected it not to be null.` : | ||
() => `Received ${ highlight(actual) } but expected it to be null.`; | ||
matcherHint('.not.toBeNull', 'received', '') + '\n\n' + | ||
`Expected value not to be null, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeNull', 'received', '') + '\n\n' + | ||
`Expected value to be null, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -161,5 +174,8 @@ }, | ||
const message = pass ? | ||
() => `Received ${ highlight(actual) } but expected it not to be defined.` : | ||
() => `Received ${ highlight(actual) } but expected it to be defined.`; | ||
matcherHint('.not.toBeDefined', 'received', '') + '\n\n' + | ||
`Expected value not to be defined, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeDefined', 'received', '') + '\n\n' + | ||
`Expected value to be defined, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -172,4 +188,8 @@ }, | ||
const message = pass ? | ||
() => `Received ${ highlight(actual) } but expected it not to be undefined.` : | ||
() => `Received ${ highlight(actual) } but expected it to be undefined.`; | ||
matcherHint('.not.toBeUndefined', 'received', '') + '\n\n' + | ||
`Expected value not to be undefined, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeUndefined', 'received', '') + '\n\n' + | ||
`Expected value to be undefined, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
@@ -183,4 +203,12 @@ return { message, pass }; | ||
const message = pass ? | ||
`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 >).`; | ||
matcherHint('.not.toBeGreaterThan') + '\n\n' + | ||
`Expected value not to be greater than:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeGreaterThan') + '\n\n' + | ||
`Expected value to be greater than:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -193,4 +221,12 @@ }, | ||
const message = pass ? | ||
`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 >=).`; | ||
matcherHint('.not.toBeGreaterThanOrEqual') + '\n\n' + | ||
`Expected value not to be greater than or equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeGreaterThanOrEqual') + '\n\n' + | ||
`Expected value to be greater than or equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -203,4 +239,12 @@ }, | ||
const message = pass ? | ||
`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 <).`; | ||
matcherHint('.not.toBeLessThan') + '\n\n' + | ||
`Expected value not to be less than:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeLessThan') + '\n\n' + | ||
`Expected value to be less than:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -213,18 +257,35 @@ }, | ||
const message = pass ? | ||
`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 <=).`; | ||
matcherHint('.not.toBeLessThanOrEqual') + '\n\n' + | ||
`Expected value not to be less than or equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }` : | ||
matcherHint('.toBeLessThanOrEqual') + '\n\n' + | ||
`Expected value to be less than or equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
}, | ||
toContain(actual, expected) { | ||
if (!Array.isArray(actual) && typeof actual !== 'string') { | ||
toContain(collection, value) { | ||
const collectionType = getType(collection); | ||
if (!Array.isArray(collection) && typeof collection !== 'string') { | ||
throw new Error( | ||
`.toContain() only works with arrays and strings. ${ typeof actual }: ${ highlight(actual) } was passed.`); | ||
`.toContain() only works with arrays and strings. ${ typeof collection }: ${ printReceived(collection) } was passed.`); | ||
} | ||
const pass = actual.indexOf(expected) != -1; | ||
const pass = collection.indexOf(value) != -1; | ||
const message = pass ? | ||
() => `Received ${ highlight(actual) } but expected it not to contain ${ printExpected(expected) }.` : | ||
() => `Received ${ highlight(actual) } but expected it to contain ${ printExpected(expected) }.`; | ||
() => matcherHint('.not.toContain', collectionType, 'value') + '\n\n' + | ||
`Expected ${ collectionType }:\n` + | ||
` ${ printReceived(collection) }\n` + | ||
`Not to contain value:\n` + | ||
` ${ printExpected(value) }\n` : | ||
() => matcherHint('.toContain', collectionType, 'value') + '\n\n' + | ||
`Expected ${ collectionType }:\n` + | ||
` ${ printReceived(collection) }\n` + | ||
`To contain value:\n` + | ||
` ${ printExpected(value) }`; | ||
@@ -238,4 +299,12 @@ return { message, pass }; | ||
const message = pass ? | ||
() => `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.`; | ||
() => 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) }` : | ||
() => 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) }`; | ||
@@ -245,7 +314,9 @@ return { message, pass }; | ||
toMatch(actual, expected) { | ||
if (typeof actual !== 'string') { | ||
return { | ||
pass: false, | ||
message: `Received ${ highlight(actual) } and expected it to be a string.` }; | ||
toMatch(received, expected) { | ||
if (typeof received !== 'string') { | ||
throw new Error( | ||
matcherHint('[.not].toMatch', 'string', 'expected') + '\n\n' + | ||
`${ RECEIVED_COLOR('string') } value should be a string.\n` + | ||
`Received:\n` + | ||
` ${ getType(received) }: ${ printReceived(received) }`); | ||
@@ -256,5 +327,7 @@ } | ||
if (!(expected instanceof RegExp) && !isString) { | ||
return { | ||
pass: false, | ||
message: `Expected ${ printExpected(expected) } to be a string or regular expression.` }; | ||
throw new Error( | ||
matcherHint('[.not].toMatch', 'string', 'expected') + '\n\n' + | ||
`${ EXPECTED_COLOR('expected') } value should be a string or a regular expression.\n` + | ||
`Received:\n` + | ||
` ${ getType(expected) }: ${ printExpected(expected) }`); | ||
@@ -264,6 +337,14 @@ } | ||
const pass = new RegExp(isString ? escapeStrForRegex(expected) : expected). | ||
test(actual); | ||
test(received); | ||
const message = pass ? | ||
() => `Received ${ highlight(actual) } but expected it not to match ${ printExpected(expected) }.` : | ||
() => `Received ${ highlight(actual) } but expected it to match ${ printExpected(expected) }.`; | ||
() => matcherHint('.not.toMatch') + | ||
`\n\nExpected value not to match:\n` + | ||
` ${ printExpected(expected) }` + | ||
`\nReceived:\n` + | ||
` ${ printReceived(received) }` : | ||
() => matcherHint('.toMatch') + | ||
`\n\nExpected value to match:\n` + | ||
` ${ printExpected(expected) }` + | ||
`\nReceived:\n` + | ||
` ${ printReceived(received) }`; | ||
@@ -270,0 +351,0 @@ return { message, pass }; |
@@ -18,4 +18,5 @@ /** | ||
require('jest-matcher-utils');const ensureNoExpected = _require.ensureNoExpected;const ensureExpectedIsNumber = _require.ensureExpectedIsNumber; | ||
require('jest-matcher-utils');const ensureNoExpected = _require.ensureNoExpected;const ensureExpectedIsNumber = _require.ensureExpectedIsNumber;const pluralize = _require.pluralize; | ||
const spyMatchers = { | ||
@@ -50,6 +51,6 @@ toHaveBeenCalled(actual, expected) { | ||
const message = pass ? | ||
`Expected the ${ type } not to be called ${ expected } times,` + | ||
` but it was called ${ count } times.` : | ||
`Expected the ${ type } to be called ${ expected } times,` + | ||
` but it was called ${ count } times.`; | ||
`Expected the ${ type } not to be called ${ pluralize('time', expected) }` + | ||
`, but it was called ${ pluralize('time', count) }.` : | ||
`Expected the ${ type } to be called ${ pluralize('time', expected) },` + | ||
` but it was called ${ pluralize('time', count) }.`; | ||
@@ -64,3 +65,3 @@ return { message, pass }; | ||
`Expected the mock function not to be called, but it was` + | ||
` called ${ actual.mock.calls.length } times.` : | ||
` called ${ pluralize('time', actual.mock.calls.length) }.` : | ||
`Expected the mock function to be called.`; | ||
@@ -75,3 +76,3 @@ | ||
`Expected the spy not to be called, but it was` + | ||
` called ${ actual.calls.count() } times.` : | ||
` called ${ pluralize('time', actual.calls.count()) }.` : | ||
`Expected the spy to be called.`; | ||
@@ -78,0 +79,0 @@ |
@@ -21,3 +21,3 @@ /** | ||
require('jest-matcher-utils');const getType = _require2.getType;const highlight = _require2.highlight;const printExpected = _require2.printExpected; | ||
require('jest-matcher-utils');const getType = _require2.getType;const printReceived = _require2.printReceived;const printExpected = _require2.printExpected; | ||
@@ -47,3 +47,3 @@ const matchers = { | ||
'"string", "Error type" or "regexp". Got: ' + | ||
`${ highlight(getType(expected)) } ${ highlight(expected) }.`); | ||
`${ printReceived(getType(expected)) } ${ printReceived(expected) }.`); | ||
@@ -80,5 +80,5 @@ } | ||
const getErrorMessage = error => { | ||
return `Received ${ highlight(error.constructor.name + ': ' + error.message) } but expected `; | ||
return `Received ${ printReceived(error.constructor.name + ': ' + error.message) } but expected `; | ||
}; | ||
module.exports = matchers; |
{ | ||
"name": "jest-matchers", | ||
"version": "14.3.0-alpha.d13c163e", | ||
"version": "14.3.1-alpha.410cb91a", | ||
"repository": { | ||
@@ -11,5 +11,5 @@ "type": "git", | ||
"dependencies": { | ||
"jest-diff": "^14.3.0-alpha.d13c163e", | ||
"jest-matcher-utils": "^14.3.0-alpha.d13c163e", | ||
"jest-util": "^14.3.0-alpha.d13c163e" | ||
"jest-diff": "^14.3.1-alpha.410cb91a", | ||
"jest-matcher-utils": "^14.3.1-alpha.410cb91a", | ||
"jest-util": "^14.3.1-alpha.410cb91a" | ||
}, | ||
@@ -16,0 +16,0 @@ "devDependencies": { |
19789
17.64%512
19.91%