@vitest/expect 4.0.0-beta.10 → 4.0.0-beta.11

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
@@ -1,8 +1,9 @@
1
1
  import { MockInstance } from '@vitest/spy';
2
- import { stringify, Constructable } from '@vitest/utils';
2
+ import { Constructable } from '@vitest/utils';
3
3
  import { Formatter } from 'tinyrainbow';
4
4
  import { diff, printDiffOrStringify } from '@vitest/utils/diff';
5
5
  export { DiffOptions } from '@vitest/utils/diff';
6
+ import { stringify } from '@vitest/utils/display';
6
7
  import * as chai from 'chai';
7
8
  export { chai };
8
9
 
@@ -12,7 +13,7 @@ declare const GLOBAL_EXPECT: unique symbol;
12
13
  declare const ASYMMETRIC_MATCHERS_OBJECT: unique symbol;
13
14
 
14
15
  interface AsymmetricMatcherInterface {
15
- asymmetricMatch: (other: unknown) => boolean;
16
+ asymmetricMatch: (other: unknown, customTesters?: Array<Tester>) => boolean;
16
17
  toString: () => string;
17
18
  getExpectedType?: () => string;
18
19
  toAsymmetricMatcher?: () => string;
@@ -26,7 +27,7 @@ declare abstract class AsymmetricMatcher<
26
27
  $$typeof: symbol;
27
28
  constructor(sample: T, inverse?: boolean);
28
29
  protected getMatcherContext(expect?: Chai.ExpectStatic): State;
29
- abstract asymmetricMatch(other: unknown): boolean;
30
+ abstract asymmetricMatch(other: unknown, customTesters?: Array<Tester>): boolean;
30
31
  abstract toString(): string;
31
32
  getExpectedType?(): string;
32
33
  toAsymmetricMatcher?(): string;
@@ -47,13 +48,13 @@ declare class ObjectContaining extends AsymmetricMatcher<Record<string | symbol
47
48
  getPrototype(obj: object): any;
48
49
  hasProperty(obj: object | null, property: string | symbol): boolean;
49
50
  getProperties(obj: object): (string | symbol)[];
50
- asymmetricMatch(other: any): boolean;
51
+ asymmetricMatch(other: any, customTesters?: Array<Tester>): boolean;
51
52
  toString(): string;
52
53
  getExpectedType(): string;
53
54
  }
54
55
  declare class ArrayContaining<T = unknown> extends AsymmetricMatcher<Array<T>> {
55
56
  constructor(sample: Array<T>, inverse?: boolean);
56
- asymmetricMatch(other: Array<T>): boolean;
57
+ asymmetricMatch(other: Array<T>, customTesters?: Array<Tester>): boolean;
57
58
  toString(): string;
58
59
  getExpectedType(): string;
59
60
  }
@@ -753,7 +754,7 @@ declare const JestChaiExpect: ChaiPlugin;
753
754
  declare const JestExtend: ChaiPlugin;
754
755
 
755
756
  declare function equals(a: unknown, b: unknown, customTesters?: Array<Tester>, strictCheck?: boolean): boolean;
756
- declare function isAsymmetric(obj: any): boolean;
757
+ declare function isAsymmetric(obj: any): obj is AsymmetricMatcher<any>;
757
758
  declare function hasAsymmetric(obj: any, seen?: Set<any>): boolean;
758
759
  declare function isA(typeName: string, value: unknown): boolean;
759
760
  declare function fnNameFor(func: Function): string;
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
- import { getType, stringify, isObject, noop, assertTypes } from '@vitest/utils';
2
1
  import { printDiffOrStringify, diff } from '@vitest/utils/diff';
2
+ import { stringify } from '@vitest/utils/display';
3
+ import { getType, isObject, noop, assertTypes } from '@vitest/utils/helpers';
3
4
  import c from 'tinyrainbow';
4
5
  import { isMockFunction } from '@vitest/spy';
5
6
  import { processError } from '@vitest/utils/error';
@@ -180,24 +181,24 @@ function hasAsymmetric(obj, seen = new Set()) {
180
181
  }
181
182
  return false;
182
183
  }
183
- function asymmetricMatch(a, b) {
184
+ function asymmetricMatch(a, b, customTesters) {
184
185
  const asymmetricA = isAsymmetric(a);
185
186
  const asymmetricB = isAsymmetric(b);
186
187
  if (asymmetricA && asymmetricB) {
187
188
  return undefined;
188
189
  }
189
190
  if (asymmetricA) {
190
- return a.asymmetricMatch(b);
191
+ return a.asymmetricMatch(b, customTesters);
191
192
  }
192
193
  if (asymmetricB) {
193
- return b.asymmetricMatch(a);
194
+ return b.asymmetricMatch(a, customTesters);
194
195
  }
195
196
  }
196
197
  // Equality function lovingly adapted from isEqual in
197
198
  // [Underscore](http://underscorejs.org)
198
199
  function eq(a, b, aStack, bStack, customTesters, hasKey) {
199
200
  let result = true;
200
- const asymmetricResult = asymmetricMatch(a, b);
201
+ const asymmetricResult = asymmetricMatch(a, b, customTesters);
201
202
  if (asymmetricResult !== undefined) {
202
203
  return asymmetricResult;
203
204
  }
@@ -782,12 +783,11 @@ class ObjectContaining extends AsymmetricMatcher {
782
783
  return (_Object$getOwnPropert = Object.getOwnPropertyDescriptor(obj, s)) === null || _Object$getOwnPropert === void 0 ? void 0 : _Object$getOwnPropert.enumerable;
783
784
  })];
