Skip to content
Vitest 3
Main Navigation 가이드 & API구성브라우저 모드고급 API
3.2.0
2.1.9
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를 선택하는 이유

시작하기

기능

Vitest 구성하기

API

테스트 API 참조

Mock 함수

Vi

expect

expectTypeOf

assert

assertType

가이드

명령줄 인터페이스

테스트 필터링

테스트 프로젝트

리포터

커버리지

스냅샷

모킹

병렬 처리

타입 검사

Vitest UI

소스 내 테스팅

테스트 컨텍스트

테스트 어노테이션

테스트 환경

매처 확장하기

IDE 통합

디버깅

일반적인 오류

마이그레이션 가이드

Vitest 3.0으로 마이그레이션

Jest에서 마이그레이션

성능

테스트 성능 프로파일링

성능 향상

브라우저 모드

고급 API

다른 테스트 러너와의 비교

이 페이지에서

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의 해결된 값을 추출하여 해당 값에 대해 다른 어설션을 수행할 수 있게 합니다.

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

이 매처는 제공된 타입이 object인지 확인합니다.

ts
import { expectTypeOf } from 'vitest';

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

toBeArray ​

  • 타입: () => void

이 매처는 제공된 타입이 배열<T>인지 확인합니다.

ts
import { expectTypeOf } from 'vitest';

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

toBeString ​

  • 타입: () => void

이 매처는 제공된 타입이 string인지 확인합니다.

ts
import { expectTypeOf } from 'vitest';

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

toBeBoolean ​

  • 타입: () => void

이 매처는 제공된 타입이 boolean인지 확인합니다.

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

이 매처는 제공된 타입이 symbol인지 확인합니다.

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<undefined | 1>().toBeNullable();
expectTypeOf<null | 1>().toBeNullable();
expectTypeOf<undefined | null | 1>().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 라이선스 하에 배포되었습니다.

Copyright (c) 2021-Present Vitest Team

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

MIT 라이선스 하에 배포되었습니다.

Copyright (c) 2021-Present Vitest Team