@vitest/expect 1.0.0-beta.0 → 1.0.0-beta.2

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
@@ -110,6 +110,7 @@ interface AsymmetricMatchersContaining {
110
110
  objectContaining<T = any>(expected: T): any;
111
111
  arrayContaining<T = unknown>(expected: Array<T>): any;
112
112
  stringMatching(expected: string | RegExp): any;
113
+ closeTo(expected: number, precision?: number): any;
113
114
  }
114
115
  interface JestAssertion<T = any> extends jest.Matchers<void, T> {
115
116
  toEqual<E>(expected: E): void;
@@ -248,6 +249,7 @@ declare function typeEquality(a: any, b: any): boolean | undefined;
248
249
  declare function arrayBufferEquality(a: unknown, b: unknown): boolean | undefined;
249
250
  declare function sparseArrayEquality(a: unknown, b: unknown): boolean | undefined;
250
251
  declare function generateToBeMessage(deepEqualityName: string, expected?: string, actual?: string): string;
252
+ declare function pluralize(word: string, count: number): string;
251
253
 
252
254
  declare const MATCHERS_OBJECT: unique symbol;
253
255
  declare const JEST_MATCHERS_OBJECT: unique symbol;
@@ -260,4 +262,4 @@ declare const JestChaiExpect: ChaiPlugin;
260
262
 
261
263
  declare const JestExtend: ChaiPlugin;
262
264
 
263
- export { Any, Anything, ArrayContaining, Assertion, AsymmetricMatcher, AsymmetricMatcherInterface, AsymmetricMatchersContaining, AsyncExpectationResult, ChaiPlugin, ExpectStatic, ExpectationResult, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, JestAssertion, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, MatcherHintOptions, MatcherState, MatchersObject, ObjectContaining, RawMatcherFn, StringContaining, StringMatching, SyncExpectationResult, Tester, arrayBufferEquality, equals, fnNameFor, generateToBeMessage, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, setState, sparseArrayEquality, subsetEquality, typeEquality };
265
+ export { 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, arrayBufferEquality, equals, fnNameFor, generateToBeMessage, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, pluralize, setState, sparseArrayEquality, subsetEquality, typeEquality };
package/dist/index.js CHANGED
@@ -409,6 +409,9 @@ Received: serializes to the same string
409
409
  `;
410
410
  return toBeMessage;
411
411
  }
412
+ function pluralize(word, count) {
413
+ return `${count} ${word}${count === 1 ? "" : "s"}`;
414
+ }
412
415
 
413
416
  class AsymmetricMatcher {
414
417
  constructor(sample, inverse = false) {
@@ -592,6 +595,44 @@ class StringMatching extends AsymmetricMatcher {
592
595
  return "string";
593
596
  }
594
597
  }
598
+ class CloseTo extends AsymmetricMatcher {
599
+ precision;
600
+ constructor(sample, precision = 2, inverse = false) {
601
+ if (!isA("Number", sample))
602
+ throw new Error("Expected is not a Number");
603
+ if (!isA("Number", precision))
604
+ throw new Error("Precision is not a Number");
605
+ super(sample);
606
+ this.inverse = inverse;
607
+ this.precision = precision;
608
+ }
609
+ asymmetricMatch(other) {
610
+ if (!isA("Number", other))
611
+ return false;
612
+ let result = false;
613
+ if (other === Number.POSITIVE_INFINITY && this.sample === Number.POSITIVE_INFINITY) {
614
+ result = true;
615
+ } else if (other === Number.NEGATIVE_INFINITY && this.sample === Number.NEGATIVE_INFINITY) {
616
+ result = true;
617
+ } else {
618
+ result = Math.abs(this.sample - other) < 10 ** -this.precision / 2;
619
+ }
620
+ return this.inverse ? !result : result;
621
+ }
622
+ toString() {
623
+ return `Number${this.inverse ? "Not" : ""}CloseTo`;
624
+ }
625
+ getExpectedType() {
626
+ return "number";
627
+ }
628
+ toAsymmetricMatcher() {
629
+ return [
630
+ this.toString(),
631
+ this.sample,
632
+ `(${pluralize("digit", this.precision)})`
633
+ ].join(" ");
634
+ }
635
+ }
595
636
  const JestAsymmetricMatchers = (chai, utils) => {
596
637
  utils.addMethod(
597
638
  chai.expect,
@@ -623,11 +664,17 @@ const JestAsymmetricMatchers = (chai, utils) => {
623
664
  "stringMatching",
624
665
  (expected) => new StringMatching(expected)
625
666
  );
667
+ utils.addMethod(
668
+ chai.expect,
669
+ "closeTo",
670
+ (expected, precision) => new CloseTo(expected, precision)
671
+ );
626
672
  chai.expect.not = {
627
673
  stringContaining: (expected) => new StringContaining(expected, true),
628
674
  objectContaining: (expected) => new ObjectContaining(expected, true),
629
675
  arrayContaining: (expected) => new ArrayContaining(expected, true),
630
- stringMatching: (expected) => new StringMatching(expected, true)
676
+ stringMatching: (expected) => new StringMatching(expected, true),
677
+ closeTo: (expected, precision) => new CloseTo(expected, precision, true)
631
678
  };
632
679
  };
633
680
 
@@ -1422,4 +1469,4 @@ const JestExtend = (chai, utils) => {
1422
1469
  });
1423
1470
  };
1424
1471
 
1425
- export { Any, Anything, ArrayContaining, AsymmetricMatcher, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, ObjectContaining, StringContaining, StringMatching, arrayBufferEquality, equals, fnNameFor, generateToBeMessage, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, setState, sparseArrayEquality, subsetEquality, typeEquality };
1472
+ export { Any, Anything, ArrayContaining, AsymmetricMatcher, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, ObjectContaining, StringContaining, StringMatching, arrayBufferEquality, equals, fnNameFor, generateToBeMessage, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, pluralize, setState, sparseArrayEquality, subsetEquality, typeEquality };
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import './dist/chai'
2
+
3
+ export * from './dist/index'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/expect",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.0",
4
+ "version": "1.0.0-beta.2",
5
5
  "description": "Jest's expect matchers as a Chai plugin",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -26,18 +26,19 @@
26
26
  "module": "./dist/index.js",
27
27
  "types": "./index.d.ts",
28
28
  "files": [
29
- "dist"
29
+ "dist",
30
+ "*.d.ts"
30
31
  ],
31
32
  "dependencies": {
32
33
  "chai": "^4.3.10",
33
- "@vitest/utils": "1.0.0-beta.0",
34
- "@vitest/spy": "1.0.0-beta.0"
34
+ "@vitest/spy": "1.0.0-beta.2",
35
+ "@vitest/utils": "1.0.0-beta.2"
35
36
  },
36
37
  "devDependencies": {
37
- "@types/chai": "^4.3.6",
38
+ "@types/chai": "4.3.6",
38
39
  "picocolors": "^1.0.0",
39
40
  "rollup-plugin-copy": "^3.5.0",
40
- "@vitest/runner": "1.0.0-beta.0"
41
+ "@vitest/runner": "1.0.0-beta.2"
41
42
  },
42
43
  "scripts": {
43
44
  "build": "rimraf dist && rollup -c",