@vitest/expect 2.1.2 → 2.1.4

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
@@ -5,12 +5,12 @@ Jest's expect matchers as a Chai plugin.
5
5
  ## Usage
6
6
 
7
7
  ```js
8
- import * as chai from 'chai'
9
8
  import {
10
9
  JestAsymmetricMatchers,
11
10
  JestChaiExpect,
12
11
  JestExtend,
13
12
  } from '@vitest/expect'
13
+ import * as chai from 'chai'
14
14
 
15
15
  // allows using expect.extend instead of chai.use to extend plugins
16
16
  chai.use(JestExtend)
package/dist/index.d.ts CHANGED
@@ -1,9 +1,14 @@
1
+ import { stringify, Constructable } from '@vitest/utils';
1
2
  import * as tinyrainbow from 'tinyrainbow';
2
3
  import { Formatter } from 'tinyrainbow';
3
- import { stringify, Constructable } from '@vitest/utils';
4
4
  import { diff, printDiffOrStringify } from '@vitest/utils/diff';
5
5
  export { DiffOptions } from '@vitest/utils/diff';
6
6
 
7
+ declare const MATCHERS_OBJECT: unique symbol;
8
+ declare const JEST_MATCHERS_OBJECT: unique symbol;
9
+ declare const GLOBAL_EXPECT: unique symbol;
10
+ declare const ASYMMETRIC_MATCHERS_OBJECT: unique symbol;
11
+
7
12
  declare function matcherHint(matcherName: string, received?: string, expected?: string, options?: MatcherHintOptions): string;
8
13
  declare function printReceived(object: unknown): string;
9
14
  declare function printExpected(value: unknown): string;
@@ -227,6 +232,10 @@ declare class StringMatching extends AsymmetricMatcher<RegExp> {
227
232
  }
228
233
  declare const JestAsymmetricMatchers: ChaiPlugin;
229
234
 
235
+ declare const JestChaiExpect: ChaiPlugin;
236
+
237
+ declare const JestExtend: ChaiPlugin;
238
+
230
239
  declare function equals(a: unknown, b: unknown, customTesters?: Array<Tester>, strictCheck?: boolean): boolean;
231
240
  declare function isAsymmetric(obj: any): boolean;
232
241
  declare function hasAsymmetric(obj: any, seen?: Set<unknown>): boolean;
@@ -243,21 +252,12 @@ declare function sparseArrayEquality(a: unknown, b: unknown, customTesters?: Arr
243
252
  declare function generateToBeMessage(deepEqualityName: string, expected?: string, actual?: string): string;
244
253
  declare function pluralize(word: string, count: number): string;
245
254
  declare function getObjectKeys(object: object): Array<string | symbol>;
246
- declare function getObjectSubset(object: any, subset: any, customTesters?: Array<Tester>): {
255
+ declare function getObjectSubset(object: any, subset: any, customTesters: Array<Tester>): {
247
256
  subset: any;
248
257
  stripped: number;
249
258
  };
250
259
 
251
- declare const MATCHERS_OBJECT: unique symbol;
252
- declare const JEST_MATCHERS_OBJECT: unique symbol;
253
- declare const GLOBAL_EXPECT: unique symbol;
254
- declare const ASYMMETRIC_MATCHERS_OBJECT: unique symbol;
255
-
256
260
  declare function getState<State extends MatcherState = MatcherState>(expect: ExpectStatic): State;
257
261
  declare function setState<State extends MatcherState = MatcherState>(state: Partial<State>, expect: ExpectStatic): void;
258
262
 
259
- declare const JestChaiExpect: ChaiPlugin;
260
-
261
- declare const JestExtend: ChaiPlugin;
262
-
263
263
  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 PromisifyAssertion, 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
@@ -1,6 +1,6 @@
1
1
  import { getType, stringify, isObject, assertTypes } from '@vitest/utils';
2
- import c from 'tinyrainbow';
3
2
  import { diff, printDiffOrStringify } from '@vitest/utils/diff';
3
+ import c from 'tinyrainbow';
4
4
  import { isMockFunction } from '@vitest/spy';
5
5
  import { processError } from '@vitest/utils/error';
6
6
  import { use, util } from 'chai';
@@ -12,39 +12,6 @@ const ASYMMETRIC_MATCHERS_OBJECT = Symbol.for(
12
12
  "asymmetric-matchers-object"
13
13
  );
14
14
 
15
- if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
16
- const globalState = /* @__PURE__ */ new WeakMap();
17
- const matchers = /* @__PURE__ */ Object.create(null);
18
- const customEqualityTesters = [];
19
- const assymetricMatchers = /* @__PURE__ */ Object.create(null);
20
- Object.defineProperty(globalThis, MATCHERS_OBJECT, {
21
- get: () => globalState
22
- });
23
- Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, {
24
- configurable: true,
25
- get: () => ({
26
- state: globalState.get(globalThis[GLOBAL_EXPECT]),
27
- matchers,
28
- customEqualityTesters
29
- })
30
- });
31
- Object.defineProperty(globalThis, ASYMMETRIC_MATCHERS_OBJECT, {
32
- get: () => assymetricMatchers
33
- });
34
- }
35
- function getState(expect) {
36
- return globalThis[MATCHERS_OBJECT].get(expect);
37
- }
38
- function setState(state, expect) {
39
- const map = globalThis[MATCHERS_OBJECT];
40
- const current = map.get(expect) || {};
41
- const results = Object.defineProperties(current, {
42
- ...Object.getOwnPropertyDescriptors(current),
43
- ...Object.getOwnPropertyDescriptors(state)
44
- });
45
- map.set(expect, results);
46
- }
47
-
48
15
  const EXPECTED_COLOR = c.green;
49
16
  const RECEIVED_COLOR = c.red;
50
17
  const INVERTED_COLOR = c.inverse;
@@ -229,6 +196,7 @@ function eq(a, b, aStack, bStack, customTesters, hasKey2) {
229
196
  const numB = +b;
230
197
  return numA === numB || Number.isNaN(numA) && Number.isNaN(numB);
231
198
  }
199
+ // RegExps are compared by their source patterns and flags.
232
200
  case "[object RegExp]":
233
201
  return a.source === b.source && a.flags === b.flags;
234
202
  }
@@ -555,7 +523,7 @@ function getObjectKeys(object) {
555
523
  )
556
524
  ];
557
525
  }
558
- function getObjectSubset(object, subset, customTesters = []) {
526
+ function getObjectSubset(object, subset, customTesters) {
559
527
  let stripped = 0;
560
528
  const getObjectSubsetWithContext = (seenReferences = /* @__PURE__ */ new WeakMap()) => (object2, subset2) => {
561
529
  if (Array.isArray(object2)) {
@@ -576,6 +544,12 @@ function getObjectSubset(object, subset, customTesters = []) {
576
544
  }
577
545
  const trimmed = {};
578
546
  seenReferences.set(object2, trimmed);
547
+ if (typeof object2.constructor === "function" && typeof object2.constructor.name === "string") {
548
+ Object.defineProperty(trimmed, "constructor", {
549
+ enumerable: false,
550
+ value: object2.constructor
551
+ });
552
+ }
579
553
  for (const key of getObjectKeys(object2)) {
580
554
  if (hasPropertyInObject(subset2, key)) {
581
555
  trimmed[key] = seenReferences.has(object2[key]) ? seenReferences.get(object2[key]) : getObjectSubsetWithContext(seenReferences)(
@@ -604,6 +578,39 @@ function getObjectSubset(object, subset, customTesters = []) {
604
578
  return { subset: getObjectSubsetWithContext()(object, subset), stripped };
605
579
  }
606
580
 
581
+ if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
582
+ const globalState = /* @__PURE__ */ new WeakMap();
583
+ const matchers = /* @__PURE__ */ Object.create(null);
584
+ const customEqualityTesters = [];
585
+ const assymetricMatchers = /* @__PURE__ */ Object.create(null);
586
+ Object.defineProperty(globalThis, MATCHERS_OBJECT, {
587
+ get: () => globalState
588
+ });
589
+ Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, {
590
+ configurable: true,
591
+ get: () => ({
592
+ state: globalState.get(globalThis[GLOBAL_EXPECT]),
593
+ matchers,
594
+ customEqualityTesters
595
+ })
596
+ });
597
+ Object.defineProperty(globalThis, ASYMMETRIC_MATCHERS_OBJECT, {
598
+ get: () => assymetricMatchers
599
+ });
600
+ }
601
+ function getState(expect) {
602
+ return globalThis[MATCHERS_OBJECT].get(expect);
603
+ }
604
+ function setState(state, expect) {
605
+ const map = globalThis[MATCHERS_OBJECT];
606
+ const current = map.get(expect) || {};
607
+ const results = Object.defineProperties(current, {
608
+ ...Object.getOwnPropertyDescriptors(current),
609
+ ...Object.getOwnPropertyDescriptors(state)
610
+ });
611
+ map.set(expect, results);
612
+ }
613
+
607
614
  class AsymmetricMatcher {
608
615
  constructor(sample, inverse = false) {
609
616
  this.sample = sample;
@@ -916,9 +923,10 @@ function recordAsyncExpect(test, promise) {
916
923
  }
917
924
  return promise;
918
925
  }
919
- function wrapSoft(utils, fn) {
926
+ function wrapAssertion(utils, name, fn) {
920
927
  return function(...args) {
921
928
  var _a;
929
+ utils.flag(this, "_name", name);
922
930
  if (!utils.flag(this, "soft")) {
923
931
  return fn.apply(this, args);
924
932
  }
@@ -942,7 +950,7 @@ const JestChaiExpect = (chai, utils) => {
942
950
  const customTesters = getCustomEqualityTesters();
943
951
  function def(name, fn) {
944
952
  const addMethod = (n) => {
945
- const softWrapper = wrapSoft(utils, fn);
953
+ const softWrapper = wrapAssertion(utils, n, fn);
946
954
  utils.addMethod(chai.Assertion.prototype, n, softWrapper);
947
955
  utils.addMethod(
948
956
  globalThis[JEST_MATCHERS_OBJECT].matchers,
@@ -1068,7 +1076,8 @@ const JestChaiExpect = (chai, utils) => {
1068
1076
  const isNot = utils.flag(this, "negate");
1069
1077
  const { subset: actualSubset, stripped } = getObjectSubset(
1070
1078
  actual,
1071
- expected
1079
+ expected,
1080
+ customTesters
1072
1081
  );
1073
1082
  if (pass && isNot || !pass && !isNot) {
1074
1083
  const msg = utils.getMessage(this, [
@@ -1163,8 +1172,8 @@ const JestChaiExpect = (chai, utils) => {
1163
1172
  Boolean(obj),
1164
1173
  "expected #{this} to be truthy",
1165
1174
  "expected #{this} to not be truthy",
1166
- obj,
1167
- false
1175
+ true,
1176
+ obj
1168
1177
  );
1169
1178
  });
1170
1179
  def("toBeFalsy", function() {
@@ -1173,8 +1182,8 @@ const JestChaiExpect = (chai, utils) => {
1173
1182
  !obj,
1174
1183
  "expected #{this} to be falsy",
1175
1184
  "expected #{this} to not be falsy",
1176
- obj,
1177
- false
1185
+ false,
1186
+ obj
1178
1187
  );
1179
1188
  });
1180
1189
  def("toBeGreaterThan", function(expected) {
@@ -1185,8 +1194,8 @@ const JestChaiExpect = (chai, utils) => {
1185
1194
  actual > expected,
1186
1195
  `expected ${actual} to be greater than ${expected}`,
1187
1196
  `expected ${actual} to be not greater than ${expected}`,
1188
- actual,
1189
1197
  expected,
1198
+ actual,
1190
1199
  false
1191
1200
  );
1192
1201
  });
@@ -1198,8 +1207,8 @@ const JestChaiExpect = (chai, utils) => {
1198
1207
  actual >= expected,
1199
1208
  `expected ${actual} to be greater than or equal to ${expected}`,
1200
1209
  `expected ${actual} to be not greater than or equal to ${expected}`,
1201
- actual,
1202
1210
  expected,
1211
+ actual,
1203
1212
  false
1204
1213
  );
1205
1214
  });
@@ -1211,8 +1220,8 @@ const JestChaiExpect = (chai, utils) => {
1211
1220
  actual < expected,
1212
1221
  `expected ${actual} to be less than ${expected}`,
1213
1222
  `expected ${actual} to be not less than ${expected}`,
1214
- actual,
1215
1223
  expected,
1224
+ actual,
1216
1225
  false
1217
1226
  );
1218
1227
  });
@@ -1224,19 +1233,40 @@ const JestChaiExpect = (chai, utils) => {
1224
1233
  actual <= expected,
1225
1234
  `expected ${actual} to be less than or equal to ${expected}`,
1226
1235
  `expected ${actual} to be not less than or equal to ${expected}`,
1227
- actual,
1228
1236
  expected,
1237
+ actual,
1229
1238
  false
1230
1239
  );
1231
1240
  });
1232
1241
  def("toBeNaN", function() {
1233
- return this.be.NaN;
1242
+ const obj = utils.flag(this, "object");
1243
+ this.assert(
1244
+ Number.isNaN(obj),
1245
+ "expected #{this} to be NaN",
1246
+ "expected #{this} not to be NaN",
1247
+ Number.NaN,
1248
+ obj
1249
+ );
1234
1250
  });
1235
1251
  def("toBeUndefined", function() {
1236
- return this.be.undefined;
1252
+ const obj = utils.flag(this, "object");
1253
+ this.assert(
1254
+ void 0 === obj,
1255
+ "expected #{this} to be undefined",
1256
+ "expected #{this} not to be undefined",
1257
+ void 0,
1258
+ obj
1259
+ );
1237
1260
  });
1238
1261
  def("toBeNull", function() {
1239
- return this.be.null;
1262
+ const obj = utils.flag(this, "object");
1263
+ this.assert(
1264
+ obj === null,
1265
+ "expected #{this} to be null",
1266
+ "expected #{this} not to be null",
1267
+ null,
1268
+ obj
1269
+ );
1240
1270
  });
1241
1271
  def("toBeDefined", function() {
1242
1272
  const obj = utils.flag(this, "object");
@@ -1435,7 +1465,7 @@ const JestChaiExpect = (chai, utils) => {
1435
1465
  ["toThrow", "toThrowError"],
1436
1466
  function(expected) {
1437
1467
  if (typeof expected === "string" || typeof expected === "undefined" || expected instanceof RegExp) {
1438
- return this.throws(expected);
1468
+ return this.throws(expected === "" ? /^$/ : expected);
1439
1469
  }
1440
1470
  const obj = this._obj;
1441
1471
  const promise = utils.flag(this, "promise");
@@ -1913,7 +1943,7 @@ function JestExtendPlugin(c, expect, matchers) {
1913
1943
  throw new JestExtendError(message(), actual, expected);
1914
1944
  }
1915
1945
  }
1916
- const softWrapper = wrapSoft(utils, expectWrapper);
1946
+ const softWrapper = wrapAssertion(utils, expectAssertionName, expectWrapper);
1917
1947
  utils.addMethod(
1918
1948
  globalThis[JEST_MATCHERS_OBJECT].matchers,
1919
1949
  expectAssertionName,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/expect",
3
3
  "type": "module",
4
- "version": "2.1.2",
4
+ "version": "2.1.4",
5
5
  "description": "Jest's expect matchers as a Chai plugin",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -30,15 +30,15 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "chai": "^5.1.1",
33
+ "chai": "^5.1.2",
34
34
  "tinyrainbow": "^1.2.0",
35
- "@vitest/spy": "2.1.2",
36
- "@vitest/utils": "2.1.2"
35
+ "@vitest/spy": "2.1.4",
36
+ "@vitest/utils": "2.1.4"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/chai": "4.3.6",
40
40
  "rollup-plugin-copy": "^3.5.0",
41
- "@vitest/runner": "2.1.2"
41
+ "@vitest/runner": "2.1.4"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "rimraf dist && rollup -c",