@vitest/expect 0.31.3 → 0.32.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
@@ -91,6 +91,7 @@ interface MatcherState {
91
91
  iterableEquality: Tester;
92
92
  subsetEquality: Tester;
93
93
  };
94
+ soft?: boolean;
94
95
  }
95
96
  interface SyncExpectationResult {
96
97
  pass: boolean;
@@ -106,6 +107,7 @@ interface RawMatcherFn<T extends MatcherState = MatcherState> {
106
107
  type MatchersObject<T extends MatcherState = MatcherState> = Record<string, RawMatcherFn<T>>;
107
108
  interface ExpectStatic extends Chai.ExpectStatic, AsymmetricMatchersContaining {
108
109
  <T>(actual: T, message?: string): Assertion<T>;
110
+ soft<T>(actual: T, message?: string): Assertion<T>;
109
111
  extend(expects: MatchersObject): void;
110
112
  assertions(expected: number): void;
111
113
  hasAssertions(): void;
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export { setupColors } from '@vitest/utils';
3
3
  import { unifiedDiff } from '@vitest/utils/diff';
4
4
  import { AssertionError, util } from 'chai';
5
5
  import { isMockFunction } from '@vitest/spy';
6
+ import { processError } from '@vitest/utils/error';
6
7
 
7
8
  const MATCHERS_OBJECT = Symbol.for("matchers-object");
8
9
  const JEST_MATCHERS_OBJECT = Symbol.for("$$jest-matchers-object");
@@ -637,13 +638,33 @@ function recordAsyncExpect(test, promise) {
637
638
  }
638
639
  return promise;
639
640
  }
641
+ function wrapSoft(utils, fn) {
642
+ return function(...args) {
643
+ var _a;
644
+ const test = utils.flag(this, "vitest-test");
645
+ const state = (test == null ? void 0 : test.context._local) ? test.context.expect.getState() : getState(globalThis[GLOBAL_EXPECT]);
646
+ if (!state.soft)
647
+ return fn.apply(this, args);
648
+ if (!test)
649
+ throw new Error("expect.soft() can only be used inside a test");
650
+ try {
651
+ return fn.apply(this, args);
652
+ } catch (err) {
653
+ test.result || (test.result = { state: "fail" });
654
+ test.result.state = "fail";
655
+ (_a = test.result).errors || (_a.errors = []);
656
+ test.result.errors.push(processError(err));
657
+ }
658
+ };
659
+ }
640
660
 
641
661
  const JestChaiExpect = (chai, utils) => {
642
662
  const c = () => getColors();
643
663
  function def(name, fn) {
644
664
  const addMethod = (n) => {
645
- utils.addMethod(chai.Assertion.prototype, n, fn);
646
- utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, n, fn);
665
+ const softWrapper = wrapSoft(utils, fn);
666
+ utils.addMethod(chai.Assertion.prototype, n, softWrapper);
667
+ utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, n, softWrapper);
647
668
  };
648
669
  if (Array.isArray(name))
649
670
  name.forEach((n) => addMethod(n));
@@ -1322,8 +1343,9 @@ function JestExtendPlugin(expect, matchers) {
1322
1343
  if (pass && isNot || !pass && !isNot)
1323
1344
  throw new JestExtendError(message(), actual, expected);
1324
1345
  }
1325
- utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, expectAssertionName, expectWrapper);
1326
- utils.addMethod(c.Assertion.prototype, expectAssertionName, expectWrapper);
1346
+ const softWrapper = wrapSoft(utils, expectWrapper);
1347
+ utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, expectAssertionName, softWrapper);
1348
+ utils.addMethod(c.Assertion.prototype, expectAssertionName, softWrapper);
1327
1349
  class CustomMatcher extends AsymmetricMatcher {
1328
1350
  constructor(inverse = false, ...sample) {
1329
1351
  super(sample, inverse);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/expect",
3
3
  "type": "module",
4
- "version": "0.31.3",
4
+ "version": "0.32.0",
5
5
  "description": "Jest's expect matchers as a Chai plugin",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -30,11 +30,12 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "chai": "^4.3.7",
33
- "@vitest/utils": "0.31.3",
34
- "@vitest/spy": "0.31.3"
33
+ "@vitest/utils": "0.32.0",
34
+ "@vitest/spy": "0.32.0"
35
35
  },
36
36
  "devDependencies": {
37
- "picocolors": "^1.0.0"
37
+ "picocolors": "^1.0.0",
38
+ "@vitest/runner": "0.32.0"
38
39
  },
39
40
  "scripts": {
40
41
  "build": "rimraf dist && rollup -c",