@vitest/expect
Advanced tools
Comparing version 2.0.0-beta.3 to 2.0.0-beta.4
@@ -154,2 +154,7 @@ import * as _vitest_utils from '@vitest/utils'; | ||
toSatisfy: <E>(matcher: (value: E) => boolean, message?: string) => void; | ||
toHaveResolved: () => void; | ||
toHaveResolvedWith: <E>(value: E) => void; | ||
toHaveResolvedTimes: (times: number) => void; | ||
toHaveLastResolvedWith: <E>(value: E) => void; | ||
toHaveNthResolvedWith: <E>(nthCall: number, value: E) => void; | ||
resolves: PromisifyAssertion<T>; | ||
@@ -156,0 +161,0 @@ rejects: PromisifyAssertion<T>; |
@@ -1207,3 +1207,3 @@ import { getType, getColors, stringify, isObject, assertTypes } from '@vitest/utils'; | ||
}; | ||
const formatCalls = (spy, msg, actualCall) => { | ||
const formatCalls = (spy, msg, showActualCall) => { | ||
if (spy.mock.calls) { | ||
@@ -1218,4 +1218,4 @@ msg += c().gray(` | ||
`); | ||
if (actualCall) | ||
methodCall += diff(actualCall, callArg, { omitAnnotationLines: true }); | ||
if (showActualCall) | ||
methodCall += diff(showActualCall, callArg, { omitAnnotationLines: true }); | ||
else | ||
@@ -1233,3 +1233,3 @@ methodCall += stringify(callArg).split("\n").map((line) => ` ${line}`).join("\n"); | ||
}; | ||
const formatReturns = (spy, msg, actualReturn) => { | ||
const formatReturns = (spy, results, msg, showActualReturn) => { | ||
msg += c().gray(` | ||
@@ -1239,8 +1239,8 @@ | ||
${spy.mock.results.map((callReturn, i) => { | ||
${results.map((callReturn, i) => { | ||
let methodCall = c().bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call return: | ||
`); | ||
if (actualReturn) | ||
methodCall += diff(actualReturn, callReturn.value, { omitAnnotationLines: true }); | ||
if (showActualReturn) | ||
methodCall += diff(showActualReturn, callReturn.value, { omitAnnotationLines: true }); | ||
else | ||
@@ -1414,75 +1414,151 @@ methodCall += stringify(callReturn).split("\n").map((line) => ` ${line}`).join("\n"); | ||
}); | ||
def(["toHaveReturned", "toReturn"], function() { | ||
const spy = getSpy(this); | ||
const spyName = spy.getMockName(); | ||
const calledAndNotThrew = spy.mock.calls.length > 0 && spy.mock.results.some(({ type }) => type !== "throw"); | ||
this.assert( | ||
calledAndNotThrew, | ||
`expected "${spyName}" to be successfully called at least once`, | ||
`expected "${spyName}" to not be successfully called`, | ||
calledAndNotThrew, | ||
!calledAndNotThrew, | ||
false | ||
); | ||
[ | ||
{ | ||
name: "toHaveResolved", | ||
condition: (spy) => spy.mock.settledResults.length > 0 && spy.mock.settledResults.some(({ type }) => type === "fulfilled"), | ||
action: "resolved" | ||
}, | ||
{ | ||
name: ["toHaveReturned", "toReturn"], | ||
condition: (spy) => spy.mock.calls.length > 0 && spy.mock.results.some(({ type }) => type !== "throw"), | ||
action: "called" | ||
} | ||
].forEach(({ name, condition, action }) => { | ||
def(name, function() { | ||
const spy = getSpy(this); | ||
const spyName = spy.getMockName(); | ||
const pass = condition(spy); | ||
this.assert( | ||
pass, | ||
`expected "${spyName}" to be successfully ${action} at least once`, | ||
`expected "${spyName}" to not be successfully ${action}`, | ||
pass, | ||
!pass, | ||
false | ||
); | ||
}); | ||
}); | ||
def(["toHaveReturnedTimes", "toReturnTimes"], function(times) { | ||
const spy = getSpy(this); | ||
const spyName = spy.getMockName(); | ||
const successfulReturns = spy.mock.results.reduce((success, { type }) => type === "throw" ? success : ++success, 0); | ||
this.assert( | ||
successfulReturns === times, | ||
`expected "${spyName}" to be successfully called ${times} times`, | ||
`expected "${spyName}" to not be successfully called ${times} times`, | ||
`expected number of returns: ${times}`, | ||
`received number of returns: ${successfulReturns}`, | ||
false | ||
); | ||
}); | ||
def(["toHaveReturnedWith", "toReturnWith"], function(value) { | ||
const spy = getSpy(this); | ||
const spyName = spy.getMockName(); | ||
const pass = spy.mock.results.some(({ type, value: result }) => type === "return" && equals(value, result)); | ||
const isNot = utils.flag(this, "negate"); | ||
const msg = utils.getMessage( | ||
this, | ||
[ | ||
[ | ||
{ | ||
name: "toHaveResolvedTimes", | ||
condition: (spy, times) => spy.mock.settledResults.reduce((s, { type }) => type === "fulfilled" ? ++s : s, 0) === times, | ||
action: "resolved" | ||
}, | ||
{ | ||
name: ["toHaveReturnedTimes", "toReturnTimes"], | ||
condition: (spy, times) => spy.mock.results.reduce((s, { type }) => type === "throw" ? s : ++s, 0) === times, | ||
action: "called" | ||
} | ||
].forEach(({ name, condition, action }) => { | ||
def(name, function(times) { | ||
const spy = getSpy(this); | ||
const spyName = spy.getMockName(); | ||
const pass = condition(spy, times); | ||
this.assert( | ||
pass, | ||
`expected "${spyName}" to return with: #{exp} at least once`, | ||
`expected "${spyName}" to not return with: #{exp}`, | ||
value | ||
] | ||
); | ||
if (pass && isNot || !pass && !isNot) | ||
throw new AssertionError(formatReturns(spy, msg, value)); | ||
`expected "${spyName}" to be successfully ${action} ${times} times`, | ||
`expected "${spyName}" to not be successfully ${action} ${times} times`, | ||
`expected resolved times: ${times}`, | ||
`received resolved times: ${pass}`, | ||
false | ||
); | ||
}); | ||
}); | ||
def(["toHaveLastReturnedWith", "lastReturnedWith"], function(value) { | ||
const spy = getSpy(this); | ||
const spyName = spy.getMockName(); | ||
const { value: lastResult } = spy.mock.results[spy.mock.results.length - 1]; | ||
const pass = equals(lastResult, value); | ||
this.assert( | ||
pass, | ||
`expected last "${spyName}" call to return #{exp}`, | ||
`expected last "${spyName}" call to not return #{exp}`, | ||
value, | ||
lastResult | ||
); | ||
[ | ||
{ | ||
name: "toHaveResolvedWith", | ||
condition: (spy, value) => spy.mock.settledResults.some(({ type, value: result }) => type === "fulfilled" && equals(value, result)), | ||
action: "resolve" | ||
}, | ||
{ | ||
name: ["toHaveReturnedWith", "toReturnWith"], | ||
condition: (spy, value) => spy.mock.results.some(({ type, value: result }) => type === "return" && equals(value, result)), | ||
action: "return" | ||
} | ||
].forEach(({ name, condition, action }) => { | ||
def(name, function(value) { | ||
const spy = getSpy(this); | ||
const pass = condition(spy, value); | ||
const isNot = utils.flag(this, "negate"); | ||
if (pass && isNot || !pass && !isNot) { | ||
const spyName = spy.getMockName(); | ||
const msg = utils.getMessage( | ||
this, | ||
[ | ||
pass, | ||
`expected "${spyName}" to ${action} with: #{exp} at least once`, | ||
`expected "${spyName}" to not ${action} with: #{exp}`, | ||
value | ||
] | ||
); | ||
const results = action === "return" ? spy.mock.results : spy.mock.settledResults; | ||
throw new AssertionError(formatReturns(spy, results, msg, value)); | ||
} | ||
}); | ||
}); | ||
def(["toHaveNthReturnedWith", "nthReturnedWith"], function(nthCall, value) { | ||
const spy = getSpy(this); | ||
const spyName = spy.getMockName(); | ||
const isNot = utils.flag(this, "negate"); | ||
const { type: callType, value: callResult } = spy.mock.results[nthCall - 1]; | ||
const ordinalCall = `${ordinalOf(nthCall)} call`; | ||
if (!isNot && callType === "throw") | ||
chai.assert.fail(`expected ${ordinalCall} to return #{exp}, but instead it threw an error`); | ||
const nthCallReturn = equals(callResult, value); | ||
this.assert( | ||
nthCallReturn, | ||
`expected ${ordinalCall} "${spyName}" call to return #{exp}`, | ||
`expected ${ordinalCall} "${spyName}" call to not return #{exp}`, | ||
value, | ||
callResult | ||
); | ||
[ | ||
{ | ||
name: "toHaveLastResolvedWith", | ||
condition: (spy, value) => { | ||
const result = spy.mock.settledResults[spy.mock.settledResults.length - 1]; | ||
return result && result.type === "fulfilled" && equals(result.value, value); | ||
}, | ||
action: "resolve" | ||
}, | ||
{ | ||
name: ["toHaveLastReturnedWith", "lastReturnedWith"], | ||
condition: (spy, value) => { | ||
const result = spy.mock.results[spy.mock.results.length - 1]; | ||
return result && result.type === "return" && equals(result.value, value); | ||
}, | ||
action: "return" | ||
} | ||
].forEach(({ name, condition, action }) => { | ||
def(name, function(value) { | ||
const spy = getSpy(this); | ||
const results = action === "return" ? spy.mock.results : spy.mock.settledResults; | ||
const result = results[results.length - 1]; | ||
const spyName = spy.getMockName(); | ||
this.assert( | ||
condition(spy, value), | ||
`expected last "${spyName}" call to ${action} #{exp}`, | ||
`expected last "${spyName}" call to not ${action} #{exp}`, | ||
value, | ||
result == null ? void 0 : result.value | ||
); | ||
}); | ||
}); | ||
[ | ||
{ | ||
name: "toHaveNthResolvedWith", | ||
condition: (spy, index, value) => { | ||
const result = spy.mock.settledResults[index - 1]; | ||
return result && result.type === "fulfilled" && equals(result.value, value); | ||
}, | ||
action: "resolve" | ||
}, | ||
{ | ||
name: ["toHaveNthReturnedWith", "nthReturnedWith"], | ||
condition: (spy, index, value) => { | ||
const result = spy.mock.results[index - 1]; | ||
return result && result.type === "return" && equals(result.value, value); | ||
}, | ||
action: "return" | ||
} | ||
].forEach(({ name, condition, action }) => { | ||
def(name, function(nthCall, value) { | ||
const spy = getSpy(this); | ||
const spyName = spy.getMockName(); | ||
const results = action === "return" ? spy.mock.results : spy.mock.settledResults; | ||
const result = results[nthCall - 1]; | ||
const ordinalCall = `${ordinalOf(nthCall)} call`; | ||
this.assert( | ||
condition(spy, nthCall, value), | ||
`expected ${ordinalCall} "${spyName}" call to ${action} #{exp}`, | ||
`expected ${ordinalCall} "${spyName}" call to not ${action} #{exp}`, | ||
value, | ||
result == null ? void 0 : result.value | ||
); | ||
}); | ||
}); | ||
def("toSatisfy", function(matcher, message) { | ||
@@ -1489,0 +1565,0 @@ return this.be.satisfy(matcher, message); |
{ | ||
"name": "@vitest/expect", | ||
"type": "module", | ||
"version": "2.0.0-beta.3", | ||
"version": "2.0.0-beta.4", | ||
"description": "Jest's expect matchers as a Chai plugin", | ||
@@ -34,4 +34,4 @@ "license": "MIT", | ||
"chai": "^5.1.1", | ||
"@vitest/spy": "2.0.0-beta.3", | ||
"@vitest/utils": "2.0.0-beta.3" | ||
"@vitest/spy": "2.0.0-beta.4", | ||
"@vitest/utils": "2.0.0-beta.4" | ||
}, | ||
@@ -42,3 +42,3 @@ "devDependencies": { | ||
"rollup-plugin-copy": "^3.5.0", | ||
"@vitest/runner": "2.0.0-beta.3" | ||
"@vitest/runner": "2.0.0-beta.4" | ||
}, | ||
@@ -45,0 +45,0 @@ "scripts": { |
146324
1984
+ Added@vitest/spy@2.0.0-beta.4(transitive)
+ Added@vitest/utils@2.0.0-beta.4(transitive)
+ Addedtinyspy@3.0.0(transitive)
- Removed@vitest/spy@2.0.0-beta.3(transitive)
- Removed@vitest/utils@2.0.0-beta.3(transitive)
- Removedtinyspy@2.2.1(transitive)
Updated@vitest/spy@2.0.0-beta.4
Updated@vitest/utils@2.0.0-beta.4