@vitest/expect 4.1.0 → 4.1.2

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
@@ -164,6 +164,7 @@ interface SyncExpectationResult {
164
164
  message: () => string;
165
165
  actual?: any;
166
166
  expected?: any;
167
+ meta?: object;
167
168
  }
168
169
  type AsyncExpectationResult = Promise<SyncExpectationResult>;
169
170
  type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;
@@ -793,13 +794,13 @@ interface ChaiMockAssertion {
793
794
  */
794
795
  nthCalledWith: <E extends any[]>(n: number, ...args: E) => void;
795
796
  /**
796
- * Checks that a spy returned successfully at least once.
797
- * Chai-style equivalent of `toHaveReturned`.
797
+ * Checks that a spy returned a specific value at least once.
798
+ * Chai-style equivalent of `toHaveReturnedWith`.
798
799
  *
799
800
  * @example
800
- * expect(spy).to.have.returned
801
+ * expect(spy).to.have.returned('value')
801
802
  */
802
- readonly returned: Assertion;
803
+ returned: <E>(value: E) => void;
803
804
  /**
804
805
  * Checks that a spy returned a specific value at least once.
805
806
  * Chai-style equivalent of `toHaveReturnedWith`.
package/dist/index.js CHANGED
@@ -28,17 +28,18 @@ const ChaiStyleAssertions = (chai, utils) => {
28
28
  });
29
29
  }
30
30
  function defMethod(name, delegateTo) {
31
- utils.addChainableMethod(chai.Assertion.prototype, name, function(...args) {
31
+ utils.addMethod(chai.Assertion.prototype, name, function(...args) {
32
32
  const jestMethod = chai.Assertion.prototype[delegateTo];
33
33
  if (!jestMethod) {
34
34
  throw new Error(`Cannot delegate to ${String(delegateTo)}: method not found. Ensure JestChaiExpect plugin is loaded first.`);
35
35
  }
36
36
  return jestMethod.call(this, ...args);
37
- }, () => {});
37
+ });
38
38
  }
39
+ // API to (somewhat) mirror sinon-chai
40
+ // https://github.com/chaijs/sinon-chai
39
41
  defProperty("called", "toHaveBeenCalled");
40
42
  defProperty("calledOnce", "toHaveBeenCalledOnce");
41
- defProperty("returned", "toHaveReturned");
42
43
  defPropertyWithArgs("calledTwice", "toHaveBeenCalledTimes", 2);
43
44
  defPropertyWithArgs("calledThrice", "toHaveBeenCalledTimes", 3);
44
45
  defMethod("callCount", "toHaveBeenCalledTimes");
@@ -46,12 +47,15 @@ const ChaiStyleAssertions = (chai, utils) => {
46
47
  defMethod("calledOnceWith", "toHaveBeenCalledExactlyOnceWith");
47
48
  defMethod("lastCalledWith", "toHaveBeenLastCalledWith");
48
49
  defMethod("nthCalledWith", "toHaveBeenNthCalledWith");
50
+ defMethod("returned", "toHaveReturned");
49
51
  defMethod("returnedWith", "toHaveReturnedWith");
50
52
  defMethod("returnedTimes", "toHaveReturnedTimes");
51
53
  defMethod("lastReturnedWith", "toHaveLastReturnedWith");
52
54
  defMethod("nthReturnedWith", "toHaveNthReturnedWith");
53
55
  defMethod("calledBefore", "toHaveBeenCalledBefore");
54
56
  defMethod("calledAfter", "toHaveBeenCalledAfter");
57
+ // TODO: implement
58
+ // defMethod('thrown', 'toHaveThrown')
55
59
  };
56
60
 
57
61
  const MATCHERS_OBJECT = Symbol.for("matchers-object");
@@ -1733,7 +1737,7 @@ const JestChaiExpect = (chai, utils) => {
1733
1737
  }
1734
1738
  throw err;
1735
1739
  });
1736
- return recordAsyncExpect(test, promise, createAssertionMessage(utils, this, !!args.length), error);
1740
+ return recordAsyncExpect(test, promise, createAssertionMessage(utils, this, !!args.length), error, utils.flag(this, "soft"));
1737
1741
  };
1738
1742
  } });
1739
1743
  return proxy;
@@ -1774,7 +1778,7 @@ const JestChaiExpect = (chai, utils) => {
1774
1778
  }
1775
1779
  throw err;
1776
1780
  });
1777
- return recordAsyncExpect(test, promise, createAssertionMessage(utils, this, !!args.length), error);
1781
+ return recordAsyncExpect(test, promise, createAssertionMessage(utils, this, !!args.length), error, utils.flag(this, "soft"));
1778
1782
  };
1779
1783
  } });
1780
1784
  return proxy;
@@ -1851,10 +1855,11 @@ function getMatcherState(assertion, expect) {
1851
1855
  };
1852
1856
  }
1853
1857
  class JestExtendError extends Error {
1854
- constructor(message, actual, expected) {
1858
+ constructor(message, actual, expected, context) {
1855
1859
  super(message);
1856
1860
  this.actual = actual;
1857
1861
  this.expected = expected;
1862
+ this.context = context;
1858
1863
  }
1859
1864
  }
1860
1865
  function JestExtendPlugin(c, expect, matchers) {
@@ -1865,17 +1870,23 @@ function JestExtendPlugin(c, expect, matchers) {
1865
1870
  const result = expectAssertion.call(state, obj, ...args);
1866
1871
  if (result && typeof result === "object" && typeof result.then === "function") {
1867
1872
  const thenable = result;
1868
- return thenable.then(({ pass, message, actual, expected }) => {
1873
+ return thenable.then(({ pass, message, actual, expected, meta }) => {
1869
1874
  if (pass && isNot || !pass && !isNot) {
1870
1875
  const errorMessage = customMessage != null ? customMessage : message();
1871
- throw new JestExtendError(errorMessage, actual, expected);
1876
+ throw new JestExtendError(errorMessage, actual, expected, {
1877
+ assertionName: expectAssertionName,
1878
+ meta
1879
+ });
1872
1880
  }
1873
1881
  });
1874
1882
  }
1875
- const { pass, message, actual, expected } = result;
1883
+ const { pass, message, actual, expected, meta } = result;
1876
1884
  if (pass && isNot || !pass && !isNot) {
1877
1885
  const errorMessage = customMessage != null ? customMessage : message();
1878
- throw new JestExtendError(errorMessage, actual, expected);
1886
+ throw new JestExtendError(errorMessage, actual, expected, {
1887
+ assertionName: expectAssertionName,
1888
+ meta
1889
+ });
1879
1890
  }
1880
1891
  }
1881
1892
  const softWrapper = wrapAssertion(utils, expectAssertionName, expectWrapper);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/expect",
3
3
  "type": "module",
4
- "version": "4.1.0",
4
+ "version": "4.1.2",
5
5
  "description": "Jest's expect matchers as a Chai plugin",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -38,12 +38,12 @@
38
38
  "@standard-schema/spec": "^1.1.0",
39
39
  "@types/chai": "^5.2.2",
40
40
  "chai": "^6.2.2",
41
- "tinyrainbow": "^3.0.3",
42
- "@vitest/spy": "4.1.0",
43
- "@vitest/utils": "4.1.0"
41
+ "tinyrainbow": "^3.1.0",
42
+ "@vitest/spy": "4.1.2",
43
+ "@vitest/utils": "4.1.2"
44
44
  },
45
45
  "devDependencies": {
46
- "@vitest/runner": "4.1.0"
46
+ "@vitest/runner": "4.1.2"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "premove dist && rollup -c",