@vitest/expect
Advanced tools
Comparing version 1.0.0-beta.1 to 1.0.0-beta.2
@@ -412,2 +412,5 @@ import { getColors, stringify, isObject, assertTypes } from '@vitest/utils'; | ||
} | ||
function pluralize(word, count) { | ||
return `${count} ${word}${count === 1 ? "" : "s"}`; | ||
} | ||
@@ -596,2 +599,40 @@ class AsymmetricMatcher { | ||
} | ||
class CloseTo extends AsymmetricMatcher { | ||
precision; | ||
constructor(sample, precision = 2, inverse = false) { | ||
if (!isA("Number", sample)) | ||
throw new Error("Expected is not a Number"); | ||
if (!isA("Number", precision)) | ||
throw new Error("Precision is not a Number"); | ||
super(sample); | ||
this.inverse = inverse; | ||
this.precision = precision; | ||
} | ||
asymmetricMatch(other) { | ||
if (!isA("Number", other)) | ||
return false; | ||
let result = false; | ||
if (other === Number.POSITIVE_INFINITY && this.sample === Number.POSITIVE_INFINITY) { | ||
result = true; | ||
} else if (other === Number.NEGATIVE_INFINITY && this.sample === Number.NEGATIVE_INFINITY) { | ||
result = true; | ||
} else { | ||
result = Math.abs(this.sample - other) < 10 ** -this.precision / 2; | ||
} | ||
return this.inverse ? !result : result; | ||
} | ||
toString() { | ||
return `Number${this.inverse ? "Not" : ""}CloseTo`; | ||
} | ||
getExpectedType() { | ||
return "number"; | ||
} | ||
toAsymmetricMatcher() { | ||
return [ | ||
this.toString(), | ||
this.sample, | ||
`(${pluralize("digit", this.precision)})` | ||
].join(" "); | ||
} | ||
} | ||
const JestAsymmetricMatchers = (chai, utils) => { | ||
@@ -628,2 +669,7 @@ utils.addMethod( | ||
); | ||
utils.addMethod( | ||
chai.expect, | ||
"closeTo", | ||
(expected, precision) => new CloseTo(expected, precision) | ||
); | ||
chai.expect.not = { | ||
@@ -633,3 +679,4 @@ stringContaining: (expected) => new StringContaining(expected, true), | ||
arrayContaining: (expected) => new ArrayContaining(expected, true), | ||
stringMatching: (expected) => new StringMatching(expected, true) | ||
stringMatching: (expected) => new StringMatching(expected, true), | ||
closeTo: (expected, precision) => new CloseTo(expected, precision, true) | ||
}; | ||
@@ -1429,2 +1476,2 @@ }; | ||
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 }; | ||
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 }; |
@@ -113,2 +113,3 @@ import { stringify, Constructable } from '@vitest/utils'; | ||
stringMatching(expected: string | RegExp): any; | ||
closeTo(expected: number, precision?: number): any; | ||
} | ||
@@ -252,2 +253,3 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T> { | ||
declare function generateToBeMessage(deepEqualityName: string, expected?: string, actual?: string): string; | ||
declare function pluralize(word: string, count: number): string; | ||
@@ -265,2 +267,2 @@ declare const MATCHERS_OBJECT: unique symbol; | ||
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 }; | ||
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 }; |
@@ -412,2 +412,5 @@ import { getColors, stringify, isObject, assertTypes } from '@vitest/utils'; | ||
} | ||
function pluralize(word, count) { | ||
return `${count} ${word}${count === 1 ? "" : "s"}`; | ||
} | ||
@@ -596,2 +599,40 @@ class AsymmetricMatcher { | ||
} | ||
class CloseTo extends AsymmetricMatcher { | ||
precision; | ||
constructor(sample, precision = 2, inverse = false) { | ||
if (!isA("Number", sample)) | ||
throw new Error("Expected is not a Number"); | ||
if (!isA("Number", precision)) | ||
throw new Error("Precision is not a Number"); | ||
super(sample); | ||
this.inverse = inverse; | ||
this.precision = precision; | ||
} | ||
asymmetricMatch(other) { | ||
if (!isA("Number", other)) | ||
return false; | ||
let result = false; | ||
if (other === Number.POSITIVE_INFINITY && this.sample === Number.POSITIVE_INFINITY) { | ||
result = true; | ||
} else if (other === Number.NEGATIVE_INFINITY && this.sample === Number.NEGATIVE_INFINITY) { | ||
result = true; | ||
} else { | ||
result = Math.abs(this.sample - other) < 10 ** -this.precision / 2; | ||
} | ||
return this.inverse ? !result : result; | ||
} | ||
toString() { | ||
return `Number${this.inverse ? "Not" : ""}CloseTo`; | ||
} | ||
getExpectedType() { | ||
return "number"; | ||
} | ||
toAsymmetricMatcher() { | ||
return [ | ||
this.toString(), | ||
this.sample, | ||
`(${pluralize("digit", this.precision)})` | ||
].join(" "); | ||
} | ||
} | ||
const JestAsymmetricMatchers = (chai, utils) => { | ||
@@ -628,2 +669,7 @@ utils.addMethod( | ||
); | ||
utils.addMethod( | ||
chai.expect, | ||
"closeTo", | ||
(expected, precision) => new CloseTo(expected, precision) | ||
); | ||
chai.expect.not = { | ||
@@ -633,3 +679,4 @@ stringContaining: (expected) => new StringContaining(expected, true), | ||
arrayContaining: (expected) => new ArrayContaining(expected, true), | ||
stringMatching: (expected) => new StringMatching(expected, true) | ||
stringMatching: (expected) => new StringMatching(expected, true), | ||
closeTo: (expected, precision) => new CloseTo(expected, precision, true) | ||
}; | ||
@@ -1429,2 +1476,2 @@ }; | ||
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 }; | ||
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 }; |
{ | ||
"name": "@vitest/expect", | ||
"type": "module", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0-beta.2", | ||
"description": "Jest's expect matchers as a Chai plugin", | ||
@@ -34,10 +34,10 @@ "license": "MIT", | ||
"chai": "^4.3.10", | ||
"@vitest/utils": "1.0.0-beta.1", | ||
"@vitest/spy": "1.0.0-beta.1" | ||
"@vitest/spy": "1.0.0-beta.2", | ||
"@vitest/utils": "1.0.0-beta.2" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.6", | ||
"@types/chai": "4.3.6", | ||
"picocolors": "^1.0.0", | ||
"rollup-plugin-copy": "^3.5.0", | ||
"@vitest/runner": "1.0.0-beta.1" | ||
"@vitest/runner": "1.0.0-beta.2" | ||
}, | ||
@@ -44,0 +44,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
182690
4936
+ Added@vitest/spy@1.0.0-beta.2(transitive)
+ Added@vitest/utils@1.0.0-beta.2(transitive)
- Removed@vitest/spy@1.0.0-beta.1(transitive)
- Removed@vitest/utils@1.0.0-beta.1(transitive)
Updated@vitest/spy@1.0.0-beta.2
Updated@vitest/utils@1.0.0-beta.2