Skip to content
Vitest 2
Main Navigation 가이드API구성브라우저 모드고급
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

테스트 API 참조

Mock 함수

Vi

expect

expectTypeOf

assert

assertType

이 페이지에서

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

이 매처는 예상 타입이 주어진 타입을 확장하는지 확인합니다. 이는 toEqualTypeOf와 다르며 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 }>() // 유니온에서 { xs?: any } 타입을 추출합니다.
  .toEqualTypeOf<{
    xs?: CSSProperties;
    sm?: CSSProperties;
    md?: CSSProperties;
  }>();

expectTypeOf(getResponsiveProp(cssProperties))
  .extract<unknown[]>() // 유니온에서 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)를 사용하여 특정 함수 인수의 타입을 추출하여 다른 어설션을 수행할 수 있습니다. 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인지 확인합니다. 타입이 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

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

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

이 매처는 제공된 타입이 Array<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를 포함하는지 확인합니다. 즉, nullable한 타입인지 확인합니다.

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) 2024 Mithril Contributors

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

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

Copyright (c) 2024 Mithril Contributors