@vitest/expect 1.3.1 → 1.5.0

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
@@ -237,6 +237,11 @@ declare function arrayBufferEquality(a: unknown, b: unknown): boolean | undefine
237
237
  declare function sparseArrayEquality(a: unknown, b: unknown, customTesters?: Array<Tester>): boolean | undefined;
238
238
  declare function generateToBeMessage(deepEqualityName: string, expected?: string, actual?: string): string;
239
239
  declare function pluralize(word: string, count: number): string;
240
+ declare function getObjectKeys(object: object): Array<string | symbol>;
241
+ declare function getObjectSubset(object: any, subset: any, customTesters?: Array<Tester>): {
242
+ subset: any;
243
+ stripped: number;
244
+ };
240
245
 
241
246
  declare const MATCHERS_OBJECT: unique symbol;
242
247
  declare const JEST_MATCHERS_OBJECT: unique symbol;
@@ -250,4 +255,4 @@ declare const JestChaiExpect: ChaiPlugin;
250
255
 
251
256
  declare const JestExtend: ChaiPlugin;
252
257
 
253
- export { ASYMMETRIC_MATCHERS_OBJECT, Any, Anything, ArrayContaining, type Assertion, AsymmetricMatcher, type AsymmetricMatcherInterface, type AsymmetricMatchersContaining, type AsyncExpectationResult, type ChaiPlugin, type ExpectStatic, type ExpectationResult, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, type JestAssertion, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, type MatcherHintOptions, type MatcherState, type MatchersObject, ObjectContaining, type RawMatcherFn, StringContaining, StringMatching, type SyncExpectationResult, type Tester, type TesterContext, addCustomEqualityTesters, arrayBufferEquality, equals, fnNameFor, generateToBeMessage, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, pluralize, setState, sparseArrayEquality, subsetEquality, typeEquality };
258
+ export { ASYMMETRIC_MATCHERS_OBJECT, Any, Anything, ArrayContaining, type Assertion, AsymmetricMatcher, type AsymmetricMatcherInterface, type AsymmetricMatchersContaining, type AsyncExpectationResult, type ChaiPlugin, type ExpectStatic, type ExpectationResult, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, type JestAssertion, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, type MatcherHintOptions, type MatcherState, type MatchersObject, ObjectContaining, type RawMatcherFn, StringContaining, StringMatching, type SyncExpectationResult, type Tester, type TesterContext, addCustomEqualityTesters, arrayBufferEquality, equals, fnNameFor, generateToBeMessage, getObjectKeys, getObjectSubset, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, pluralize, setState, sparseArrayEquality, subsetEquality, typeEquality };
package/dist/index.js CHANGED
@@ -446,6 +446,57 @@ Received: serializes to the same string
446
446
  function pluralize(word, count) {
447
447
  return `${count} ${word}${count === 1 ? "" : "s"}`;
448
448
  }
449
+ function getObjectKeys(object) {
450
+ return [
451
+ ...Object.keys(object),
452
+ ...Object.getOwnPropertySymbols(object).filter(
453
+ (s) => {
454
+ var _a;
455
+ return (_a = Object.getOwnPropertyDescriptor(object, s)) == null ? void 0 : _a.enumerable;
456
+ }
457
+ )
458
+ ];
459
+ }
460
+ function getObjectSubset(object, subset, customTesters = []) {
461
+ let stripped = 0;
462
+ const getObjectSubsetWithContext = (seenReferences = /* @__PURE__ */ new WeakMap()) => (object2, subset2) => {
463
+ if (Array.isArray(object2)) {
464
+ if (Array.isArray(subset2) && subset2.length === object2.length) {
465
+ return subset2.map(
466
+ (sub, i) => getObjectSubsetWithContext(seenReferences)(object2[i], sub)
467
+ );
468
+ }
469
+ } else if (object2 instanceof Date) {
470
+ return object2;
471
+ } else if (isObject(object2) && isObject(subset2)) {
472
+ if (equals(object2, subset2, [
473
+ ...customTesters,
474
+ iterableEquality,
475
+ subsetEquality
476
+ ])) {
477
+ return subset2;
478
+ }
479
+ const trimmed = {};
480
+ seenReferences.set(object2, trimmed);
481
+ for (const key of getObjectKeys(object2)) {
482
+ if (hasPropertyInObject(subset2, key)) {
483
+ trimmed[key] = seenReferences.has(object2[key]) ? seenReferences.get(object2[key]) : getObjectSubsetWithContext(seenReferences)(object2[key], subset2[key]);
484
+ } else {
485
+ if (!seenReferences.has(object2[key])) {
486
+ stripped += 1;
487
+ if (isObject(object2[key]))
488
+ stripped += getObjectKeys(object2[key]).length;
489
+ getObjectSubsetWithContext(seenReferences)(object2[key], subset2[key]);
490
+ }
491
+ }
492
+ }
493
+ if (getObjectKeys(trimmed).length > 0)
494
+ return trimmed;
495
+ }
496
+ return object2;
497
+ };
498
+ return { subset: getObjectSubsetWithContext()(object, subset), stripped };
499
+ }
449
500
 
