@vitest/expect 0.29.8 → 0.30.1

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
@@ -5,11 +5,31 @@ export { setupColors } from '@vitest/utils';
5
5
  type Formatter = (input: string | number | null | undefined) => string;
6
6
 
7
7
  declare function getMatcherUtils(): {
8
- EXPECTED_COLOR: (input: unknown) => string;
9
- RECEIVED_COLOR: (input: unknown) => string;
10
- INVERTED_COLOR: (input: unknown) => string;
11
- BOLD_WEIGHT: (input: unknown) => string;
12
- DIM_COLOR: (input: unknown) => string;
8
+ EXPECTED_COLOR: {
9
+ (input: unknown): string;
10
+ open: string;
11
+ close: string;
12
+ };
13
+ RECEIVED_COLOR: {
14
+ (input: unknown): string;
15
+ open: string;
16
+ close: string;
17
+ };
18
+ INVERTED_COLOR: {
19
+ (input: unknown): string;
20
+ open: string;
21
+ close: string;
22
+ };
23
+ BOLD_WEIGHT: {
24
+ (input: unknown): string;
25
+ open: string;
26
+ close: string;
27
+ };
28
+ DIM_COLOR: {
29
+ (input: unknown): string;
30
+ open: string;
31
+ close: string;
32
+ };
13
33
  matcherHint: (matcherName: string, received?: string, expected?: string, options?: MatcherHintOptions) => string;
14
34
  printReceived: (object: unknown) => string;
15
35
  printExpected: (value: unknown) => string;
@@ -151,19 +171,19 @@ declare function fnNameFor(func: Function): string;
151
171
  declare function hasProperty(obj: object | null, property: string): boolean;
152
172
  declare function isImmutableUnorderedKeyed(maybeKeyed: any): boolean;
153
173
  declare function isImmutableUnorderedSet(maybeSet: any): boolean;
154
- declare const iterableEquality: (a: any, b: any, aStack?: Array<any>, bStack?: Array<any>) => boolean | undefined;
155
- declare const subsetEquality: (object: unknown, subset: unknown) => boolean | undefined;
156
- declare const typeEquality: (a: any, b: any) => boolean | undefined;
157
- declare const arrayBufferEquality: (a: unknown, b: unknown) => boolean | undefined;
158
- declare const sparseArrayEquality: (a: unknown, b: unknown) => boolean | undefined;
159
- declare const generateToBeMessage: (deepEqualityName: string, expected?: string, actual?: string) => string;
174
+ declare function iterableEquality(a: any, b: any, aStack?: Array<any>, bStack?: Array<any>): boolean | undefined;
175
+ declare function subsetEquality(object: unknown, subset: unknown): boolean | undefined;
176
+ declare function typeEquality(a: any, b: any): boolean | undefined;
177
+ declare function arrayBufferEquality(a: unknown, b: unknown): boolean | undefined;
178
+ declare function sparseArrayEquality(a: unknown, b: unknown): boolean | undefined;
179
+ declare function generateToBeMessage(deepEqualityName: string, expected?: string, actual?: string): string;
160
180
 
161
181
  declare const MATCHERS_OBJECT: unique symbol;
162
182
  declare const JEST_MATCHERS_OBJECT: unique symbol;
163
183
  declare const GLOBAL_EXPECT: unique symbol;
164
184
 
165
- declare const getState: <State extends MatcherState = MatcherState>(expect: Vi.ExpectStatic) => State;
166
- declare const setState: <State extends MatcherState = MatcherState>(state: Partial<State>, expect: Vi.ExpectStatic) => void;
185
+ declare function getState<State extends MatcherState = MatcherState>(expect: Vi.ExpectStatic): State;
186
+ declare function setState<State extends MatcherState = MatcherState>(state: Partial<State>, expect: Vi.ExpectStatic): void;
167
187
 
168
188
  declare const JestChaiExpect: ChaiPlugin;
169
189
 
package/dist/index.js CHANGED
@@ -22,13 +22,15 @@ if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
22
22
  })
23
23
  });
