This commit is contained in:
2026-08-14 16:03:47 +01:00
parent fd53e3f4fb
commit dfdc3b2840
2463 changed files with 186456 additions and 111315 deletions
+180 -56
View File
@@ -1,6 +1,4 @@
import { Test } from '@vitest/runner';
import { MockInstance } from '@vitest/spy';
import { Constructable } from '@vitest/utils';
import { Formatter } from 'tinyrainbow';
import { StandardSchemaV1 } from '@standard-schema/spec';
import { diff, printDiffOrStringify } from '@vitest/utils/diff';
@@ -9,11 +7,6 @@ import { stringify } from '@vitest/utils/display';
import * as chai from 'chai';
export { chai };
declare const MATCHERS_OBJECT: unique symbol;
declare const JEST_MATCHERS_OBJECT: unique symbol;
declare const GLOBAL_EXPECT: unique symbol;
declare const ASYMMETRIC_MATCHERS_OBJECT: unique symbol;
interface AsymmetricMatcherInterface {
asymmetricMatch: (other: unknown, customTesters?: Array<Tester>) => boolean;
toString: () => string;
@@ -131,9 +124,18 @@ interface MatcherState {
customTesters: Array<Tester>;
assertionCalls: number;
currentTestName?: string;
/**
* @deprecated exists only in types
*/
dontThrow?: () => void;
/**
* @deprecated exists only in types
*/
error?: Error;
equals: (a: unknown, b: unknown, customTesters?: Array<Tester>, strictCheck?: boolean) => boolean;
/**
* @deprecated exists only in types
*/
expand?: boolean;
expectedAssertionsNumber?: number | null;
expectedAssertionsNumberErrorGen?: (() => Error) | null;
@@ -141,6 +143,9 @@ interface MatcherState {
isExpectingAssertionsError?: Error | null;
isNot: boolean;
promise: string;
/**
* @deprecated exists only in types
*/
suppressedErrors: Array<Error>;
testPath?: string;
utils: ReturnType<typeof getMatcherUtils> & {
@@ -151,13 +156,19 @@ interface MatcherState {
};
soft?: boolean;
poll?: boolean;
task?: Readonly<Test>;
/**
* The same assertion instance that chai plugins receive.
* @experimental
* @see {@link https://www.chaijs.com/guide/plugins/} Core Plugin Concepts
*/
readonly assertion: Assertion;
}
interface SyncExpectationResult {
pass: boolean;
message: () => string;
actual?: any;
expected?: any;
meta?: object;
}
type AsyncExpectationResult = Promise<SyncExpectationResult>;
type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;
@@ -234,8 +245,6 @@ interface AsymmetricMatchersContaining extends CustomMatcher {
/**
* Matches if the received number is within a certain precision of the expected number.
*
* @param precision - Optional decimal precision for comparison. Default is 2.
*
* @example
* expect(10.45).toEqual(expect.closeTo(10.5, 1));
* expect(5.11).toEqual(expect.closeTo(5.12)); // with default precision
@@ -452,6 +461,7 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*
* @example
* expect(mockFunc).toBeCalledTimes(2);
* @deprecated Use `toHaveBeenCalledTimes` instead
*/
toBeCalledTimes: (times: number) => void;
/**
@@ -470,6 +480,7 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*
* @example
* expect(mockFunc).toBeCalled();
* @deprecated Use `toHaveBeenCalled` instead
*/
toBeCalled: () => void;
/**
@@ -488,6 +499,7 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*
* @example
* expect(mockFunc).toBeCalledWith('arg1', 42);
* @deprecated Use `toHaveBeenCalledWith` instead
*/
toBeCalledWith: <E extends any[]>(...args: E) => void;
/**
@@ -500,15 +512,6 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*/
toHaveBeenNthCalledWith: <E extends any[]>(n: number, ...args: E) => void;
/**
* Ensure that a mock function is called with specific arguments on an Nth call.
*
* Alias for `expect.toHaveBeenNthCalledWith`.
*
* @example
* expect(mockFunc).nthCalledWith(2, 'secondArg');
*/
nthCalledWith: <E extends any[]>(nthCall: number, ...args: E) => void;
/**
* If you have a mock function, you can use `.toHaveBeenLastCalledWith`
* to test what arguments it was last called with.
*
@@ -519,16 +522,6 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*/
toHaveBeenLastCalledWith: <E extends any[]>(...args: E) => void;
/**
* If you have a mock function, you can use `.lastCalledWith`
* to test what arguments it was last called with.
*
* Alias for `expect.toHaveBeenLastCalledWith`.
*
* @example
* expect(mockFunc).lastCalledWith('lastArg');
*/
lastCalledWith: <E extends any[]>(...args: E) => void;
/**
* Used to test that a function throws when it is called.
*
* Also under the alias `expect.toThrowError`.
@@ -536,8 +529,9 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
* @example
* expect(() => functionWithError()).toThrow('Error message');
* expect(() => parseJSON('invalid')).toThrow(SyntaxError);
* expect(() => { throw 42 }).toThrow(42);
*/
toThrow: (expected?: string | Constructable | RegExp | Error) => void;
toThrow: (expected?: any) => void;
/**
* Used to test that a function throws when it is called.
*
@@ -546,8 +540,10 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
* @example
* expect(() => functionWithError()).toThrowError('Error message');
* expect(() => parseJSON('invalid')).toThrowError(SyntaxError);
* expect(() => { throw 42 }).toThrowError(42);
* @deprecated Use `toThrow` instead
*/
toThrowError: (expected?: string | Constructable | RegExp | Error) => void;
toThrowError: (expected?: any) => void;
/**
* Use to test that the mock function successfully returned (i.e., did not throw an error) at least one time
*
@@ -555,6 +551,7 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*
* @example
* expect(mockFunc).toReturn();
* @deprecated Use `toHaveReturned` instead
*/
toReturn: () => void;
/**
@@ -574,6 +571,7 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*
* @example
* expect(mockFunc).toReturnTimes(3);
* @deprecated Use `toHaveReturnedTimes` instead
*/
toReturnTimes: (times: number) => void;
/**
@@ -593,6 +591,7 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*
* @example
* expect(mockFunc).toReturnWith('returnValue');
* @deprecated Use `toHaveReturnedWith` instead
*/
toReturnWith: <E>(value: E) => void;
/**
@@ -616,17 +615,6 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
*/
toHaveLastReturnedWith: <E>(value: E) => void;
/**
* Use to test the specific value that a mock function last returned.
* If the last call to the mock function threw an error, then this matcher will fail
* no matter what value you provided as the expected return value.
*
* Alias for `expect.toHaveLastReturnedWith`.
*
* @example
* expect(mockFunc).lastReturnedWith('lastValue');
*/
lastReturnedWith: <E>(value: E) => void;
/**
* Use to test the specific value that a mock function returned for the nth call.
* If the nth call to the mock function threw an error, then this matcher will fail
* no matter what value you provided as the expected return value.
@@ -637,17 +625,6 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
* expect(mockFunc).toHaveNthReturnedWith(2, 'nthValue');
*/
toHaveNthReturnedWith: <E>(nthCall: number, value: E) => void;
/**
* Use to test the specific value that a mock function returned for the nth call.
* If the nth call to the mock function threw an error, then this matcher will fail
* no matter what value you provided as the expected return value.
*
* Alias for `expect.toHaveNthReturnedWith`.
*
* @example
* expect(mockFunc).nthReturnedWith(2, 'nthValue');
*/
nthReturnedWith: <E>(nthCall: number, value: E) => void;
}
type VitestAssertion<
A,
@@ -655,7 +632,7 @@ type VitestAssertion<
> = { [K in keyof A] : A[K] extends Chai.Assertion ? Assertion<T> : A[K] extends (...args: any[]) => any ? A[K] : VitestAssertion<A[K], T> } & ((type: string, message?: string) => Assertion);
type Promisify<O> = { [K in keyof O] : O[K] extends (...args: infer A) => infer R ? Promisify<O[K]> & ((...args: A) => Promise<R>) : O[K] };
type PromisifyAssertion<T> = Promisify<Assertion<T>>;
interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAssertion<T>, Matchers<T> {
interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAssertion<T>, ChaiMockAssertion, Matchers<T> {
/**
* Ensures a value is of a specific type.
*
@@ -759,6 +736,140 @@ interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAss
*/
rejects: PromisifyAssertion<T>;
}
/**
* Chai-style assertions for spy/mock testing.
* These provide sinon-chai compatible assertion names that delegate to Jest-style implementations.
*/
interface ChaiMockAssertion {
/**
* Checks that a spy was called at least once.
* Chai-style equivalent of `toHaveBeenCalled`.
*
* @example
* expect(spy).to.have.been.called
*/
readonly called: Assertion;
/**
* Checks that a spy was called a specific number of times.
* Chai-style equivalent of `toHaveBeenCalledTimes`.
*
* @example
* expect(spy).to.have.callCount(3)
*/
callCount: (count: number) => void;
/**
* Checks that a spy was called with specific arguments at least once.
* Chai-style equivalent of `toHaveBeenCalledWith`.
*
* @example
* expect(spy).to.have.been.calledWith('arg1', 'arg2')
*/
calledWith: <E extends any[]>(...args: E) => void;
/**
* Checks that a spy was called exactly once.
* Chai-style equivalent of `toHaveBeenCalledOnce`.
*
* @example
* expect(spy).to.have.been.calledOnce
*/
readonly calledOnce: Assertion;
/**
* Checks that a spy was called exactly once with specific arguments.
* Chai-style equivalent of `toHaveBeenCalledExactlyOnceWith`.
*
* @example
* expect(spy).to.have.been.calledOnceWith('arg1', 'arg2')
*/
calledOnceWith: <E extends any[]>(...args: E) => void;
/**
* Checks that the last call to a spy was made with specific arguments.
* Chai-style equivalent of `toHaveBeenLastCalledWith`.
*
* @example
* expect(spy).to.have.been.lastCalledWith('arg1', 'arg2')
*/
lastCalledWith: <E extends any[]>(...args: E) => void;
/**
* Checks that the nth call to a spy was made with specific arguments.
* Chai-style equivalent of `toHaveBeenNthCalledWith`.
*
* @example
* expect(spy).to.have.been.nthCalledWith(2, 'arg1', 'arg2')
*/
nthCalledWith: <E extends any[]>(n: number, ...args: E) => void;
/**
* Checks that a spy returned a specific value at least once.
* Chai-style equivalent of `toHaveReturnedWith`.
*
* @example
* expect(spy).to.have.returned('value')
*/
returned: <E>(value: E) => void;
/**
* Checks that a spy returned a specific value at least once.
* Chai-style equivalent of `toHaveReturnedWith`.
*
* @example
* expect(spy).to.have.returnedWith('value')
*/
returnedWith: <E>(value: E) => void;
/**
* Checks that a spy returned successfully a specific number of times.
* Chai-style equivalent of `toHaveReturnedTimes`.
*
* @example
* expect(spy).to.have.returnedTimes(3)
*/
returnedTimes: (count: number) => void;
/**
* Checks that the last return value of a spy matches the expected value.
* Chai-style equivalent of `toHaveLastReturnedWith`.
*
* @example
* expect(spy).to.have.lastReturnedWith('value')
*/
lastReturnedWith: <E>(value: E) => void;
/**
* Checks that the nth return value of a spy matches the expected value.
* Chai-style equivalent of `toHaveNthReturnedWith`.
*
* @example
* expect(spy).to.have.nthReturnedWith(2, 'value')
*/
nthReturnedWith: <E>(n: number, value: E) => void;
/**
* Checks that a spy was called before another spy.
* Chai-style equivalent of `toHaveBeenCalledBefore`.
*
* @example
* expect(spy1).to.have.been.calledBefore(spy2)
*/
calledBefore: (mock: MockInstance, failIfNoFirstInvocation?: boolean) => void;
/**
* Checks that a spy was called after another spy.
* Chai-style equivalent of `toHaveBeenCalledAfter`.
*
* @example
* expect(spy1).to.have.been.calledAfter(spy2)
*/
calledAfter: (mock: MockInstance, failIfNoFirstInvocation?: boolean) => void;
/**
* Checks that a spy was called exactly twice.
* Chai-style equivalent of `toHaveBeenCalledTimes(2)`.
*
* @example
* expect(spy).to.have.been.calledTwice
*/
readonly calledTwice: Assertion;
/**
* Checks that a spy was called exactly three times.
* Chai-style equivalent of `toHaveBeenCalledTimes(3)`.
*
* @example
* expect(spy).to.have.been.calledThrice
*/
readonly calledThrice: Assertion;
}
declare global {
namespace jest {
interface Matchers<
@@ -768,6 +879,13 @@ declare global {
}
}
declare const ChaiStyleAssertions: ChaiPlugin;
declare const MATCHERS_OBJECT: unique symbol;
declare const JEST_MATCHERS_OBJECT: unique symbol;
declare const GLOBAL_EXPECT: unique symbol;
declare const ASYMMETRIC_MATCHERS_OBJECT: unique symbol;
declare const customMatchers: MatchersObject;
declare const JestChaiExpect: ChaiPlugin;
@@ -777,6 +895,7 @@ declare const JestExtend: ChaiPlugin;
declare function equals(a: unknown, b: unknown, customTesters?: Array<Tester>, strictCheck?: boolean): boolean;
declare function isAsymmetric(obj: any): obj is AsymmetricMatcher<any>;
declare function hasAsymmetric(obj: any, seen?: Set<any>): boolean;
declare function isError(value: unknown): value is Error;
declare function isA(typeName: string, value: unknown): boolean;
declare function fnNameFor(func: Function): string;
declare function hasProperty(obj: object | null, property: string): boolean;
@@ -802,5 +921,10 @@ declare function isStandardSchema(obj: any): obj is StandardSchemaV1;
declare function getState<State extends MatcherState = MatcherState>(expect: ExpectStatic): State;
declare function setState<State extends MatcherState = MatcherState>(state: Partial<State>, expect: ExpectStatic): void;
export { ASYMMETRIC_MATCHERS_OBJECT, Any, Anything, ArrayContaining, AsymmetricMatcher, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, ObjectContaining, SchemaMatching, StringContaining, StringMatching, addCustomEqualityTesters, arrayBufferEquality, customMatchers, equals, fnNameFor, generateToBeMessage, getObjectKeys, getObjectSubset, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, isStandardSchema, iterableEquality, pluralize, setState, sparseArrayEquality, subsetEquality, typeEquality };
export type { Assertion, AsymmetricMatcherInterface, AsymmetricMatchersContaining, AsyncExpectationResult, ChaiPlugin, DeeplyAllowMatchers, ExpectStatic, ExpectationResult, JestAssertion, MatcherHintOptions, MatcherState, Matchers, MatchersObject, PromisifyAssertion, RawMatcherFn, SyncExpectationResult, Tester, TesterContext };
declare function createAssertionMessage(util: Chai.ChaiUtils, assertion: Chai.Assertion, hasArgs: boolean): string;
declare function recordAsyncExpect(_test: any, promise: Promise<any>, assertion: string, error: Error, isSoft?: boolean): Promise<any>;
/** wrap assertion function to support `expect.soft` and provide assertion name as `_name` */
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>;
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 };
export type { Assertion, AsymmetricMatcherInterface, AsymmetricMatchersContaining, AsyncExpectationResult, ChaiMockAssertion, ChaiPlugin, DeeplyAllowMatchers, ExpectStatic, ExpectationResult, JestAssertion, MatcherHintOptions, MatcherState, Matchers, MatchersObject, PromisifyAssertion, RawMatcherFn, SyncExpectationResult, Tester, TesterContext };