jest-matchers
Advanced tools
Comparing version 15.0.0 to 15.0.1
@@ -27,18 +27,9 @@ /** | ||
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; | ||
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 printWithType = _require2.printWithType; | ||
const IteratorSymbol = Symbol.iterator; | ||
const equals = global.jasmine.matchersUtil.equals; | ||
const printWithType = (name, received, print) => { | ||
const type = getType(received); | ||
return ( | ||
name + ':' + ( | ||
type !== 'null' && type !== 'undefined' ? | ||
'\n ' + type + ': ' : | ||
' ') + | ||
print(received)); | ||
}; | ||
const hasIterator = object => !!(object != null && object[Symbol.iterator]); | ||
const hasIterator = object => !!(object != null && object[IteratorSymbol]); | ||
const iterableEquality = (a, b) => { | ||
@@ -58,3 +49,3 @@ if ( | ||
} | ||
const bIterator = b[Symbol.iterator](); | ||
const bIterator = b[IteratorSymbol](); | ||
@@ -61,0 +52,0 @@ for (const aValue of a) { |
@@ -16,3 +16,2 @@ /** | ||
require('jest-util');const escapeStrForRegex = _require.escapeStrForRegex;var _require2 = | ||
@@ -22,38 +21,63 @@ | ||
require('jest-util');const escapeStrForRegex = _require.escapeStrForRegex;const formatStackTrace = _require.formatStackTrace;const separateMessageFromStack = _require.separateMessageFromStack;var _require2 = | ||
require('jest-matcher-utils');const getType = _require2.getType;const matcherHint = _require2.matcherHint;const printReceived = _require2.printReceived;const printExpected = _require2.printExpected; | ||
const matchers = { | ||
toThrowError(actual, expected) { | ||
const value = expected; | ||
let error; | ||
try { | ||
actual(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
if (typeof expected === 'string') { | ||
expected = new RegExp(escapeStrForRegex(expected)); | ||
} | ||
if (typeof expected === 'function') { | ||
return toThrowMatchingError(error, expected); | ||
} else if (expected instanceof RegExp) { | ||
return toThrowMatchingStringOrRegexp(error, expected, value); | ||
} else { | ||
return { | ||
pass: false, | ||
message: | ||
'Unexpected argument passed. Expected to get\n' + | ||
` ${ printExpected('"string"') }, ${ printExpected('"Error type"') } or ${ printExpected('"regexp"') }.\n` + | ||
`Got: \n` + | ||
` ${ printReceived(getType(expected)) }: ${ printReceived(expected) }.` }; | ||
} | ||
} }; | ||
require('jest-matcher-utils');const RECEIVED_COLOR = _require2.RECEIVED_COLOR;const getType = _require2.getType;const matcherHint = _require2.matcherHint;const printExpected = _require2.printExpected;const printWithType = _require2.printWithType; | ||
const equals = global.jasmine.matchersUtil.equals; | ||
const createMatcher = name => | ||
(actual, expected) => { | ||
const value = expected; | ||
let error; | ||
try { | ||
actual(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
if (typeof expected === 'string') { | ||
expected = new RegExp(escapeStrForRegex(expected)); | ||
} | ||
if (typeof expected === 'function') { | ||
return toThrowMatchingError(name, error, expected); | ||
} else if (expected instanceof RegExp) { | ||
return toThrowMatchingStringOrRegexp(name, error, expected, value); | ||
} else if (expected && typeof expected === 'object') { | ||
return toThrowMatchingErrorInstance(name, error, expected); | ||
} else if (expected === undefined) { | ||
const pass = error !== undefined; | ||
return { | ||
pass, | ||
message: pass ? | ||
() => matcherHint('.not.' + name, 'function', '') + '\n\n' + | ||
'Expected the function not to throw an error.\n' + | ||
printActualErrorMessage(error) : | ||
() => matcherHint('.' + name, 'function', getType(value)) + '\n\n' + | ||
'Expected the function to throw an error.\n' + | ||
printActualErrorMessage(error) }; | ||
} else { | ||
throw new Error( | ||
matcherHint('.not.' + name, 'function', getType(value)) + '\n\n' + | ||
'Unexpected argument passed.\nExpected: ' + | ||
`${ printExpected('string') }, ${ printExpected('Error (type)') } or ${ printExpected('regexp') }.\n` + | ||
printWithType('Got', expected, printExpected)); | ||
} | ||
}; | ||
const matchers = { | ||
toThrow: createMatcher('toThrow'), | ||
toThrowError: createMatcher('toThrowError') }; | ||
const toThrowMatchingStringOrRegexp = ( | ||
name, | ||
error, | ||
@@ -63,3 +87,3 @@ pattern, | ||
{ | ||
if (error && !(error instanceof Error)) { | ||
if (error && !error.message && !error.name) { | ||
error = new Error(error); | ||
@@ -69,13 +93,11 @@ } | ||
const pass = !!(error && error.message.match(pattern)); | ||
const message = pass ? | ||
() => matcherHint('.not.toThrowError', 'function', getType(value)) + '\n\n' + | ||
`Expeced the function not to throw an error matching:\n` + | ||
() => matcherHint('.not.' + name, 'function', getType(value)) + '\n\n' + | ||
`Expected the function not to throw an error matching:\n` + | ||
` ${ printExpected(value) }\n` + | ||
`But it threw:\n` + | ||
` ${ printReceived(error) }` : | ||
() => matcherHint('.toThrowError', 'function', getType(value)) + '\n\n' + | ||
printActualErrorMessage(error) : | ||
() => matcherHint('.' + name, 'function', getType(value)) + '\n\n' + | ||
`Expected the function to throw an error matching:\n` + | ||
` ${ printExpected(value) }\n` + | ||
getActualErrorMessage(error); | ||
printActualErrorMessage(error); | ||
@@ -85,3 +107,27 @@ return { pass, message }; | ||
const toThrowMatchingErrorInstance = ( | ||
name, | ||
error, | ||
expectedError) => | ||
{ | ||
if (error && !error.message && !error.name) { | ||
error = new Error(error); | ||
} | ||
const pass = equals(error, expectedError); | ||
const message = pass ? | ||
() => matcherHint('.not.' + name, 'function', 'error') + '\n\n' + | ||
`Expected the function not to throw an error matching:\n` + | ||
` ${ printExpected(expectedError) }\n` + | ||
printActualErrorMessage(error) : | ||
() => matcherHint('.' + name, 'function', 'error') + '\n\n' + | ||
`Expected the function to throw an error matching:\n` + | ||
` ${ printExpected(expectedError) }\n` + | ||
printActualErrorMessage(error); | ||
return { pass, message }; | ||
}; | ||
const toThrowMatchingError = ( | ||
name, | ||
error, | ||
@@ -91,13 +137,11 @@ ErrorClass) => | ||
const pass = !!(error && error instanceof ErrorClass); | ||
const message = pass ? | ||
() => matcherHint('.not.toThrowError', 'function', 'type') + '\n\n' + | ||
`Expeced the function not to throw an error of type:\n` + | ||
() => matcherHint('.not.' + name, 'function', 'type') + '\n\n' + | ||
`Expected the function not to throw an error of type:\n` + | ||
` ${ printExpected(ErrorClass.name) }\n` + | ||
`But it threw:\n` + | ||
` ${ printReceived(error) }` : | ||
() => matcherHint('.toThrowError', 'function', 'type') + '\n\n' + | ||
printActualErrorMessage(error) : | ||
() => matcherHint('.' + name, 'function', 'type') + '\n\n' + | ||
`Expected the function to throw an error of type:\n` + | ||
` ${ printExpected(ErrorClass.name) }\n` + | ||
getActualErrorMessage(error); | ||
printActualErrorMessage(error); | ||
@@ -107,9 +151,16 @@ return { pass, message }; | ||
const getActualErrorMessage = error => { | ||
return error ? | ||
`Instead, it threw:\n` + | ||
` ${ printReceived(error) }` : | ||
`But it didn't throw anything.`; | ||
const printActualErrorMessage = error => { | ||
if (error) {var _separateMessageFromS = | ||
separateMessageFromStack(error.stack);const message = _separateMessageFromS.message;const stack = _separateMessageFromS.stack; | ||
return ( | ||
`Instead, it threw:\n` + | ||
RECEIVED_COLOR( | ||
' ' + message + formatStackTrace(stack, { rootDir: process.cwd() }))); | ||
} | ||
return `But it didn't throw anything.`; | ||
}; | ||
module.exports = matchers; |
{ | ||
"name": "jest-matchers", | ||
"version": "15.0.0", | ||
"version": "15.0.1", | ||
"repository": { | ||
@@ -11,5 +11,5 @@ "type": "git", | ||
"dependencies": { | ||
"jest-diff": "^15.0.0", | ||
"jest-matcher-utils": "^15.0.0", | ||
"jest-util": "^15.0.0" | ||
"jest-diff": "^15.0.1", | ||
"jest-matcher-utils": "^15.0.1", | ||
"jest-util": "^15.0.1" | ||
}, | ||
@@ -16,0 +16,0 @@ "devDependencies": { |
22403
581
Updatedjest-diff@^15.0.1
Updatedjest-matcher-utils@^15.0.1
Updatedjest-util@^15.0.1