Skip to content
Vitest 1
Main Navigation 指南API配置高级
1.6.1
0.34.6

简体中文

English
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

简体中文

English
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

主题

Sidebar Navigation

指南

为什么选择 Vitest

快速开始

特性

工作区

命令行界面

测试筛选

报告器

代码覆盖率

快照(Snapshot)

模拟(Mocking)

类型测试

Vitest UI

浏览器模式

源码内测试

测试上下文

测试环境

扩展匹配器

IDE 集成

调试

与其他测试运行器的比较

迁移指南

常见错误

提升性能

API

测试 API 索引

模拟函数

Vi

expect

expectTypeOf

assert(断言)

assertType

配置

管理 Vitest 配置文件

配置 Vitest

页面导航

expectTypeOf ​

WARNING

在运行时,此函数不做任何事情。 要启用类型检查,请不要忘记传递 --typecheck 标志。

  • 类型: <T>(a: unknown) => ExpectTypeOf

not ​

  • 类型: ExpectTypeOf

你可以使用 .not 属性来否定所有断言。

toEqualTypeOf ​

  • 类型: <T>(expected: T) => void

此匹配器会检查类型是否完全相同。如果两个对象的值不同但类型相同,此匹配器不会报错。但是,如果一个对象缺少属性,它将会失败。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>();
expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 });
expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 });
expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>();

toMatchTypeOf ​

  • 类型: <T>(expected: T) => void

此匹配器检查被测试的类型是否继承自提供的类型。它与 toEqual 不同,更类似于 expect 中的 toMatchObject()。使用此匹配器,你可以检查一个对象是否“匹配”一个类型。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 });
expectTypeOf<number>().toMatchTypeOf<string | number>();
expectTypeOf<string | number>().not.toMatchTypeOf<number>();

extract ​

  • 类型: ExpectTypeOf<ExtractedUnion>

你可以使用 .extract 来缩小类型,以便进行进一步的测试。

ts
import { expectTypeOf } from 'vitest';

type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T };

interface CSSProperties {
  margin?: string;
  padding?: string;
}

function getResponsiveProp<T>(_props: T): ResponsiveProp<T> {
  return {};
}

const cssProperties: CSSProperties = { margin: '1px', padding: '2px' };

expectTypeOf(getResponsiveProp(cssProperties))
  .extract<{ xs?: any }>() // 从联合类型中提取指定类型
  .toEqualTypeOf<{
    xs?: CSSProperties;
    sm?: CSSProperties;
    md?: CSSProperties;
  }>();

expectTypeOf(getResponsiveProp(cssProperties))
  .extract<unknown[]>() // 从联合类型中提取一个数组
  .toEqualTypeOf<CSSProperties[]>();

WARNING

如果在联合类型中没有找到指定的类型,.extract 将返回 never。

exclude ​

  • 类型: ExpectTypeOf<NonExcludedUnion>

你可以使用 .exclude 从联合类型中移除类型,以便进行进一步测试。

ts
import { expectTypeOf } from 'vitest';

type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T };

interface CSSProperties {
  margin?: string;
  padding?: string;
}

function getResponsiveProp<T>(_props: T): ResponsiveProp<T> {
  return {};
}

const cssProperties: CSSProperties = { margin: '1px', padding: '2px' };

expectTypeOf(getResponsiveProp(cssProperties))
  .exclude<unknown[]>()
  .exclude<{ xs?: unknown }>() // 或者可以写成 .exclude<unknown[] | { xs?: unknown }>()
  .toEqualTypeOf<CSSProperties>();

WARNING

如果在联合类型中没有找到指定的类型,.exclude 将返回 never。

returns ​

  • 类型: ExpectTypeOf<ReturnValue>

你可以使用 .returns 提取函数类型的返回值。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2]);

WARNING

如果在非函数类型上使用,它将返回 never,因此你无法与其他匹配器链式调用。

parameters ​

  • 类型: ExpectTypeOf<Parameters>

你可以使用 .parameters 提取函数参数,然后对参数值执行断言。提取的参数会以数组形式返回。

