@vitest/expect 4.1.0-beta.4 → 4.1.0-beta.6

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # @vitest/expect
2
2
 
3
+ [![NPM version](https://img.shields.io/npm/v/@vitest/runner?color=a1b858&label=)](https://npmx.dev/package/@vitest/runner)
4
+
3
5
  Jest's expect matchers as a Chai plugin.
4
6
 
5
7
  ## Usage
@@ -19,3 +21,5 @@ chai.use(JestChaiExpect)
19
21
  // adds asymmetric matchers like stringContaining, objectContaining
20
22
  chai.use(JestAsymmetricMatchers)
21
23
  ```
24
+
25
+ [GitHub](https://github.com/vitest-dev/vitest/tree/main/packages/expect) | [Documentation](https://vitest.dev/api/expect)
package/dist/index.d.ts CHANGED
@@ -240,8 +240,6 @@ interface AsymmetricMatchersContaining extends CustomMatcher {
240
240
  /**
241
241
  * Matches if the received number is within a certain precision of the expected number.
242
242
  *
243
- * @param precision - Optional decimal precision for comparison. Default is 2.
244
- *
245
243
  * @example
246
244
  * expect(10.45).toEqual(expect.closeTo(10.5, 1));
247
245
  * expect(5.11).toEqual(expect.closeTo(5.12)); // with default precision
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { printDiffOrStringify, diff } from '@vitest/utils/diff';
2
2
  import { stringify } from '@vitest/utils/display';
3
- import { getType, isObject, noop, assertTypes } from '@vitest/utils/helpers';
3
+ import { getType, isObject, noop, assertTypes, ordinal } from '@vitest/utils/helpers';
4
4
  import c from 'tinyrainbow';
5
5
  import { isMockFunction } from '@vitest/spy';
6
6
  import { processError } from '@vitest/utils/error';
@@ -1503,7 +1503,7 @@ const JestChaiExpect = (chai, utils) => {
1503
1503
  const nthCall = spy.mock.calls[times - 1];
1504
1504
  const callCount = spy.mock.calls.length;
1505
1505
  const isCalled = times <= callCount;
1506
- this.assert(nthCall && equalsArgumentArray(nthCall, args), `expected ${ordinalOf(times)} "${spyName}" call to have been called with #{exp}${isCalled ? `` : `, but called only ${callCount} times`}`, `expected ${ordinalOf(times)} "${spyName}" call to not have been called with #{exp}`, args, nthCall, isCalled);
1506
+ this.assert(nthCall && equalsArgumentArray(nthCall, args), `expected ${ordinal(times)} "${spyName}" call to have been called with #{exp}${isCalled ? `` : `, but called only ${callCount} times`}`, `expected ${ordinal(times)} "${spyName}" call to not have been called with #{exp}`, args, nthCall, isCalled);
1507
1507
  });
1508
1508
  def("toHaveBeenLastCalledWith", function(...args) {
1509
1509
  const spy = getSpy(this);
@@ -1688,7 +1688,7 @@ const JestChaiExpect = (chai, utils) => {
1688
1688
  const spyName = spy.getMockName();
1689
1689
  const results = action === "return" ? spy.mock.results : spy.mock.settledResults;
1690
1690
  const result = results[nthCall - 1];
1691
- const ordinalCall = `${ordinalOf(nthCall)} call`;
1691
+ const ordinalCall = `${ordinal(nthCall)} call`;
1692
1692
  this.assert(condition(spy, nthCall, value), `expected ${ordinalCall} "${spyName}" call to ${action} #{exp}`, `expected ${ordinalCall} "${spyName}" call to not ${action} #{exp}`, value, result?.value);
1693
1693
  });
1694
1694
  });
@@ -1718,14 +1718,18 @@ const JestChaiExpect = (chai, utils) => {
1718
1718
  }
