jest-matcher-utils
Advanced tools
Comparing version 15.1.0 to 15.2.0-alpha.c681f819
@@ -14,2 +14,3 @@ /** | ||
const chalk = require('chalk'); | ||
const prettyFormat = require('pretty-format'); | ||
@@ -31,2 +32,19 @@ | ||
const NUMBERS = [ | ||
'zero', | ||
'one', | ||
'two', | ||
'three', | ||
'four', | ||
'five', | ||
'six', | ||
'seven', | ||
'eight', | ||
'nine', | ||
'ten', | ||
'eleven', | ||
'twelve', | ||
'thirteen']; | ||
// get the type of a value with handling the edge cases like `typeof []` | ||
@@ -62,66 +80,15 @@ // and `typeof null` | ||
const stringifyValue = (value, visitedSet) => { | ||
if (value instanceof Error) { | ||
const name = value.constructor && value.constructor.name || 'Error'; | ||
return `${ name }: ${ value.message }`; | ||
} else if (typeof value === 'object' && value !== null) { | ||
if ( | ||
value && | ||
value.constructor && | ||
value.constructor.name === 'RegExp') | ||
{ | ||
return value.toString(); | ||
} else { | ||
if (visitedSet.has(value)) { | ||
return '[Circular]'; | ||
} | ||
visitedSet.add(value); | ||
} | ||
} else if (typeof value === 'function') { | ||
return value.toString(); | ||
} else if (typeof value === 'undefined') { | ||
return 'undefined'; | ||
// $FlowFixMe symbols are not supported by flow yet | ||
} else if (typeof value === 'symbol') { | ||
return value.toString(); | ||
} else if (value === Infinity) { | ||
return 'Infinity'; | ||
} else if (value === -Infinity) { | ||
return '-Infinity'; | ||
} else if (Number.isNaN(value)) { | ||
return 'NaN'; | ||
} | ||
return value; | ||
}; | ||
const stringifyDeep = obj => { | ||
const visitedSet = new Set(); | ||
let result = null; | ||
const stringify = object => { | ||
try { | ||
result = JSON.stringify( | ||
obj, | ||
(_, value) => stringifyValue(value, visitedSet)); | ||
return prettyFormat(object, { | ||
min: true }); | ||
} catch (err) {} | ||
return typeof result === 'string' ? result : null; | ||
}; | ||
} catch (e) { | ||
return prettyFormat(object, { | ||
callToJSON: false, | ||
min: true }); | ||
const stringifyShallow = obj => { | ||
let result = null; | ||
try { | ||
result = stringifyValue(obj, new Set()); | ||
} catch (err) {} | ||
return typeof result === 'string' ? result : null; | ||
} | ||
}; | ||
// Convert to JSON removing circular references and | ||
// converting JS values to strings. | ||
const stringify = obj => { | ||
return ( | ||
stringifyDeep(obj) || | ||
stringifyShallow(obj) || | ||
'[' + typeof obj + ']'); | ||
}; | ||
const printReceived = object => RECEIVED_COLOR(stringify(object)); | ||
@@ -148,3 +115,7 @@ const printExpected = value => EXPECTED_COLOR(stringify(value)); | ||
if (typeof expected !== 'undefined') { | ||
throw new Error(`${ matcherName } matcher does not accept any arguments.`); | ||
throw new Error( | ||
matcherHint('[.not]' + matcherName, undefined, '') + '\n\n' + | ||
'Matcher does not accept any arguments.\n' + | ||
printWithType('Got', expected, printExpected)); | ||
} | ||
@@ -157,4 +128,5 @@ }; | ||
throw new Error( | ||
`${ matcherName } actual value should be a number. ` + | ||
`'${ typeof actual }' was passed.`); | ||
matcherHint('[.not]' + matcherName) + '\n\n' + | ||
`Actual value must be a number.\n` + | ||
printWithType('Received', actual, printReceived)); | ||
@@ -168,4 +140,5 @@ } | ||
throw new Error( | ||
`${ matcherName } expected value should be a number. ` + | ||
`'${ typeof expected }' was passed.`); | ||
matcherHint('[.not]' + matcherName) + '\n\n' + | ||
`Expected value must be a number.\n` + | ||
printWithType('Got', expected, printExpected)); | ||
@@ -181,3 +154,4 @@ } | ||
const pluralize = | ||
(word, count) => `${ count } ${ word }${ count === 1 ? '' : 's' }`; | ||
(word, count) => | ||
(NUMBERS[count] || count) + ' ' + word + (count === 1 ? '' : 's'); | ||
@@ -189,5 +163,7 @@ const matcherHint = function ( | ||
{let received = arguments.length <= 1 || arguments[1] === undefined ? 'received' : arguments[1];let expected = arguments.length <= 2 || arguments[2] === undefined ? 'expected' : arguments[2]; | ||
return chalk.dim('expect(') + RECEIVED_COLOR(received) + | ||
chalk.dim(')' + matcherName + '(') + | ||
EXPECTED_COLOR(expected) + chalk.dim(')'); | ||
return ( | ||
chalk.dim('expect(') + RECEIVED_COLOR(received) + | ||
chalk.dim(')' + matcherName + '(') + | ||
EXPECTED_COLOR(expected) + chalk.dim(')')); | ||
}; | ||
@@ -194,0 +170,0 @@ |
{ | ||
"name": "jest-matcher-utils", | ||
"description": "A set of utility functions for jest-matchers and related packages", | ||
"version": "15.1.0", | ||
"version": "15.2.0-alpha.c681f819", | ||
"repository": { | ||
@@ -15,4 +15,5 @@ "type": "git", | ||
"dependencies": { | ||
"chalk": "^1.1.3" | ||
"chalk": "^1.1.3", | ||
"pretty-format": "~4.2.1" | ||
} | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4421
2
137
2
+ Addedpretty-format@~4.2.1
+ Addedpretty-format@4.2.3(transitive)