ts
import { expectTypeOf } from 'vitest';

type NoParam = () => void;
type HasParam = (s: string) => void;

expectTypeOf<NoParam>().parameters.toEqualTypeOf<[]>();
expectTypeOf<HasParam>().parameters.toEqualTypeOf<[string]>();

WARNING

如果在非函数类型上使用,它将返回 never,因此你将无法将其与其他匹配器链接。

TIP

你也可以使用 .toBeCallableWith 匹配器,它提供更具表达力的断言方式。

parameter ​

  • 类型: (nth: number) => ExpectTypeOf

你可以使用 .parameter(number) 提取指定位置的函数参数,然后对其执行其他断言。

ts
import { expectTypeOf } from 'vitest';

function foo(a: number, b: string) {
  return [a, b];
}

expectTypeOf(foo).parameter(0).toBeNumber();
expectTypeOf(foo).parameter(1).toBeString();

WARNING

如果在非函数类型上使用,它将返回 never,因此你将无法将其与其他匹配器链接。

constructorParameters ​

  • 类型: ExpectTypeOf<ConstructorParameters>

你可以提取构造函数的参数,这些参数会以数组形式返回,然后你可以使用此方法对它们执行断言。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(Date).constructorParameters.toEqualTypeOf<
  [] | [string | number | Date]
>();

WARNING

如果在非函数类型上使用,它将返回 never,因此你将无法将其与其他匹配器链接。

TIP

你也可以使用 .toBeConstructibleWith 匹配器,它提供更具表达力的断言方式。

instance ​

  • 类型: ExpectTypeOf<ConstructableInstance>

通过此属性,可以访问一组用于测试提供的类的实例的匹配器。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(Date).instance.toHaveProperty('toISOString');

WARNING

如果在非函数类型上使用,它将返回 never,因此你将无法将其与其他匹配器链接。

items ​

  • 类型: ExpectTypeOf<T>

你可以使用 .items 获取数组项的类型,以便执行进一步的断言。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf([1, 2, 3]).items.toEqualTypeOf<number>();
expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf<string>();

resolves ​

  • 类型: ExpectTypeOf<ResolvedPromise>

此匹配器提取 Promise resolve 后的值,然后你可以对其执行其他断言。

ts
import { expectTypeOf } from 'vitest';

async function asyncFunc() {
  return 123;
}

expectTypeOf(asyncFunc).returns.resolves.toBeNumber();
expectTypeOf(Promise.resolve('string')).resolves.toBeString();

WARNING

如果在非 Promise 类型上使用,它将返回 never,因此你将无法将其与其他匹配器链接。

guards ​

  • 类型: ExpectTypeOf<Guard>

此匹配器提取类型守卫的值(例如,v is number),然后你可以对其执行断言。

ts
import { expectTypeOf } from 'vitest';

function isString(v: any): v is string {
  return typeof v === 'string';
}
expectTypeOf(isString).guards.toBeString();

WARNING

如果该值不是类型守卫函数,则返回 never,因此你将无法将其与其他匹配器链接。

asserts ​

  • 类型: ExpectTypeOf<Assert>

此匹配器提取断言函数的值(例如,assert v is number),然后你可以对其执行断言。

ts
import { expectTypeOf } from 'vitest';

function assertNumber(v: any): asserts v is number {
  if (typeof v !== 'number') throw new TypeError('Nope !');
}

expectTypeOf(assertNumber).asserts.toBeNumber();

WARNING

如果该值不是断言函数,则返回 never,因此你将无法将其与其他匹配器链接。

toBeAny ​

  • 类型: () => void

使用此匹配器,你可以检查提供的类型是否为 any。如果类型过于具体,测试将失败。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf<any>().toBeAny();
expectTypeOf({} as any).toBeAny();
expectTypeOf('string').not.toBeAny();

toBeUnknown ​

  • 类型: () => void

此匹配器检查提供的类型是否为 unknown。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf().toBeUnknown();
expectTypeOf({} as unknown).toBeUnknown();
expectTypeOf('string').not.toBeUnknown();