24
24
  }
25
- const getState = (expect) => globalThis[MATCHERS_OBJECT].get(expect);
26
- const setState = (state, expect) => {
25
+ function getState(expect) {
26
+ return globalThis[MATCHERS_OBJECT].get(expect);
27
+ }
28
+ function setState(state, expect) {
27
29
  const map = globalThis[MATCHERS_OBJECT];
28
30
  const current = map.get(expect) || {};
29
31
  Object.assign(current, state);
30
32
  map.set(expect, current);
31
- };
33
+ }
32
34
 
33
35
  function getMatcherUtils() {
34
36
  const c = () => getColors();
@@ -41,6 +43,7 @@ function getMatcherUtils() {
41
43
  const {
42
44
  comment = "",
43
45
  isDirectExpectCall = false,
46
+ // seems redundant with received === ''
44
47
  isNot = false,
45
48
  promise = "",
46
49
  secondArgument = "",
@@ -98,11 +101,7 @@ function getMatcherUtils() {
98
101
  };
99
102
  }
100
103
  function diff(a, b, options) {
101
- const c = getColors();
102
- return unifiedDiff(stringify(b), stringify(a), {
103
- colorDim: c.dim,
104
- colorSuccess: c.green,
105
- colorError: c.red,
104
+ return unifiedDiff(b, a, {
106
105
  showLegend: options == null ? void 0 : options.showLegend
107
106
  });
108
107
  }
@@ -258,8 +257,10 @@ function isImmutableUnorderedSet(maybeSet) {
258
257
  return !!(maybeSet && maybeSet[IS_SET_SENTINEL] && !maybeSet[IS_ORDERED_SENTINEL]);
259
258
  }
260
259
  const IteratorSymbol = Symbol.iterator;
261
- const hasIterator = (object) => !!(object != null && object[IteratorSymbol]);
262
- const iterableEquality = (a, b, aStack = [], bStack = []) => {
260
+ function hasIterator(object) {
261
+ return !!(object != null && object[IteratorSymbol]);
262
+ }
263
+ function iterableEquality(a, b, aStack = [], bStack = []) {
263
264
  if (typeof a !== "object" || typeof b !== "object" || Array.isArray(a) || Array.isArray(b) || !hasIterator(a) || !hasIterator(b))
264
265
  return void 0;
265
266
  if (a.constructor !== b.constructor)
@@ -334,15 +335,17 @@ const iterableEquality = (a, b, aStack = [], bStack = []) => {
334
335
  aStack.pop();
335
336
  bStack.pop();
336
337
  return true;
337
- };
338
- const hasPropertyInObject = (object, key) => {
338
+ }
339
+ function hasPropertyInObject(object, key) {
339
340
  const shouldTerminate = !object || typeof object !== "object" || object === Object.prototype;
340
341
  if (shouldTerminate)
341
342
  return false;
342
343
  return Object.prototype.hasOwnProperty.call(object, key) || hasPropertyInObject(Object.getPrototypeOf(object), key);
343
- };
344
- const isObjectWithKeys = (a) => isObject(a) && !(a instanceof Error) && !Array.isArray(a) && !(a instanceof Date);
345
- const subsetEquality = (object, subset) => {
344
+ }
345
+ function isObjectWithKeys(a) {
346
+ return isObject(a) && !(a instanceof Error) && !Array.isArray(a) && !(a instanceof Date);
347
+ }
348
+ function subsetEquality(object, subset) {
346
349
  const subsetEqualityWithContext = (seenReferences = /* @__PURE__ */ new WeakMap()) => (object2, subset2) => {
347
350
  if (!isObjectWithKeys(subset2))
348
351
  return void 0;
@@ -361,13 +364,13 @@ const subsetEquality = (object, subset) => {
361
364
  });
362
365
  };
363
366
  return subsetEqualityWithContext()(object, subset);
364
- };
365
- const typeEquality = (a, b) => {
367
+ }
368
+ function typeEquality(a, b) {
366
369
  if (a == null || b == null || a.constructor === b.constructor)
367
370
  return void 0;
368
371
  return false;
369
- };
370
- const arrayBufferEquality = (a, b) => {
372
+ }
373
+ function arrayBufferEquality(a, b) {
371
374
  if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer))
372
375
  return void 0;
373
376
  const dataViewA = new DataView(a);
@@ -379,15 +382,15 @@ const arrayBufferEquality = (a, b) => {
379
382
  return false;
380
383
  }
381
384
  return true;
382
- };
383
- const sparseArrayEquality = (a, b) => {
385
+ }
386
+ function sparseArrayEquality(a, b) {
384
387
  if (!Array.isArray(a) || !Array.isArray(b))
385
388
  return void 0;
386
389
  const aKeys = Object.keys(a);
387
390
  const bKeys = Object.keys(b);
388
391
  return equals(a, b, [iterableEquality, typeEquality], true) && equals(aKeys, bKeys);
389
- };
390
- const generateToBeMessage = (deepEqualityName, expected = "#{this}", actual = "#{exp}") => {
392
+ }
393
+ function generateToBeMessage(deepEqualityName, expected = "#{this}", actual = "#{exp}") {
391
394
  const toBeMessage = `expected ${expected} to be ${actual} // Object.is equality`;
392
395
  if (["toStrictEqual", "toEqual"].includes(deepEqualityName))
393
396
  return `${toBeMessage}
@@ -398,12 +401,13 @@ Expected: ${expected}
398
401
  Received: serializes to the same string
399
402
  `;
400
403
  return toBeMessage;
401
- };
404
+ }
402
405
 
403
406
  class AsymmetricMatcher {
404
407
  constructor(sample, inverse = false) {
405
408
  this.sample = sample;
406
409
  this.inverse = inverse;
410
+ // should have "jest" to be compatible with its ecosystem
407
411
  this.$$typeof = Symbol.for("jest.asymmetricMatcher");
408
412
  }
409
413
  getMatcherContext(expect) {
@@ -620,6 +624,20 @@ const JestAsymmetricMatchers = (chai, utils) => {
620
624
  };
621
625
  };
622
626
 
627
+ function recordAsyncExpect(test, promise) {
628
+ if (test) {
629
+ promise = promise.finally(() => {
630
+ const index = test.promises.indexOf(promise);
631
+ if (index !== -1)
632
+ test.promises.splice(index, 1);
633
+ });
634
+ if (!test.promises)
635
+ test.promises = [];
636
+ test.promises.push(promise);
637
+ }
638
+ return promise;
639
+ }
640
+
623
641
  const JestChaiExpect = (chai, utils) => {
624
642
  const c = () => getColors();
625
643
  function def(name, fn) {
@@ -1050,7 +1068,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1050
1068
  def(["toHaveBeenLastCalledWith", "lastCalledWith"], function(...args) {
1051
1069
  const spy = getSpy(this);
1052
1070
  const spyName = spy.getMockName();
1053
- const lastCall = spy.mock.calls[spy.calls.length - 1];
1071
+ const lastCall = spy.mock.calls[spy.mock.calls.length - 1];
1054
1072
  this.assert(
1055
1073
  equals(lastCall, args, [iterableEquality]),
1056
1074
  `expected last "${spyName}" call to have been called with #{exp}`,
@@ -1164,7 +1182,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1164
1182
  def(["toHaveLastReturnedWith", "lastReturnedWith"], function(value) {
1165
1183
  const spy = getSpy(this);
1166
1184
  const spyName = spy.getMockName();
1167
- const { value: lastResult } = spy.mock.results[spy.returns.length - 1];
1185
+ const { value: lastResult } = spy.mock.results[spy.mock.results.length - 1];
1168
1186
  const pass = equals(lastResult, value);
1169
1187
  this.assert(
1170
1188
  pass,
@@ -1197,6 +1215,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1197
1215
  utils.addProperty(chai.Assertion.prototype, "resolves", function __VITEST_RESOLVES__() {
1198
1216
  utils.flag(this, "promise", "resolves");
1199
1217
  utils.flag(this, "error", new Error("resolves"));
1218
+ const test = utils.flag(this, "vitest-test");
1200
1219
  const obj = utils.flag(this, "object");
1201
1220
  if (typeof (obj == null ? void 0 : obj.then) !== "function")
1202
1221
  throw new TypeError(`You must provide a Promise to expect() when using .resolves, not '${typeof obj}'.`);
@@ -1206,7 +1225,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1206
1225
  if (typeof result !== "function")
1207
1226
  return result instanceof chai.Assertion ? proxy : result;
1208
1227
  return async (...args) => {
1209
- return obj.then(
1228
+ const promise = obj.then(
1210
1229
  (value) => {
1211
1230
  utils.flag(this, "object", value);
1212
1231
  return result.call(this, ...args);
@@ -1215,6 +1234,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1215
1234
  throw new Error(`promise rejected "${String(err)}" instead of resolving`);
1216
1235
  }
1217
1236
  );
1237
+ return recordAsyncExpect(test, promise);
1218
1238
  };
1219
1239
  }
1220
1240
  });
@@ -1223,6 +1243,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1223
1243
  utils.addProperty(chai.Assertion.prototype, "rejects", function __VITEST_REJECTS__() {
1224
1244
  utils.flag(this, "promise", "rejects");
1225
1245
  utils.flag(this, "error", new Error("rejects"));
1246
+ const test = utils.flag(this, "vitest-test");
1226
1247
  const obj = utils.flag(this, "object");
1227
1248
  const wrapper = typeof obj === "function" ? obj() : obj;
1228
1249
  if (typeof (wrapper == null ? void 0 : wrapper.then) !== "function")
@@ -1233,7 +1254,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1233
1254
  if (typeof result !== "function")
1234
1255
  return result instanceof chai.Assertion ? proxy : result;
1235
1256
  return async (...args) => {
1236
- return wrapper.then(
1257
+ const promise = wrapper.then(
1237
1258
  (value) => {
1238
1259
  throw new Error(`promise resolved "${String(value)}" instead of rejecting`);
1239
1260
  },
@@ -1242,6 +1263,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1242
1263
  return result.call(this, ...args);
1243
1264
  }
1244
1265
  );
1266
+ return recordAsyncExpect(test, promise);
1245
1267
  };
1246
1268
  }
1247
1269
  });
@@ -1249,7 +1271,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1249
1271
  });
1250
1272
  };
1251
1273
 
1252
- const getMatcherState = (assertion, expect) => {
1274
+ function getMatcherState(assertion, expect) {
1253
1275
  const obj = assertion._obj;
1254
1276
  const isNot = util.flag(assertion, "negate");
1255
1277
  const promise = util.flag(assertion, "promise") || "";
@@ -1266,6 +1288,7 @@ const getMatcherState = (assertion, expect) => {
1266
1288
  utils: jestUtils,
1267
1289
  promise,
1268
1290
  equals,
1291
+ // needed for built-in jest-snapshots, but we don't use it
1269
1292
  suppressedErrors: []
1270
1293
  };
1271
1294
  return {
@@ -1273,7 +1296,7 @@ const getMatcherState = (assertion, expect) => {
1273
1296
  isNot,
1274
1297
  obj
1275
1298
  };
1276
- };
1299
+ }
1277
1300
  class JestExtendError extends Error {
1278
1301
  constructor(message, actual, expected) {
1279
1302
  super(message);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/expect",
3
3
  "type": "module",
4
- "version": "0.29.8",
4
+ "version": "0.30.1",
5
5
  "description": "Jest's expect matchers as a Chai plugin",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -25,8 +25,8 @@
25
25
  ],
26
26
  "dependencies": {
27
27
  "chai": "^4.3.7",
28
- "@vitest/utils": "0.29.8",
29
- "@vitest/spy": "0.29.8"
28
+ "@vitest/spy": "0.30.1",
29
+ "@vitest/utils": "0.30.1"
30
30
  },
31
31
  "devDependencies": {
32
32
  "picocolors": "^1.0.0"