@vitest/expect 0.34.3 → 0.34.5

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.
Files changed (2) hide show
  1. package/dist/index.js +41 -26
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { getColors, stringify, isObject, assertTypes } from '@vitest/utils';
2
2
  export { setupColors } from '@vitest/utils';
3
3
  import { diff } from '@vitest/utils/diff';
4
- import { AssertionError, util } from 'chai';
5
4
  import { isMockFunction } from '@vitest/spy';
6
5
  import { processError } from '@vitest/utils/error';
6
+ import { util } from 'chai';
7
7
 
8
8
  const MATCHERS_OBJECT = Symbol.for("matchers-object");
9
9
  const JEST_MATCHERS_OBJECT = Symbol.for("$$jest-matchers-object");
@@ -375,8 +375,12 @@ function arrayBufferEquality(a, b) {
375
375
  if (!(a instanceof DataView && b instanceof DataView)) {
376
376
  if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer))
377
377
  return void 0;
378
- dataViewA = new DataView(a);
379
- dataViewB = new DataView(b);
378
+ try {
379
+ dataViewA = new DataView(a);
380
+ dataViewB = new DataView(b);
381
+ } catch {
382
+ return void 0;
383
+ }
380
384
  }
381
385
  if (dataViewA.byteLength !== dataViewB.byteLength)
382
386
  return false;
@@ -661,6 +665,7 @@ function wrapSoft(utils, fn) {
661
665
  }
662
666
 
663
667
  const JestChaiExpect = (chai, utils) => {
668
+ const { AssertionError } = chai;
664
669
  const c = () => getColors();
665
670
  function def(name, fn) {
666
671
  const addMethod = (n) => {
@@ -1063,18 +1068,15 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1063
1068
  );
1064
1069
  if (called && isNot)
1065
1070
  msg = formatCalls(spy, msg);
1066
- if (called && isNot || !called && !isNot) {
1067
- const err = new Error(msg);
1068
- err.name = "AssertionError";
1069
- throw err;
1070
- }
1071
+ if (called && isNot || !called && !isNot)
1072
+ throw new AssertionError(msg);
1071
1073
  });