toBeNever ​

  • 类型: () => void

此匹配器检查提供的类型是否为 never。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf<never>().toBeNever();
expectTypeOf((): never => {}).returns.toBeNever();

toBeFunction ​

  • 类型: () => void

此匹配器检查提供的类型是否为函数。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeFunction();
expectTypeOf((): never => {}).toBeFunction();

toBeObject ​

  • 类型: () => void

此匹配器检查提供的类型是否为对象。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeObject();
expectTypeOf({}).toBeObject();

toBeArray ​

  • 类型: () => void

此匹配器检查提供的类型是否为数组。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeArray();
expectTypeOf([]).toBeArray();
expectTypeOf([1, 2]).toBeArray();
expectTypeOf([{}, 42]).toBeArray();

toBeString ​

  • 类型: () => void

此匹配器检查提供的类型是否为字符串。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeString();
expectTypeOf('').toBeString();
expectTypeOf('a').toBeString();

toBeBoolean ​

  • 类型: () => void

此匹配器检查提供的类型是否为布尔值。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeBoolean();
expectTypeOf(true).toBeBoolean();
expectTypeOf<boolean>().toBeBoolean();

toBeVoid ​

  • 类型: () => void

此匹配器检查提供的类型是否为 void。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf<void>().toBeVoid();

toBeSymbol ​

  • 类型: () => void

此匹配器检查提供的类型是否为符号。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(Symbol(1)).toBeSymbol();
expectTypeOf<symbol>().toBeSymbol();

toBeNull ​

  • 类型: () => void

此匹配器检查提供的类型是否为 null。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(null).toBeNull();
expectTypeOf<null>().toBeNull();
expectTypeOf(undefined).not.toBeNull();

toBeUndefined ​

  • 类型: () => void

此匹配器检查提供的类型是否为 undefined。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(undefined).toBeUndefined();
expectTypeOf<undefined>().toBeUndefined();
expectTypeOf(null).not.toBeUndefined();

toBeNullable ​

  • 类型: () => void

此匹配器检查提供的类型是否可以与 null 或 undefined 一起使用。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf<1 | undefined>().toBeNullable();
expectTypeOf<1 | null>().toBeNullable();
expectTypeOf<1 | undefined | null>().toBeNullable();

toBeCallableWith ​

  • 类型: () => void

此匹配器用于确保可以使用给定的参数调用函数。

ts
import { expectTypeOf } from 'vitest';

type NoParam = () => void;
type HasParam = (s: string) => void;

expectTypeOf<NoParam>().toBeCallableWith();
expectTypeOf<HasParam>().toBeCallableWith('some string');

WARNING

如果在非函数类型上使用,它将返回 never,因此你将无法将其与其他匹配器链接。

toBeConstructibleWith ​

  • 类型: () => void

此匹配器用于确保可以使用给定的构造函数参数创建一个新的实例。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(Date).toBeConstructibleWith(new Date());
expectTypeOf(Date).toBeConstructibleWith('01-01-2000');

WARNING

如果在非函数类型上使用,它将返回 never,因此你将无法将其与其他匹配器链接。

toHaveProperty ​

  • 类型: <K extends keyof T>(property: K) => ExpectTypeOf<T[K>

此匹配器检查提供的对象是否存在属性。如果属性存在,它还会返回一套用于该属性类型的匹配器,因此你可以链式地进行断言。

ts
import { expectTypeOf } from 'vitest';

const obj = { a: 1, b: '' };

expectTypeOf(obj).toHaveProperty('a');
expectTypeOf(obj).not.toHaveProperty('c');

expectTypeOf(obj).toHaveProperty('a').toBeNumber();
expectTypeOf(obj).toHaveProperty('b').toBeString();
expectTypeOf(obj).toHaveProperty('a').not.toBeString();
Pager
上一页expect
下一页assert(断言)

基于 MIT 许可证 发布。

版权所有 (c) 2024 Mithril Contributors

https://v1.vitest.dev/api/expect-typeof

基于 MIT 许可证 发布。

版权所有 (c) 2024 Mithril Contributors