@vitest/expect 4.1.2 → 4.1.4
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 +8 -3
- package/dist/index.js +17 -8
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Test } from '@vitest/runner';
|
|
2
1
|
import { MockInstance } from '@vitest/spy';
|
|
3
2
|
import { Formatter } from 'tinyrainbow';
|
|
4
3
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
@@ -157,7 +156,12 @@ interface MatcherState {
|
|
|
157
156
|
};
|
|
158
157
|
soft?: boolean;
|
|
159
158
|
poll?: boolean;
|
|
160
|
-
|
|
159
|
+
/**
|
|
160
|
+
* The same assertion instance that chai plugins receive.
|
|
161
|
+
* @experimental
|
|
162
|
+
* @see {@link https://www.chaijs.com/guide/plugins/} Core Plugin Concepts
|
|
163
|
+
*/
|
|
164
|
+
readonly assertion: Assertion;
|
|
161
165
|
}
|
|
162
166
|
interface SyncExpectationResult {
|
|
163
167
|
pass: boolean;
|
|
@@ -917,8 +921,9 @@ declare function isStandardSchema(obj: any): obj is StandardSchemaV1;
|
|
|
917
921
|
declare function getState<State extends MatcherState = MatcherState>(expect: ExpectStatic): State;
|
|
918
922
|
declare function setState<State extends MatcherState = MatcherState>(state: Partial<State>, expect: ExpectStatic): void;
|
|
919
923
|
|
|
920
|
-
declare function createAssertionMessage(util: Chai.ChaiUtils, assertion: Assertion, hasArgs: boolean): string;
|
|
924
|
+
declare function createAssertionMessage(util: Chai.ChaiUtils, assertion: Chai.Assertion, hasArgs: boolean): string;
|
|
921
925
|
declare function recordAsyncExpect(_test: any, promise: Promise<any>, assertion: string, error: Error, isSoft?: boolean): Promise<any>;
|
|
926
|
+
/** wrap assertion function to support `expect.soft` and provide assertion name as `_name` */
|
|
922
927
|
declare function wrapAssertion(utils: Chai.ChaiUtils, name: string, fn: (this: Chai.AssertionStatic & Assertion, ...args: any[]) => void | PromiseLike<void>): (this: Chai.AssertionStatic & Assertion, ...args: any[]) => void | PromiseLike<void>;
|
|
923
928
|
|
|
924
929
|
export { ASYMMETRIC_MATCHERS_OBJECT, Any, Anything, ArrayContaining, AsymmetricMatcher, ChaiStyleAssertions, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, ObjectContaining, SchemaMatching, StringContaining, StringMatching, addCustomEqualityTesters, arrayBufferEquality, createAssertionMessage, customMatchers, equals, fnNameFor, generateToBeMessage, getObjectKeys, getObjectSubset, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isError, isImmutableUnorderedKeyed, isImmutableUnorderedSet, isStandardSchema, iterableEquality, pluralize, recordAsyncExpect, setState, sparseArrayEquality, subsetEquality, typeEquality, wrapAssertion };
|
package/dist/index.js
CHANGED
|
@@ -1142,6 +1142,7 @@ function handleTestError(test, err) {
|
|
|
1142
1142
|
test.result.errors ||= [];
|
|
1143
1143
|
test.result.errors.push(processError(err));
|
|
1144
1144
|
}
|
|
1145
|
+
/** wrap assertion function to support `expect.soft` and provide assertion name as `_name` */
|
|
1145
1146
|
function wrapAssertion(utils, name, fn) {
|
|
1146
1147
|
return function(...args) {
|
|
1147
1148
|
// private
|
|
@@ -1836,7 +1837,6 @@ function getMatcherState(assertion, expect) {
|
|
|
1836
1837
|
}
|
|
1837
1838
|
const matcherState = {
|
|
1838
1839
|
...getState(expect),
|
|
1839
|
-
task,
|
|
1840
1840
|
currentTestName,
|
|
1841
1841
|
customTesters: getCustomEqualityTesters(),
|
|
1842
1842
|
isNot,
|
|
@@ -1845,8 +1845,10 @@ function getMatcherState(assertion, expect) {
|
|
|
1845
1845
|
equals,
|
|
1846
1846
|
suppressedErrors: [],
|
|
1847
1847
|
soft: util.flag(assertion, "soft"),
|
|
1848
|
-
poll: util.flag(assertion, "poll")
|
|
1848
|
+
poll: util.flag(assertion, "poll"),
|
|
1849
|
+
assertion
|
|
1849
1850
|
};
|
|
1851
|
+
Object.assign(matcherState, { task });
|
|
1850
1852
|
return {
|
|
1851
1853
|
state: matcherState,
|
|
1852
1854
|
isNot,
|
|
@@ -1855,24 +1857,24 @@ function getMatcherState(assertion, expect) {
|
|
|
1855
1857
|
};
|
|
1856
1858
|
}
|
|
1857
1859
|
class JestExtendError extends Error {
|
|
1858
|
-
constructor(message, actual, expected,
|
|
1860
|
+
constructor(message, actual, expected, __vitest_error_context__) {
|
|
1859
1861
|
super(message);
|
|
1860
1862
|
this.actual = actual;
|
|
1861
1863
|
this.expected = expected;
|
|
1862
|
-
this.
|
|
1864
|
+
this.__vitest_error_context__ = __vitest_error_context__;
|
|
1863
1865
|
}
|
|
1864
1866
|
}
|
|
1865
1867
|
function JestExtendPlugin(c, expect, matchers) {
|
|
1866
1868
|
return (_, utils) => {
|
|
1867
1869
|
Object.entries(matchers).forEach(([expectAssertionName, expectAssertion]) => {
|
|
1868
|
-
function
|
|
1870
|
+
function __VITEST_EXTEND_ASSERTION__(...args) {
|
|
1869
1871
|
const { state, isNot, obj, customMessage } = getMatcherState(this, expect);
|
|
1870
1872
|
const result = expectAssertion.call(state, obj, ...args);
|
|
1871
1873
|
if (result && typeof result === "object" && typeof result.then === "function") {
|
|
1872
1874
|
const thenable = result;
|
|
1873
1875
|
return thenable.then(({ pass, message, actual, expected, meta }) => {
|
|
1874
1876
|
if (pass && isNot || !pass && !isNot) {
|
|
1875
|
-
const errorMessage = customMessage
|
|
1877
|
+
const errorMessage = (customMessage ? `${customMessage}: ` : "") + message();
|
|
1876
1878
|
throw new JestExtendError(errorMessage, actual, expected, {
|
|
1877
1879
|
assertionName: expectAssertionName,
|
|
1878
1880
|
meta
|
|
@@ -1882,16 +1884,23 @@ function JestExtendPlugin(c, expect, matchers) {
|
|
|
1882
1884
|
}
|
|
1883
1885
|
const { pass, message, actual, expected, meta } = result;
|
|
1884
1886
|
if (pass && isNot || !pass && !isNot) {
|
|
1885
|
-
const errorMessage = customMessage
|
|
1887
|
+
const errorMessage = (customMessage ? `${customMessage}: ` : "") + message();
|
|
1886
1888
|
throw new JestExtendError(errorMessage, actual, expected, {
|
|
1887
1889
|
assertionName: expectAssertionName,
|
|
1888
1890
|
meta
|
|
1889
1891
|
});
|
|
1890
1892
|
}
|
|
1891
1893
|
}
|
|
1892
|
-
const softWrapper = wrapAssertion(utils, expectAssertionName,
|
|
1894
|
+
const softWrapper = wrapAssertion(utils, expectAssertionName, __VITEST_EXTEND_ASSERTION__);
|
|
1893
1895
|
utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, expectAssertionName, softWrapper);
|
|
1894
1896
|
utils.addMethod(c.Assertion.prototype, expectAssertionName, softWrapper);
|
|
1897
|
+
// `expect.poll()` inspects the installed Chai assertion method,
|
|
1898
|
+
// so copy the internal marker from the original matcher function.
|
|
1899
|
+
// this is only for domain snapshot matchers for now.
|
|
1900
|
+
if (expectAssertion.__vitest_poll_takeover__) {
|
|
1901
|
+
const addedMethod = c.Assertion.prototype[expectAssertionName];
|
|
1902
|
+
Object.defineProperty(addedMethod, "__vitest_poll_takeover__", { value: true });
|
|
1903
|
+
}
|
|
1895
1904
|
class CustomMatcher extends AsymmetricMatcher {
|
|
1896
1905
|
constructor(inverse = false, ...sample) {
|
|
1897
1906
|
super(sample, inverse);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitest/expect",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.1.
|
|
4
|
+
"version": "4.1.4",
|
|
5
5
|
"description": "Jest's expect matchers as a Chai plugin",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": "https://opencollective.com/vitest",
|
|
@@ -39,11 +39,11 @@
|
|
|
39
39
|
"@types/chai": "^5.2.2",
|
|
40
40
|
"chai": "^6.2.2",
|
|
41
41
|
"tinyrainbow": "^3.1.0",
|
|
42
|
-
"@vitest/spy": "4.1.
|
|
43
|
-
"@vitest/utils": "4.1.
|
|
42
|
+
"@vitest/spy": "4.1.4",
|
|
43
|
+
"@vitest/utils": "4.1.4"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@vitest/runner": "4.1.
|
|
46
|
+
"@vitest/runner": "4.1.4"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "premove dist && rollup -c",
|