784
785
  }
785
- asymmetricMatch(other) {
786
+ asymmetricMatch(other, customTesters) {
786
787
  if (typeof this.sample !== "object") {
787
788
  throw new TypeError(`You must provide an object to ${this.toString()}, not '${typeof this.sample}'.`);
788
789
  }
789
790
  let result = true;
790
- const matcherContext = this.getMatcherContext();
791
791
  const properties = this.getProperties(this.sample);
792
792
  for (const property of properties) {
793
793
  var _Object$getOwnPropert2, _Object$getOwnPropert3;
@@ -797,7 +797,7 @@ class ObjectContaining extends AsymmetricMatcher {
797
797
  }
798
798
  const value = ((_Object$getOwnPropert2 = Object.getOwnPropertyDescriptor(this.sample, property)) === null || _Object$getOwnPropert2 === void 0 ? void 0 : _Object$getOwnPropert2.value) ?? this.sample[property];
799
799
  const otherValue = ((_Object$getOwnPropert3 = Object.getOwnPropertyDescriptor(other, property)) === null || _Object$getOwnPropert3 === void 0 ? void 0 : _Object$getOwnPropert3.value) ?? other[property];
800
- if (!equals(value, otherValue, matcherContext.customTesters)) {
800
+ if (!equals(value, otherValue, customTesters)) {
801
801
  result = false;
802
802
  break;
803
803
  }
@@ -815,12 +815,11 @@ class ArrayContaining extends AsymmetricMatcher {
815
815
  constructor(sample, inverse = false) {
816
816
  super(sample, inverse);
817
817
  }
818
- asymmetricMatch(other) {
818
+ asymmetricMatch(other, customTesters) {
819
819
  if (!Array.isArray(this.sample)) {
820
820
  throw new TypeError(`You must provide an array to ${this.toString()}, not '${typeof this.sample}'.`);
821
821
  }
822
- const matcherContext = this.getMatcherContext();
823
- const result = this.sample.length === 0 || Array.isArray(other) && this.sample.every((item) => other.some((another) => equals(item, another, matcherContext.customTesters)));
822
+ const result = this.sample.length === 0 || Array.isArray(other) && this.sample.every((item) => other.some((another) => equals(item, another, customTesters)));
824
823
  return this.inverse ? !result : result;
825
824
  }
826
825
  toString() {
@@ -1715,6 +1714,7 @@ function getMatcherState(assertion, expect) {
1715
1714
  const obj = assertion._obj;
1716
1715
  const isNot = util.flag(assertion, "negate");
1717
1716
  const promise = util.flag(assertion, "promise") || "";
1717
+ const customMessage = util.flag(assertion, "message");
1718
1718
  const jestUtils = {
1719
1719
  ...getMatcherUtils(),
1720
1720
  diff,
@@ -1736,7 +1736,8 @@ function getMatcherState(assertion, expect) {
1736
1736
  return {
1737
1737
  state: matcherState,
1738
1738
  isNot,
1739
- obj
1739
+ obj,
1740
+ customMessage
1740
1741
  };
1741
1742
  }
1742
1743
  class JestExtendError extends Error {
@@ -1750,19 +1751,21 @@ function JestExtendPlugin(c, expect, matchers) {
1750
1751
  return (_, utils) => {
1751
1752
  Object.entries(matchers).forEach(([expectAssertionName, expectAssertion]) => {
1752
1753
  function expectWrapper(...args) {
1753
- const { state, isNot, obj } = getMatcherState(this, expect);
1754
+ const { state, isNot, obj, customMessage } = getMatcherState(this, expect);
1754
1755
  const result = expectAssertion.call(state, obj, ...args);
1755
1756
  if (result && typeof result === "object" && typeof result.then === "function") {
1756
1757
  const thenable = result;
1757
1758
  return thenable.then(({ pass, message, actual, expected }) => {
1758
1759
  if (pass && isNot || !pass && !isNot) {
1759
- throw new JestExtendError(message(), actual, expected);
1760
+ const errorMessage = customMessage != null ? customMessage : message();
1761
+ throw new JestExtendError(errorMessage, actual, expected);
1760
1762
  }
1761
1763
  });
1762
1764
  }
1763
1765
  const { pass, message, actual, expected } = result;
1764
1766
  if (pass && isNot || !pass && !isNot) {
1765
- throw new JestExtendError(message(), actual, expected);
1767
+ const errorMessage = customMessage != null ? customMessage : message();
1768
+ throw new JestExtendError(errorMessage, actual, expected);
1766
1769
  }
1767
1770
  }
1768
1771
  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.0.0-beta.10",
4
+ "version": "4.0.0-beta.11",
5
5
  "description": "Jest's expect matchers as a Chai plugin",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -31,12 +31,12 @@
31
31
  "dependencies": {
32
32
  "@types/chai": "^5.2.2",
33
33
  "chai": "^6.0.1",
34
- "tinyrainbow": "^2.0.0",
35
- "@vitest/spy": "4.0.0-beta.10",
36
- "@vitest/utils": "4.0.0-beta.10"
34
+ "tinyrainbow": "^3.0.3",
35
+ "@vitest/spy": "4.0.0-beta.11",
36
+ "@vitest/utils": "4.0.0-beta.11"
37
37
  },
38
38
  "devDependencies": {
39
- "@vitest/runner": "4.0.0-beta.10"
39
+ "@vitest/runner": "4.0.0-beta.11"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "rimraf dist && rollup -c",