@vitest/expect 2.0.0-beta.3 → 2.0.0-beta.5

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 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, actualCall) => {
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 (actualCall)
1218
- methodCall += diff(actualCall, callArg, { omitAnnotationLines: true });
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, actualReturn) => {
1231
+ const formatReturns = (spy, results, msg, showActualReturn) => {
1232
1232
  msg += c().gray(`
1233
1233
 
1234
1234
  Received:
1235
1235
 
1236
- ${spy.mock.results.map((callReturn, i) => {
1236
+ ${results.map((callReturn, i) => {
1237
1237
  let methodCall = c().bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call return:
1238
1238
 
1239
1239
  `);
1240
- if (actualReturn)
1241
- methodCall += diff(actualReturn, callReturn.value, { omitAnnotationLines: true });
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
- def(["toHaveReturned", "toReturn"], function() {
1411
- const spy = getSpy(this);
1412
- const spyName = spy.getMockName();
1413
- const calledAndNotThrew = spy.mock.calls.length > 0 && spy.mock.results.some(({ type }) => type !== "throw");
1414
- this.assert(
1415
- calledAndNotThrew,
1416
- `expected "${spyName}" to be successfully called at least once`,
1417
- `expected "${spyName}" to not be successfully called`,
1418
- calledAndNotThrew,
1419
- !calledAndNotThrew,
1420
- false
1421
- );
1422
- });
1423
- def(["toHaveReturnedTimes", "toReturnTimes"], function(times) {
1424
- const spy = getSpy(this);
1425
- const spyName = spy.getMockName();
1426
- const successfulReturns = spy.mock.results.reduce((success, { type }) => type === "throw" ? success : ++success, 0);
1427
- this.assert(
1428
- successfulReturns === times,
1429
- `expected "${spyName}" to be successfully called ${times} times`,
1430
- `expected "${spyName}" to not be successfully called ${times} times`,
1431
- `expected number of returns: ${times}`,
1432
- `received number of returns: ${successfulReturns}`,
1433
- false
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
- def(["toHaveReturnedWith", "toReturnWith"], function(value) {
1437
- const spy = getSpy(this);
1438
- const spyName = spy.getMockName();
1439
- const pass = spy.mock.results.some(({ type, value: result }) => type === "return" && equals(value, result));
1440
- const isNot = utils.flag(this, "negate");
1441
- const msg = utils.getMessage(
1442
- this,
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 return with: #{exp} at least once`,
1446
- `expected "${spyName}" to not return with: #{exp}`,
1447
- value
1448
- ]
1449
- );
1450
- if (pass && isNot || !pass && !isNot)
1451
- throw new AssertionError(formatReturns(spy, msg, value));
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
- def(["toHaveLastReturnedWith", "lastReturnedWith"], function(value) {
1454
- const spy = getSpy(this);
1455
- const spyName = spy.getMockName();
1456
- const { value: lastResult } = spy.mock.results[spy.mock.results.length - 1];
1457
- const pass = equals(lastResult, value);
1458
- this.assert(
1459
- pass,
1460
- `expected last "${spyName}" call to return #{exp}`,
1461
- `expected last "${spyName}" call to not return #{exp}`,
1462
- value,
1463
- lastResult
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
- def(["toHaveNthReturnedWith", "nthReturnedWith"], function(nthCall, value) {
1467
- const spy = getSpy(this);
1468
- const spyName = spy.getMockName();
1469
- const isNot = utils.flag(this, "negate");
1470
- const { type: callType, value: callResult } = spy.mock.results[nthCall - 1];
1471
- const ordinalCall = `${ordinalOf(nthCall)} call`;
1472
- if (!isNot && callType === "throw")
1473
- chai.assert.fail(`expected ${ordinalCall} to return #{exp}, but instead it threw an error`);
1474
- const nthCallReturn = equals(callResult, value);
1475
- this.assert(
1476
- nthCallReturn,
1477
- `expected ${ordinalCall} "${spyName}" call to return #{exp}`,
1478
- `expected ${ordinalCall} "${spyName}" call to not return #{exp}`,
1479
- value,
1480
- callResult
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.3",
4
+ "version": "2.0.0-beta.5",
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.3",
35
- "@vitest/utils": "2.0.0-beta.3"
34
+ "@vitest/utils": "2.0.0-beta.5",
35
+ "@vitest/spy": "2.0.0-beta.5"
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.3"
41
+ "@vitest/runner": "2.0.0-beta.5"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "rimraf dist && rollup -c",