jest-matchers
Advanced tools
Comparing version 17.0.1 to 17.0.2
@@ -100,107 +100,24 @@ /** | ||
toEqual(received, expected) { | ||
const pass = equals(received, expected, [iterableEquality]); | ||
toBeCloseTo( | ||
actual, | ||
expected) | ||
{let precision = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2; | ||
ensureNumbers(actual, expected, '.toBeCloseTo'); | ||
const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2; | ||
const message = pass ? | ||
() => matcherHint('.not.toEqual') + '\n\n' + | ||
`Expected value to not equal:\n` + | ||
() => 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(received) }` : | ||
() => { | ||
const diffString = diff(expected, received, { | ||
expand: this.expand }); | ||
return matcherHint('.toEqual') + '\n\n' + | ||
`Expected value to equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` + ( | ||
diffString ? `\n\nDifference:\n\n${ diffString }` : ''); | ||
}; | ||
return { message, pass }; | ||
}, | ||
toBeInstanceOf(received, constructor) { | ||
const constType = getType(constructor); | ||
if (constType !== 'function') { | ||
throw new Error( | ||
matcherHint('[.not].toBeInstanceOf', 'value', 'constructor') + `\n\n` + | ||
`Expected constructor to be a function. Instead got:\n` + | ||
` ${ printExpected(constType) }`); | ||
} | ||
const pass = received instanceof constructor; | ||
const message = pass ? | ||
() => matcherHint('.not.toBeInstanceOf', 'value', 'constructor') + '\n\n' + | ||
`Expected value not to be an instance of:\n` + | ||
` ${ printExpected(constructor.name || constructor) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` : | ||
() => matcherHint('.toBeInstanceOf', 'value', 'constructor') + '\n\n' + | ||
`Expected value to be an instance of:\n` + | ||
` ${ printExpected(constructor.name || constructor) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + | ||
`Constructor:\n` + | ||
` ${ printReceived(received.constructor && received.constructor.name) }`; | ||
return { message, pass }; | ||
}, | ||
toBeTruthy(actual, expected) { | ||
ensureNoExpected(expected, '.toBeTruthy'); | ||
const pass = !!actual; | ||
const message = pass ? | ||
() => matcherHint('.not.toBeTruthy', 'received', '') + '\n\n' + | ||
`Expected value not to be truthy, instead received\n` + | ||
`Received: \n` + | ||
` ${ printReceived(actual) }` : | ||
() => matcherHint('.toBeTruthy', 'received', '') + '\n\n' + | ||
`Expected value to be truthy, instead received\n` + | ||
() => 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) }`; | ||
return { message, pass }; | ||
}, | ||
toBeFalsy(actual, expected) { | ||
ensureNoExpected(expected, '.toBeFalsy'); | ||
const pass = !actual; | ||
const message = pass ? | ||
() => 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 }; | ||
}, | ||
toBeNaN(actual, expected) { | ||
ensureNoExpected(expected, '.toBeNaN'); | ||
const pass = Number.isNaN(actual); | ||
const message = pass ? | ||
() => 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 }; | ||
}, | ||
toBeNull(actual, expected) { | ||
ensureNoExpected(expected, '.toBeNull'); | ||
const pass = actual === null; | ||
const message = pass ? | ||
() => 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 }; | ||
}, | ||
toBeDefined(actual, expected) { | ||
@@ -219,13 +136,12 @@ ensureNoExpected(expected, '.toBeDefined'); | ||
toBeUndefined(actual, expected) { | ||
ensureNoExpected(expected, '.toBeUndefined'); | ||
const pass = actual === void 0; | ||
toBeFalsy(actual, expected) { | ||
ensureNoExpected(expected, '.toBeFalsy'); | ||
const pass = !actual; | ||
const message = pass ? | ||
() => matcherHint('.not.toBeUndefined', 'received', '') + '\n\n' + | ||
`Expected value not to be undefined, instead received\n` + | ||
() => matcherHint('.not.toBeFalsy', 'received', '') + '\n\n' + | ||
`Expected value not to be falsy, instead received\n` + | ||
` ${ printReceived(actual) }` : | ||
() => matcherHint('.toBeUndefined', 'received', '') + '\n\n' + | ||
`Expected value to be undefined, instead received\n` + | ||
() => matcherHint('.toBeFalsy', 'received', '') + '\n\n' + | ||
`Expected value to be falsy, instead received\n` + | ||
` ${ printReceived(actual) }`; | ||
return { message, pass }; | ||
@@ -268,2 +184,31 @@ }, | ||
toBeInstanceOf(received, constructor) { | ||
const constType = getType(constructor); | ||
if (constType !== 'function') { | ||
throw new Error( | ||
matcherHint('[.not].toBeInstanceOf', 'value', 'constructor') + `\n\n` + | ||
`Expected constructor to be a function. Instead got:\n` + | ||
` ${ printExpected(constType) }`); | ||
} | ||
const pass = received instanceof constructor; | ||
const message = pass ? | ||
() => matcherHint('.not.toBeInstanceOf', 'value', 'constructor') + '\n\n' + | ||
`Expected value not to be an instance of:\n` + | ||
` ${ printExpected(constructor.name || constructor) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` : | ||
() => matcherHint('.toBeInstanceOf', 'value', 'constructor') + '\n\n' + | ||
`Expected value to be an instance of:\n` + | ||
` ${ printExpected(constructor.name || constructor) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }\n` + | ||
`Constructor:\n` + | ||
` ${ printReceived(received.constructor && received.constructor.name) }`; | ||
return { message, pass }; | ||
}, | ||
toBeLessThan(actual, expected) { | ||
@@ -303,2 +248,55 @@ ensureNumbers(actual, expected, '.toBeLessThan'); | ||
toBeNaN(actual, expected) { | ||
ensureNoExpected(expected, '.toBeNaN'); | ||
const pass = Number.isNaN(actual); | ||
const message = pass ? | ||
() => 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 }; | ||
}, | ||
toBeNull(actual, expected) { | ||
ensureNoExpected(expected, '.toBeNull'); | ||
const pass = actual === null; | ||
const message = pass ? | ||
() => 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 }; | ||
}, | ||
toBeTruthy(actual, expected) { | ||
ensureNoExpected(expected, '.toBeTruthy'); | ||
const pass = !!actual; | ||
const message = pass ? | ||
() => 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 }; | ||
}, | ||
toBeUndefined(actual, expected) { | ||
ensureNoExpected(expected, '.toBeUndefined'); | ||
const pass = actual === void 0; | ||
const message = pass ? | ||
() => 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) }`; | ||
return { message, pass }; | ||
}, | ||
toContain(collection, value) { | ||
@@ -355,21 +353,23 @@ const collectionType = getType(collection); | ||
toBeCloseTo( | ||
actual, | ||
expected) | ||
toEqual(received, expected) { | ||
const pass = equals(received, expected, [iterableEquality]); | ||
{let precision = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2; | ||
ensureNumbers(actual, expected, '.toBeCloseTo'); | ||
const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2; | ||
const message = pass ? | ||
() => matcherHint('.not.toBeCloseTo', 'received', 'expected, precision') + '\n\n' + | ||
`Expected value not to be close to (with ${ printExpected(precision) }-digit precision):\n` + | ||
() => matcherHint('.not.toEqual') + '\n\n' + | ||
`Expected value to not equal:\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) }`; | ||
`Received:\n` + | ||
` ${ printReceived(received) }` : | ||
() => { | ||
const diffString = diff(expected, received, { | ||
expand: this.expand }); | ||
return matcherHint('.toEqual') + '\n\n' + | ||
`Expected value to equal:\n` + | ||
` ${ printExpected(expected) }\n` + | ||
`Received:\n` + | ||
` ${ printReceived(received) }` + ( | ||
diffString ? `\n\nDifference:\n\n${ diffString }` : ''); | ||
}; | ||
return { message, pass }; | ||
@@ -376,0 +376,0 @@ }, |
@@ -30,4 +30,4 @@ /** | ||
const RECEIVED_NAME = { | ||
spy: 'spy', | ||
'mock function': 'jest.fn()' }; | ||
'mock function': 'jest.fn()', | ||
spy: 'spy' }; | ||
@@ -114,10 +114,6 @@ | ||
const spyMatchers = { | ||
lastCalledWith: createLastCalledWithMatcher('.lastCalledWith'), | ||
toBeCalled: createToBeCalledMatcher('.toBeCalled'), | ||
toBeCalledWith: createToBeCalledWithMatcher('.toBeCalledWith'), | ||
toHaveBeenCalled: createToBeCalledMatcher('.toHaveBeenCalled'), | ||
toBeCalledWith: createToBeCalledWithMatcher('.toBeCalledWith'), | ||
toHaveBeenCalledWith: createToBeCalledWithMatcher('.toHaveBeenCalledWith'), | ||
toHaveBeenLastCalledWith: | ||
createLastCalledWithMatcher('.toHaveBeenLastCalledWith'), | ||
lastCalledWith: createLastCalledWithMatcher('.lastCalledWith'), | ||
toHaveBeenCalledTimes(received, expected) { | ||
@@ -152,3 +148,6 @@ const matcherName = '.toHaveBeenCalledTimes'; | ||
return { message, pass }; | ||
} }; | ||
}, | ||
toHaveBeenCalledWith: createToBeCalledWithMatcher('.toHaveBeenCalledWith'), | ||
toHaveBeenLastCalledWith: | ||
createLastCalledWithMatcher('.toHaveBeenLastCalledWith') }; | ||
@@ -155,0 +154,0 @@ |
@@ -55,3 +55,2 @@ /** | ||
return { | ||
pass, | ||
message: pass ? | ||
@@ -63,3 +62,4 @@ () => matcherHint('.not' + matcherName, 'function', '') + '\n\n' + | ||
'Expected the function to throw an error.\n' + | ||
printActualErrorMessage(error) }; | ||
printActualErrorMessage(error), | ||
pass }; | ||
@@ -102,3 +102,3 @@ } else { | ||
return { pass, message }; | ||
return { message, pass }; | ||
}; | ||
@@ -126,3 +126,3 @@ | ||
return { pass, message }; | ||
return { message, pass }; | ||
}; | ||
@@ -146,3 +146,3 @@ | ||
return { pass, message }; | ||
return { message, pass }; | ||
}; | ||
@@ -149,0 +149,0 @@ |
{ | ||
"name": "jest-matchers", | ||
"version": "17.0.1", | ||
"version": "17.0.2", | ||
"repository": { | ||
@@ -11,5 +11,5 @@ "type": "git", | ||
"dependencies": { | ||
"jest-diff": "^17.0.1", | ||
"jest-diff": "^17.0.2", | ||
"jest-matcher-utils": "^17.0.1", | ||
"jest-util": "^17.0.0" | ||
"jest-util": "^17.0.2" | ||
}, | ||
@@ -16,0 +16,0 @@ "scripts": { |
28843
Updatedjest-diff@^17.0.2
Updatedjest-util@^17.0.2