450
501
  class AsymmetricMatcher {
451
502
  constructor(sample, inverse = false) {
@@ -876,20 +927,37 @@ const JestChaiExpect = (chai, utils) => {
876
927
  });
877
928
  def("toMatchObject", function(expected) {
878
929
  const actual = this._obj;
930
+ const pass = equals(actual, expected, [...customTesters, iterableEquality, subsetEquality]);
931
+ const isNot = utils.flag(this, "negate");
932
+ const { subset: actualSubset, stripped } = getObjectSubset(actual, expected);
933
+ const msg = utils.getMessage(
934
+ this,
935
+ [
936
+ pass,
937
+ "expected #{this} to match object #{exp}",
938
+ "expected #{this} to not match object #{exp}",
939
+ expected,
940
+ actualSubset
941
+ ]
942
+ );
943
+ if (pass && isNot || !pass && !isNot) {
944
+ const message = stripped === 0 ? msg : `${msg}
945
+ (${stripped} matching ${stripped === 1 ? "property" : "properties"} omitted from actual)`;
946
+ throw new AssertionError(message, { showDiff: true, expected, actual: actualSubset });
947
+ }
948
+ });
949
+ def("toMatch", function(expected) {
950
+ const actual = this._obj;
951
+ if (typeof actual !== "string")
952
+ throw new TypeError(`.toMatch() expects to receive a string, but got ${typeof actual}`);
879
953
  return this.assert(
880
- equals(actual, expected, [...customTesters, iterableEquality, subsetEquality]),
881
- "expected #{this} to match object #{exp}",
882
- "expected #{this} to not match object #{exp}",
954
+ typeof expected === "string" ? actual.includes(expected) : actual.match(expected),
955
+ `expected #{this} to match #{exp}`,
956
+ `expected #{this} not to match #{exp}`,
883
957
  expected,
884
958
  actual
885
959
  );
886
960
  });
887
- def("toMatch", function(expected) {
888
- if (typeof expected === "string")
889
- return this.include(expected);
890
- else
891
- return this.match(expected);
892
- });
893
961
  def("toContain", function(item) {
894
962
  const actual = this._obj;
895
963
  if (typeof Node !== "undefined" && actual instanceof Node) {
@@ -915,6 +983,15 @@ const JestChaiExpect = (chai, utils) => {
915
983
  actual.value
916
984
  );
917
985
  }
986
+ if (typeof actual === "string" && typeof item === "string") {
987
+ return this.assert(
988
+ actual.includes(item),
989
+ `expected #{this} to contain #{exp}`,
990
+ `expected #{this} not to contain #{exp}`,
991
+ item,
992
+ actual
993
+ );
994
+ }
918
995
  if (actual != null && typeof actual !== "string")
919
996
  utils.flag(this, "object", Array.from(actual));
920
997
  return this.contain(item);
@@ -1214,12 +1291,15 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1214
1291
  const spy = getSpy(this);
1215
1292
  const spyName = spy.getMockName();
1216
1293
  const nthCall = spy.mock.calls[times - 1];
1294
+ const callCount = spy.mock.calls.length;
1295
+ const isCalled = times <= callCount;
1217
1296
  this.assert(
1218
1297
  equals(nthCall, args, [...customTesters, iterableEquality]),
1219
- `expected ${ordinalOf(times)} "${spyName}" call to have been called with #{exp}`,
1298
+ `expected ${ordinalOf(times)} "${spyName}" call to have been called with #{exp}${isCalled ? `` : `, but called only ${callCount} times`}`,
1220
1299
  `expected ${ordinalOf(times)} "${spyName}" call to not have been called with #{exp}`,
1221
1300
  args,
1222
- nthCall
1301
+ nthCall,
1302
+ isCalled
1223
1303
  );
1224
1304
  });
1225
1305
  def(["toHaveBeenLastCalledWith", "lastCalledWith"], function(...args) {
@@ -1551,4 +1631,4 @@ const JestExtend = (chai, utils) => {
1551
1631
  });
1552
1632
  };
1553
1633
 
1554
- export { ASYMMETRIC_MATCHERS_OBJECT, Any, Anything, ArrayContaining, AsymmetricMatcher, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, ObjectContaining, StringContaining, StringMatching, addCustomEqualityTesters, arrayBufferEquality, equals, fnNameFor, generateToBeMessage, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, pluralize, setState, sparseArrayEquality, subsetEquality, typeEquality };
1634
+ export { ASYMMETRIC_MATCHERS_OBJECT, Any, Anything, ArrayContaining, AsymmetricMatcher, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, ObjectContaining, StringContaining, StringMatching, addCustomEqualityTesters, arrayBufferEquality, equals, fnNameFor, generateToBeMessage, getObjectKeys, getObjectSubset, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, pluralize, setState, sparseArrayEquality, subsetEquality, typeEquality };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/expect",
3
3
  "type": "module",
4
- "version": "1.3.1",
4
+ "version": "1.5.0",
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": "^4.3.10",
34
- "@vitest/spy": "1.3.1",
35
- "@vitest/utils": "1.3.1"
34
+ "@vitest/utils": "1.5.0",
35
+ "@vitest/spy": "1.5.0"
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": "1.3.1"
41
+ "@vitest/runner": "1.5.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "rimraf dist && rollup -c",