@vitest/expect 2.0.0-beta.3 → 2.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -0
- package/dist/index.js +151 -75
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
@@ -151,6 +151,11 @@ interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAss
|
|
151
151
|
toBeTypeOf: (expected: 'bigint' | 'boolean' | 'function' | 'number' | 'object' | 'string' | 'symbol' | 'undefined') => void;
|
152
152
|
toHaveBeenCalledOnce: () => void;
|
153
153
|
toSatisfy: <E>(matcher: (value: E) => boolean, message?: string) => void;
|
154
|
+
toHaveResolved: () => void;
|
155
|
+
toHaveResolvedWith: <E>(value: E) => void;
|
156
|
+
toHaveResolvedTimes: (times: number) => void;
|
157
|
+
toHaveLastResolvedWith: <E>(value: E) => void;
|
158
|
+
toHaveNthResolvedWith: <E>(nthCall: number, value: E) => void;
|
154
159
|
resolves: PromisifyAssertion<T>;
|
155
160
|
rejects: PromisifyAssertion<T>;
|
156
161
|
}
|
package/dist/index.js
CHANGED
@@ -1204,7 +1204,7 @@ const JestChaiExpect = (chai, utils) => {
|
|
1204
1204
|
return `${i}rd`;
|
1205
1205
|
return `${i}th`;
|
1206
1206
|
};
|
1207
|
-
const formatCalls = (spy, msg,
|
1207
|
+
const formatCalls = (spy, msg, showActualCall) => {
|
1208
1208
|
if (spy.mock.calls) {
|
1209
1209
|
msg += c().gray(`
|
1210
1210
|
|
@@ -1214,8 +1214,8 @@ ${spy.mock.calls.map((callArg, i) => {
|
|
1214
1214
|
let methodCall = c().bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call:
|
1215
1215
|
|
1216
1216
|
`);
|
1217
|
-
if (
|
1218
|
-
methodCall += diff(
|
1217
|
+
if (showActualCall)
|
1218
|
+
methodCall += diff(showActualCall, callArg, { omitAnnotationLines: true });
|
1219
1219
|
else
|
1220
1220
|
methodCall += stringify(callArg).split("\n").map((line) => ` ${line}`).join("\n");
|
1221
1221
|
methodCall += "\n";
|
@@ -1228,17 +1228,17 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
|
|
1228
1228
|
`);
|
1229
1229
|
return msg;
|
1230
1230
|
};
|
1231
|
-
const formatReturns = (spy, msg,
|
1231
|
+
const formatReturns = (spy, results, msg, showActualReturn) => {
|
1232
1232
|
msg += c().gray(`
|
1233
1233
|
|
1234
1234
|
Received:
|
1235
1235
|
|
1236
|
-
${
|
1236
|
+
${results.map((callReturn, i) => {
|
1237
1237
|
let methodCall = c().bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call return:
|
1238
1238
|
|
1239
1239
|
`);
|
1240
|
-
if (
|
1241
|
-
methodCall += diff(
|
1240
|
+
if (showActualReturn)
|
1241
|
+
methodCall += diff(showActualReturn, callReturn.value, { omitAnnotationLines: true });
|
1242
1242
|
else
|
1243
1243
|
methodCall += stringify(callReturn).split("\n").map((line) => ` ${line}`).join("\n");
|
1244
1244
|
methodCall += "\n";
|
@@ -1407,78 +1407,154 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
|
|
1407
1407
|
}
|
1408
1408
|
throw new Error(`"toThrow" expects string, RegExp, function, Error instance or asymmetric matcher, got "${typeof expected}"`);
|
1409
1409
|
});
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1434
|
-
);
|
1410
|
+
[
|
1411
|
+
{
|
1412
|
+
name: "toHaveResolved",
|
1413
|
+
condition: (spy) => spy.mock.settledResults.length > 0 && spy.mock.settledResults.some(({ type }) => type === "fulfilled"),
|
1414
|
+
action: "resolved"
|
1415
|
+
},
|
1416
|
+
{
|
1417
|
+
name: ["toHaveReturned", "toReturn"],
|
1418
|
+
condition: (spy) => spy.mock.calls.length > 0 && spy.mock.results.some(({ type }) => type !== "throw"),
|
1419
|
+
action: "called"
|
1420
|
+
}
|
1421
|
+
].forEach(({ name, condition, action }) => {
|
1422
|
+
def(name, function() {
|
1423
|
+
const spy = getSpy(this);
|
1424
|
+
const spyName = spy.getMockName();
|
1425
|
+
const pass = condition(spy);
|
1426
|
+
this.assert(
|
1427
|
+
pass,
|
1428
|
+
`expected "${spyName}" to be successfully ${action} at least once`,
|
1429
|
+
`expected "${spyName}" to not be successfully ${action}`,
|
1430
|
+
pass,
|
1431
|
+
!pass,
|
1432
|
+
false
|
1433
|
+
);
|
1434
|
+
});
|
1435
1435
|
});
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
[
|
1436
|
+
[
|
1437
|
+
{
|
1438
|
+
name: "toHaveResolvedTimes",
|
1439
|
+
condition: (spy, times) => spy.mock.settledResults.reduce((s, { type }) => type === "fulfilled" ? ++s : s, 0) === times,
|
1440
|
+
action: "resolved"
|
1441
|
+
},
|
1442
|
+
{
|
1443
|
+
name: ["toHaveReturnedTimes", "toReturnTimes"],
|
1444
|
+
condition: (spy, times) => spy.mock.results.reduce((s, { type }) => type === "throw" ? s : ++s, 0) === times,
|
1445
|
+
action: "called"
|
1446
|
+
}
|
1447
|
+
].forEach(({ name, condition, action }) => {
|
1448
|
+
def(name, function(times) {
|
1449
|
+
const spy = getSpy(this);
|
1450
|
+
const spyName = spy.getMockName();
|
1451
|
+
const pass = condition(spy, times);
|
1452
|
+
this.assert(
|
1444
1453
|
pass,
|
1445
|
-
`expected "${spyName}" to
|
1446
|
-
`expected "${spyName}" to not
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1454
|
+
`expected "${spyName}" to be successfully ${action} ${times} times`,
|
1455
|
+
`expected "${spyName}" to not be successfully ${action} ${times} times`,
|
1456
|
+
`expected resolved times: ${times}`,
|
1457
|
+
`received resolved times: ${pass}`,
|
1458
|
+
false
|
1459
|
+
);
|
1460
|
+
});
|
1452
1461
|
});
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1462
|
+
[
|
1463
|
+
{
|
1464
|
+
name: "toHaveResolvedWith",
|
1465
|
+
condition: (spy, value) => spy.mock.settledResults.some(({ type, value: result }) => type === "fulfilled" && equals(value, result)),
|
1466
|
+
action: "resolve"
|
1467
|
+
},
|
1468
|
+
{
|
1469
|
+
name: ["toHaveReturnedWith", "toReturnWith"],
|
1470
|
+
condition: (spy, value) => spy.mock.results.some(({ type, value: result }) => type === "return" && equals(value, result)),
|
1471
|
+
action: "return"
|
1472
|
+
}
|
1473
|
+
].forEach(({ name, condition, action }) => {
|
1474
|
+
def(name, function(value) {
|
1475
|
+
const spy = getSpy(this);
|
1476
|
+
const pass = condition(spy, value);
|
1477
|
+
const isNot = utils.flag(this, "negate");
|
1478
|
+
if (pass && isNot || !pass && !isNot) {
|
1479
|
+
const spyName = spy.getMockName();
|
1480
|
+
const msg = utils.getMessage(
|
1481
|
+
this,
|
1482
|
+
[
|
1483
|
+
pass,
|
1484
|
+
`expected "${spyName}" to ${action} with: #{exp} at least once`,
|
1485
|
+
`expected "${spyName}" to not ${action} with: #{exp}`,
|
1486
|
+
value
|
1487
|
+
]
|
1488
|
+
);
|
1489
|
+
const results = action === "return" ? spy.mock.results : spy.mock.settledResults;
|
1490
|
+
throw new AssertionError(formatReturns(spy, results, msg, value));
|
1491
|
+
}
|
1492
|
+
});
|
1465
1493
|
});
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1494
|
+
[
|
1495
|
+
{
|
1496
|
+
name: "toHaveLastResolvedWith",
|
1497
|
+
condition: (spy, value) => {
|
1498
|
+
const result = spy.mock.settledResults[spy.mock.settledResults.length - 1];
|
1499
|
+
return result && result.type === "fulfilled" && equals(result.value, value);
|
1500
|
+
},
|
1501
|
+
action: "resolve"
|
1502
|
+
},
|
1503
|
+
{
|
1504
|
+
name: ["toHaveLastReturnedWith", "lastReturnedWith"],
|
1505
|
+
condition: (spy, value) => {
|
1506
|
+
const result = spy.mock.results[spy.mock.results.length - 1];
|
1507
|
+
return result && result.type === "return" && equals(result.value, value);
|
1508
|
+
},
|
1509
|
+
action: "return"
|
1510
|
+
}
|
1511
|
+
].forEach(({ name, condition, action }) => {
|
1512
|
+
def(name, function(value) {
|
1513
|
+
const spy = getSpy(this);
|
1514
|
+
const results = action === "return" ? spy.mock.results : spy.mock.settledResults;
|
1515
|
+
const result = results[results.length - 1];
|
1516
|
+
const spyName = spy.getMockName();
|
1517
|
+
this.assert(
|
1518
|
+
condition(spy, value),
|
1519
|
+
`expected last "${spyName}" call to ${action} #{exp}`,
|
1520
|
+
`expected last "${spyName}" call to not ${action} #{exp}`,
|
1521
|
+
value,
|
1522
|
+
result == null ? void 0 : result.value
|
1523
|
+
);
|
1524
|
+
});
|
1525
|
+
});
|
1526
|
+
[
|
1527
|
+
{
|
1528
|
+
name: "toHaveNthResolvedWith",
|
1529
|
+
condition: (spy, index, value) => {
|
1530
|
+
const result = spy.mock.settledResults[index - 1];
|
1531
|
+
return result && result.type === "fulfilled" && equals(result.value, value);
|
1532
|
+
},
|
1533
|
+
action: "resolve"
|
1534
|
+
},
|
1535
|
+
{
|
1536
|
+
name: ["toHaveNthReturnedWith", "nthReturnedWith"],
|
1537
|
+
condition: (spy, index, value) => {
|
1538
|
+
const result = spy.mock.results[index - 1];
|
1539
|
+
return result && result.type === "return" && equals(result.value, value);
|
1540
|
+
},
|
1541
|
+
action: "return"
|
1542
|
+
}
|
1543
|
+
].forEach(({ name, condition, action }) => {
|
1544
|
+
def(name, function(nthCall, value) {
|
1545
|
+
const spy = getSpy(this);
|
1546
|
+
const spyName = spy.getMockName();
|
1547
|
+
const results = action === "return" ? spy.mock.results : spy.mock.settledResults;
|
1548
|
+
const result = results[nthCall - 1];
|
1549
|
+
const ordinalCall = `${ordinalOf(nthCall)} call`;
|
1550
|
+
this.assert(
|
1551
|
+
condition(spy, nthCall, value),
|
1552
|
+
`expected ${ordinalCall} "${spyName}" call to ${action} #{exp}`,
|
1553
|
+
`expected ${ordinalCall} "${spyName}" call to not ${action} #{exp}`,
|
1554
|
+
value,
|
1555
|
+
result == null ? void 0 : result.value
|
1556
|
+
);
|
1557
|
+
});
|
1482
1558
|
});
|
1483
1559
|
def("toSatisfy", function(matcher, message) {
|
1484
1560
|
return this.be.satisfy(matcher, message);
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/expect",
|
3
3
|
"type": "module",
|
4
|
-
"version": "2.0.0-beta.
|
4
|
+
"version": "2.0.0-beta.4",
|
5
5
|
"description": "Jest's expect matchers as a Chai plugin",
|
6
6
|
"license": "MIT",
|
7
7
|
"funding": "https://opencollective.com/vitest",
|
@@ -31,14 +31,14 @@
|
|
31
31
|
],
|
32
32
|
"dependencies": {
|
33
33
|
"chai": "^5.1.1",
|
34
|
-
"@vitest/spy": "2.0.0-beta.
|
35
|
-
"@vitest/utils": "2.0.0-beta.
|
34
|
+
"@vitest/spy": "2.0.0-beta.4",
|
35
|
+
"@vitest/utils": "2.0.0-beta.4"
|
36
36
|
},
|
37
37
|
"devDependencies": {
|
38
38
|
"@types/chai": "4.3.6",
|
39
39
|
"picocolors": "^1.0.0",
|
40
40
|
"rollup-plugin-copy": "^3.5.0",
|
41
|
-
"@vitest/runner": "2.0.0-beta.
|
41
|
+
"@vitest/runner": "2.0.0-beta.4"
|
42
42
|
},
|
43
43
|
"scripts": {
|
44
44
|
"build": "rimraf dist && rollup -c",
|