assertType example with function overloads
Example usage of assertType: import { assertType } from 'vitest'; function concat(a: string, b: string): string; function concat(a: number, b: number): number; function concat(a: string | number, b: string | number): string | number; assertType<string>(concat('a', 'b')); assertType<number>(concat(1, 2)); // @ts-expect-error wrong types; assertType(concat('a', 2));
assertType as alternative to expectTypeOf
assertType can be used as an alternative to expectTypeOf for easily asserting that the argument type is equal to the generic provided.
assertType function signature
assertType is a generic function with the signature <T>(value: T): void. It asserts that the argument type is equal to the generic type provided.
assertType does not execute at runtime
During runtime, the assertType function doesn't do anything. It only performs type checking when the --typecheck flag is passed.
expectTypeOf.not negation property
The `.not` property on ExpectTypeOf has type `ExpectTypeOf` and can be used to negate all assertions.
toExtend matcher type
The `toExtend` matcher has type `<T>(expected: T) => void`. It checks if expected type extends provided type, similar to expect's `toMatchObject()`. With this matcher you can check if an object 'matches' a type.
toMatchObjectType matcher type
The `toMatchObjectType` matcher has type `() => void`. It performs a strict check on object types, ensuring that the expected type matches the provided object type. It's stricter than `toExtend` and recommended for object types as it's more likely to catch issues like readonly properties. This matcher only works with plain object types and will fail for union types and other complex types.
extract property type
The `.extract` property has type `ExpectTypeOf<ExtractedUnion>`. You can use `.extract` to narrow down types for further testing by extracting a specific type from a union. If no type is found in the union, `.extract` will return `never`.
exclude property type
The `.exclude` property has type `ExpectTypeOf<NonExcludedUnion>`. You can use `.exclude` to remove types from a union for further testing. If no type is found in the union, `.exclude` will return `never`.
returns property type
The `.returns` property has type `ExpectTypeOf<ReturnValue>`. You can use `.returns` to extract return value of a function type. If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers.
parameters property type
The `.parameters` property has type `ExpectTypeOf<Parameters>`. You can extract function arguments with `.parameters` to perform assertions on its value. Parameters are returned as an array. If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers.
parameter method type
The `.parameter(nth: number)` method has type `(nth: number) => ExpectTypeOf`. You can extract a certain function argument with `.parameter(number)` call to perform other assertions on it. If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers.
constructorParameters property type
The `.constructorParameters` property has type `ExpectTypeOf<ConstructorParameters>`. You can extract constructor parameters as an array of values and perform assertions on them with this property. If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers.
instance property type
The `.instance` property has type `ExpectTypeOf<ConstructableInstance>`. This property gives access to matchers that can be performed on an instance of the provided class. If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers.
items property type
The `.items` property has type `ExpectTypeOf<T>`. You can get array item type with `.items` to perform further assertions.
resolves property type
The `.resolves` property has type `ExpectTypeOf<ResolvedPromise>`. This property extracts resolved value of a `Promise`, so you can perform other assertions on it. If used on a non-promise type, it will return `never`, so you won't be able to chain it with other matchers.
guards property type
The `.guards` property has type `ExpectTypeOf<Guard>`. This property extracts guard value (e.g., `v is number`), so you can perform assertions on it. Returns `never`, if the value is not a guard function, so you won't be able to chain it with other matchers.
asserts property type
The `.asserts` property has type `ExpectTypeOf<Assert>`. This property extracts assert value (e.g., `assert v is number`), so you can perform assertions on it. Returns `never`, if the value is not an assert function, so you won't be able to chain it with other matchers.
toBeAny matcher type
The `toBeAny` matcher has type `() => void`. With this matcher you can check if provided type is `any` type. If the type is too specific, the test will fail.
toBeNever matcher type
The `toBeNever` matcher has type `() => void`. This matcher checks if provided type is a `never` type.
toBeFunction matcher type
The `toBeFunction` matcher has type `() => void`. This matcher checks if provided type is a `function`.
toBeObject matcher type
The `toBeObject` matcher has type `() => void`. This matcher checks if provided type is an `object`.
toBeCallableWith matcher type
The `toBeCallableWith` matcher has type `() => void`. This matcher ensures you can call provided function with a set of parameters. If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers.
toBeConstructibleWith matcher type
The `toBeConstructibleWith` matcher has type `() => void`. This matcher ensures you can create a new instance with a set of constructor parameters. If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers.
toHaveProperty matcher type
The `toHaveProperty` matcher has type `<K extends keyof T>(property: K) => ExpectTypeOf<T[K]>`. This matcher checks if a property exists on the provided object. If it exists, it also returns the same set of matchers for the type of this property, so you can chain assertions one after another.
branded property type
The `.branded` property has type `ExpectTypeOf<BrandedType>`. You can use `.branded` to allow type assertions to succeed for types that are semantically equivalent but differ in representation. This helper comes at a performance cost and can cause the TypeScript compiler to 'give up' if used with excessively deep types. Use it sparingly and only when necessary.
toBeString matcher type
The `toBeString` matcher has type `() => void`. This matcher checks if provided type is a `string`.
toBeBoolean matcher type
The `toBeBoolean` matcher has type `() => void`. This matcher checks if provided type is `boolean`.
toBeVoid matcher type
The `toBeVoid` matcher has type `() => void`. This matcher checks if provided type is `void`.
toBeSymbol matcher type
The `toBeSymbol` matcher has type `() => void`. This matcher checks if provided type is a `symbol`.
toBeNull matcher type
The `toBeNull` matcher has type `() => void`. This matcher checks if provided type is `null`.
toBeUndefined matcher type
The `toBeUndefined` matcher has type `() => void`. This matcher checks if provided type is `undefined`.
toBeNullable matcher type
The `toBeNullable` matcher has type `() => void`. This matcher checks if you can use `null` or `undefined` with provided type.
toMatchTypeOf matcher deprecated status
The `toMatchTypeOf` matcher has been deprecated since expect-type v1.2.0. Use `toExtend` instead.
toBeArray matcher type
The `toBeArray` matcher has type `() => void`. This matcher checks if provided type is `Array<T>`.
toEqualTypeOf matcher type
The `toEqualTypeOf` matcher has type `<T>(expected: T) => void`. It checks if types are fully equal to each other. It does not fail if two objects have different values but the same type, but will fail if an object is missing a property.
expectTypeOf function signature and type
expectTypeOf is a function with type `<T>(a: unknown) => ExpectTypeOf` that takes an unknown argument and returns an ExpectTypeOf object. During runtime this function doesn't do anything. To enable typechecking, pass the `--typecheck` flag.
toHaveBeenLastCalledWith checks last spy call parameters
toHaveBeenLastCalledWith type: (...args: any[]) => Awaitable<void>. It checks if a function was called with certain parameters at its last invocation. Requires a spy function to be passed to expect.
toHaveBeenNthCalledWith checks spy call parameters at specific time
toHaveBeenNthCalledWith type: (time: number, ...args: any[]) => Awaitable<void>. It checks if a function was called with certain parameters at the certain time. The count starts at 1. Requires a spy function to be passed to expect.
toHaveReturnedTimes checks spy return count
toHaveReturnedTimes type: (amount: number) => Awaitable<void>. It checks if a function has successfully returned a value an exact amount of times (i.e., did not throw an error). Requires a spy function to be passed to expect.
toHaveLastReturnedWith checks last spy return value
toHaveLastReturnedWith type: (returnValue: any) => Awaitable<void>. It checks if a function has successfully returned a certain value when it was last invoked. Requires a spy function to be passed to expect.
toHaveNthReturnedWith checks spy return value at specific time
toHaveNthReturnedWith type: (time: number, returnValue: any) => Awaitable<void>. It checks if a function has successfully returned a value with certain parameters on a certain call. The count starts at 1.
toHaveResolvedTimes checks spy resolve count
toHaveResolvedTimes type: (amount: number) => Awaitable<void>. It checks if a function has successfully resolved a value an exact amount of times (i.e., did not reject). Requires a spy function to be passed to expect. Only counts resolved promises. If the function returned a promise but it was not resolved yet, it will not be counted.
toHaveLastResolvedWith checks last spy resolved value
toHaveLastResolvedWith type: (returnValue: any) => Awaitable<void>. It checks if a function has successfully resolved a certain value when it was last invoked. Requires a spy function to be passed to expect. If the function returned a promise but it was not resolved yet, this will fail.
toHaveNthResolvedWith checks spy resolved value at specific time
toHaveNthResolvedWith type: (time: number, returnValue: any) => Awaitable<void>. It checks if a function has successfully resolved a certain value on a specific invocation. Requires a spy function to be passed to expect. If the function returned a promise but it was not resolved yet, this will fail. The count starts at 1.
toHaveBeenExhausted checks vi.when chain fully consumed
toHaveBeenExhausted type: () => void. It checks that every behavior registered on a vi.when chain has been consumed. A behavior is considered exhausted when it has been called the number of times specified by its times option, or at least once for behaviors that apply indefinitely. Requires a When chain returned by vi.when to be passed to expect. A When chain with no registered behaviors is never considered exhausted. toHaveBeenExhausted only passes when at least one calledWith with an associated action (then*) has been registered and every registered behavior has been fully consumed.
Chai-style callCount assertion checks spy call count
callCount type: (count: number) => void. As of version 4.1.0. Chai-style assertion that checks if a spy was called a specific number of times. Equivalent to toHaveBeenCalledTimes(count).
Chai-style calledOnce assertion checks spy called once
calledOnce: Assertion (property, not a method). Type as of version 4.1.0. Chai-style assertion that checks if a spy was called exactly once. Equivalent to toHaveBeenCalledOnce(). Access it without parentheses: expect(spy).to.have.been.calledOnce
Chai-style calledOnceWith assertion checks spy called once with parameters
calledOnceWith type: (...args: any[]) => void. As of version 4.1.0. Chai-style assertion that checks if a spy was called exactly once with specific arguments. Equivalent to toHaveBeenCalledExactlyOnceWith(...args).
Chai-style calledTwice assertion checks spy called twice
calledTwice: Assertion (property, not a method). Type as of version 4.1.0. Chai-style assertion that checks if a spy was called exactly twice. Equivalent to toHaveBeenCalledTimes(2). Access it without parentheses: expect(spy).to.have.been.calledTwice
Chai-style calledThrice assertion checks spy called thrice
calledThrice: Assertion (property, not a method). Type as of version 4.1.0. Chai-style assertion that checks if a spy was called exactly three times. Equivalent to toHaveBeenCalledTimes(3). Access it without parentheses: expect(spy).to.have.been.calledThrice
Chai-style lastCalledWith assertion checks last spy call
lastCalledWith type: (...args: any[]) => void. Chai-style assertion that checks if the last call to a spy was made with specific arguments. Equivalent to toHaveBeenLastCalledWith(...args).
Chai-style nthCalledWith assertion checks nth spy call
nthCalledWith type: (n: number, ...args: any[]) => void. Chai-style assertion that checks if the nth call to a spy was made with specific arguments. Equivalent to toHaveBeenNthCalledWith(n, ...args).
Chai-style returnedWith assertion checks spy returned value
returnedWith type: (value: any) => void. As of version 4.1.0. Chai-style assertion that checks if a spy returned a specific value at least once. Equivalent to toHaveReturnedWith(value).
Chai-style returnedTimes assertion checks spy return count
returnedTimes type: (count: number) => void. As of version 4.1.0. Chai-style assertion that checks if a spy returned successfully a specific number of times. Equivalent to toHaveReturnedTimes(count).
Chai-style lastReturnedWith assertion checks last spy return
lastReturnedWith type: (value: any) => void. Chai-style assertion that checks if the last return value of a spy matches the expected value. Equivalent to toHaveLastReturnedWith(value).
Chai-style nthReturnedWith assertion checks nth spy return
nthReturnedWith type: (n: number, value: any) => void. Chai-style assertion that checks if the nth return value of a spy matches the expected value. Equivalent to toHaveNthReturnedWith(n, value).
Chai-style calledAfter assertion checks spy called after another
calledAfter type: (mock: MockInstance, failIfNoFirstInvocation?: boolean) => void. As of version 4.1.0. Chai-style assertion that checks if a spy was called after another spy. Equivalent to toHaveBeenCalledAfter(mock, failIfNoFirstInvocation).
expect.addSnapshotSerializer registers custom snapshot serializer
expect.addSnapshotSerializer type: (plugin: PrettyFormatPlugin) => void. Registers a custom snapshot serializer plugin.
toSatisfy checks value satisfies predicate
toSatisfy type: (predicate: (value: any) => boolean) => Awaitable<void>. It checks if a value satisfies a certain predicate function.