1719
1719
  return (...args) => {
1720
1720
  utils.flag(this, "_name", key);
1721
- const promise = obj.then((value) => {
1721
+ const promise = Promise.resolve(obj).then((value) => {
1722
1722
  utils.flag(this, "object", value);
1723
1723
  return result.call(this, ...args);
1724
1724
  }, (err) => {
1725
1725
  const _error = new AssertionError(`promise rejected "${utils.inspect(err)}" instead of resolving`, { showDiff: false });
1726
1726
  _error.cause = err;
1727
- _error.stack = error.stack.replace(error.message, _error.message);
1728
1727
  throw _error;
1728
+ }).catch((err) => {
1729
+ if (isError(err) && error.stack) {
1730
+ err.stack = error.stack.replace(error.message, err.message);
1731
+ }
1732
+ throw err;
1729
1733
  });
1730
1734
  return recordAsyncExpect(test, promise, createAssertionMessage(utils, this, !!args.length), error);
1731
1735
  };
@@ -1752,17 +1756,21 @@ const JestChaiExpect = (chai, utils) => {
1752
1756
  }
1753
1757
  return (...args) => {
1754
1758
  utils.flag(this, "_name", key);
1755
- const promise = wrapper.then((value) => {
1759
+ const promise = Promise.resolve(wrapper).then((value) => {
1756
1760
  const _error = new AssertionError(`promise resolved "${utils.inspect(value)}" instead of rejecting`, {
1757
1761
  showDiff: true,
1758
1762
  expected: new Error("rejected promise"),
1759
1763
  actual: value
1760
1764
  });
1761
- _error.stack = error.stack.replace(error.message, _error.message);
1762
1765
  throw _error;
1763
1766
  }, (err) => {
1764
1767
  utils.flag(this, "object", err);
1765
1768
  return result.call(this, ...args);
1769
+ }).catch((err) => {
1770
+ if (isError(err) && error.stack) {
1771
+ err.stack = error.stack.replace(error.message, err.message);
1772
+ }
1773
+ throw err;
1766
1774
  });
1767
1775
  return recordAsyncExpect(test, promise, createAssertionMessage(utils, this, !!args.length), error);
1768
1776
  };
@@ -1770,24 +1778,10 @@ const JestChaiExpect = (chai, utils) => {
1770
1778
  return proxy;
1771
1779
  });
1772
1780
  };
1773
- function ordinalOf(i) {
1774
- const j = i % 10;
1775
- const k = i % 100;
1776
- if (j === 1 && k !== 11) {
1777
- return `${i}st`;
1778
- }
1779
- if (j === 2 && k !== 12) {
1780
- return `${i}nd`;
1781
- }
1782
- if (j === 3 && k !== 13) {
1783
- return `${i}rd`;
1784
- }
1785
- return `${i}th`;
1786
- }
1787
1781
  function formatCalls(spy, msg, showActualCall) {
1788
1782
  if (spy.mock.calls.length) {
1789
- msg += c.gray(`\n\nReceived: \n\n${spy.mock.calls.map((callArg, i) => {
1790
- let methodCall = c.bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call:\n\n`);
1783
+ msg += c.gray(`\n\nReceived:\n\n${spy.mock.calls.map((callArg, i) => {
1784
+ let methodCall = c.bold(` ${ordinal(i + 1)} ${spy.getMockName()} call:\n\n`);
1791
1785
  if (showActualCall) {
1792
1786
  methodCall += diff(showActualCall, callArg, { omitAnnotationLines: true });
1793
1787
  } else {
@@ -1802,8 +1796,8 @@ function formatCalls(spy, msg, showActualCall) {
1802
1796
  }
1803
1797
  function formatReturns(spy, results, msg, showActualReturn) {
1804
1798
  if (results.length) {
1805
- msg += c.gray(`\n\nReceived: \n\n${results.map((callReturn, i) => {
1806
- let methodCall = c.bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call return:\n\n`);
1799
+ msg += c.gray(`\n\nReceived:\n\n${results.map((callReturn, i) => {
1800
+ let methodCall = c.bold(` ${ordinal(i + 1)} ${spy.getMockName()} call return:\n\n`);
1807
1801
  if (showActualReturn) {
1808
1802
  methodCall += diff(showActualReturn, callReturn.value, { omitAnnotationLines: true });
1809
1803
  } else {
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@vitest/expect",
3
3
  "type": "module",
4
- "version": "4.1.0-beta.4",
4
+ "version": "4.1.0-beta.6",
5
5
  "description": "Jest's expect matchers as a Chai plugin",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
8
- "homepage": "https://github.com/vitest-dev/vitest/tree/main/packages/expect#readme",
8
+ "homepage": "https://vitest.dev/api/expect",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https://github.com/vitest-dev/vitest.git",
@@ -14,6 +14,12 @@
14
14
  "bugs": {
15
15
  "url": "https://github.com/vitest-dev/vitest/issues"
16
16
  },
17
+ "keywords": [
18
+ "vitest",
19
+ "test",
20
+ "chai",
21
+ "assertion"
22
+ ],
17
23
  "sideEffects": false,
18
24
  "exports": {
19
25
  ".": {
@@ -33,11 +39,11 @@
33
39
  "@types/chai": "^5.2.2",
34
40
  "chai": "^6.2.2",
35
41
  "tinyrainbow": "^3.0.3",
36
- "@vitest/spy": "4.1.0-beta.4",
37
- "@vitest/utils": "4.1.0-beta.4"
42
+ "@vitest/spy": "4.1.0-beta.6",
43
+ "@vitest/utils": "4.1.0-beta.6"
38
44
  },
39
45
  "devDependencies": {
40
- "@vitest/runner": "4.1.0-beta.4"
46
+ "@vitest/runner": "4.1.0-beta.6"
41
47
  },
42
48
  "scripts": {
43
49
  "build": "premove dist && rollup -c",