1072
1074
  def(["toHaveBeenCalledWith", "toBeCalledWith"], function(...args) {
1073
1075
  const spy = getSpy(this);
1074
1076
  const spyName = spy.getMockName();
1075
1077
  const pass = spy.mock.calls.some((callArg) => equals(callArg, args, [iterableEquality]));
1076
1078
  const isNot = utils.flag(this, "negate");
1077
- let msg = utils.getMessage(
1079
+ const msg = utils.getMessage(
1078
1080
  this,
1079
1081
  [
1080
1082
  pass,
@@ -1083,12 +1085,8 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1083
1085
  args
1084
1086
  ]
1085
1087
  );
1086
- if (pass && isNot || !pass && !isNot) {
1087
- msg = formatCalls(spy, msg, args);
1088
- const err = new Error(msg);
1089
- err.name = "AssertionError";
1090
- throw err;
1091
- }
1088
+ if (pass && isNot || !pass && !isNot)
1089
+ throw new AssertionError(formatCalls(spy, msg, args));
1092
1090
  });
1093
1091
  def(["toHaveBeenNthCalledWith", "nthCalledWith"], function(times, ...args) {
1094
1092
  const spy = getSpy(this);
@@ -1134,11 +1132,20 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1134
1132
  return;
1135
1133
  }
1136
1134
  } else {
1135
+ let isThrow = false;
1137
1136
  try {
1138
1137
  obj();
1139
1138
  } catch (err) {
1139
+ isThrow = true;
1140
1140
  thrown = err;
1141
1141
  }
1142
+ if (!isThrow && !isNot) {
1143
+ const message = utils.flag(this, "message") || "expected function to throw an error, but it didn't";
1144
+ const error = {
1145
+ showDiff: false
1146
+ };
1147
+ throw new AssertionError(message, error, utils.flag(this, "ssfi"));
1148
+ }
1142
1149
  }
1143
1150
  if (typeof expected === "function") {
1144
1151
  const name = expected.name || expected.prototype.constructor.name;
@@ -1204,7 +1211,7 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1204
1211
  const spyName = spy.getMockName();
1205
1212
  const pass = spy.mock.results.some(({ type, value: result }) => type === "return" && equals(value, result));
1206
1213
  const isNot = utils.flag(this, "negate");
1207
- let msg = utils.getMessage(
1214
+ const msg = utils.getMessage(
1208
1215
  this,
1209
1216
  [
1210
1217
  pass,
@@ -1213,12 +1220,8 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1213
1220
  value
1214
1221
  ]
1215
1222
  );
1216
- if (pass && isNot || !pass && !isNot) {
1217
- msg = formatReturns(spy, msg, value);
1218
- const err = new Error(msg);
1219
- err.name = "AssertionError";
1220
- throw err;
1221
- }
1223
+ if (pass && isNot || !pass && !isNot)
1224
+ throw new AssertionError(formatReturns(spy, msg, value));
1222
1225
  });
1223
1226
  def(["toHaveLastReturnedWith", "lastReturnedWith"], function(value) {
1224
1227
  const spy = getSpy(this);
@@ -1254,8 +1257,9 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1254
1257
  return this.be.satisfy(matcher, message);
1255
1258
  });
1256
1259
  utils.addProperty(chai.Assertion.prototype, "resolves", function __VITEST_RESOLVES__() {
1260
+ const error = new Error("resolves");
1257
1261
  utils.flag(this, "promise", "resolves");
1258
- utils.flag(this, "error", new Error("resolves"));
1262
+ utils.flag(this, "error", error);
1259
1263
  const test = utils.flag(this, "vitest-test");
1260
1264
  const obj = utils.flag(this, "object");
1261
1265
  if (typeof (obj == null ? void 0 : obj.then) !== "function")
@@ -1272,7 +1276,12 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1272
1276
  return result.call(this, ...args);
1273
1277
  },
1274
1278
  (err) => {
1275
- throw new Error(`promise rejected "${String(err)}" instead of resolving`);
1279
+ const _error = new AssertionError(
1280
+ `promise rejected "${utils.inspect(err)}" instead of resolving`,
1281
+ { showDiff: false }
1282
+ );
1283
+ _error.stack = error.stack.replace(error.message, _error.message);
1284
+ throw _error;
1276
1285
  }
1277
1286
  );
1278
1287
  return recordAsyncExpect(test, promise);
@@ -1282,8 +1291,9 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1282
1291
  return proxy;
1283
1292
  });
1284
1293
  utils.addProperty(chai.Assertion.prototype, "rejects", function __VITEST_REJECTS__() {
1294
+ const error = new Error("rejects");
1285
1295
  utils.flag(this, "promise", "rejects");
1286
- utils.flag(this, "error", new Error("rejects"));
1296
+ utils.flag(this, "error", error);
1287
1297
  const test = utils.flag(this, "vitest-test");
1288
1298
  const obj = utils.flag(this, "object");
1289
1299
  const wrapper = typeof obj === "function" ? obj() : obj;
@@ -1297,7 +1307,12 @@ Number of calls: ${c().bold(spy.mock.calls.length)}
1297
1307
  return async (...args) => {
1298
1308
  const promise = wrapper.then(
1299
1309
  (value) => {
1300
- throw new Error(`promise resolved "${String(value)}" instead of rejecting`);
1310
+ const _error = new AssertionError(
1311
+ `promise resolved "${utils.inspect(value)}" instead of rejecting`,
1312
+ { showDiff: false }
1313
+ );
1314
+ _error.stack = error.stack.replace(error.message, _error.message);
1315
+ throw _error;
1301
1316
  },
1302
1317
  (err) => {
1303
1318
  utils.flag(this, "object", err);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/expect",
3
3
  "type": "module",
4
- "version": "0.34.3",
4
+ "version": "0.34.5",
5
5
  "description": "Jest's expect matchers as a Chai plugin",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -30,12 +30,12 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "chai": "^4.3.7",
33
- "@vitest/utils": "0.34.3",
34
- "@vitest/spy": "0.34.3"
33
+ "@vitest/utils": "0.34.5",
34
+ "@vitest/spy": "0.34.5"
35
35
  },
36
36
  "devDependencies": {
37
37
  "picocolors": "^1.0.0",
38
- "@vitest/runner": "0.34.3"
38
+ "@vitest/runner": "0.34.5"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "rimraf dist && rollup -c",