diff --git a/docs/api/assert-type.md b/docs/api/assert-type.md index 9bfa028d61bb..9491cd9c3090 100644 --- a/docs/api/assert-type.md +++ b/docs/api/assert-type.md @@ -1,18 +1,22 @@ # assertType - - **Type:** `(value: T): void` +::: warning +During runtime this function doesn't do anything. To [enable typechecking](/guide/testing-types#run-typechecking), don't forget to pass down `--typecheck` flag. +::: - You can use this function as an alternative for [`expectTypeOf`](/api/expect-typeof) to easily assert that the argument type is equal to the generic provided. +- **Type:** `(value: T): void` - ```ts - import { assertType } from 'vitest' +You can use this function as an alternative for [`expectTypeOf`](/api/expect-typeof) to easily assert that the argument type is equal to the generic provided. - function concat(a: string, b: string): string - function concat(a: number, b: number): number - function concat(a: string | number, b: string | number): string | number +```ts +import { assertType } from 'vitest' - assertType(concat('a', 'b')) - assertType(concat(1, 2)) - // @ts-expect-error wrong types - assertType(concat('a', 2)) - ``` +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(concat('a', 'b')) +assertType(concat(1, 2)) +// @ts-expect-error wrong types +assertType(concat('a', 2)) +``` diff --git a/docs/api/expect-typeof.md b/docs/api/expect-typeof.md index 4dae16cc2ca0..fab8f40c8ef0 100644 --- a/docs/api/expect-typeof.md +++ b/docs/api/expect-typeof.md @@ -1,505 +1,509 @@ # expectTypeOf +::: warning +During runtime this function doesn't do anything. To [enable typechecking](/guide/testing-types#run-typechecking), don't forget to pass down `--typecheck` flag. +::: + - **Type:** `(a: unknown) => ExpectTypeOf` ## not - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - You can negate all assertions, using `.not` property. +You can negate all assertions, using `.not` property. ## toEqualTypeOf - - **Type:** `(expected: T) => void` +- **Type:** `(expected: T) => void` - This matcher will check if the types are fully equal to each other. This matcher will not fail if two objects have different values, but the same type. It will fail however if an object is missing a property. +This matcher will check if the types are fully equal to each other. This matcher will not fail if two objects have different values, but the same type. It will fail however if an object is missing a property. - ```ts - import { expectTypeOf } from 'vitest' +```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 }>() - ``` +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 - - **Type:** `(expected: T) => void` +- **Type:** `(expected: T) => void` - This matcher checks if expect type extends provided type. It is different from `toEqual` and is more similar to [expect's](/api/expect) `toMatchObject()`. With this matcher, you can check if an object “matches” a type. +This matcher checks if expect type extends provided type. It is different from `toEqual` and is more similar to [expect's](/api/expect) `toMatchObject()`. With this matcher, you can check if an object “matches” a type. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 }) - expectTypeOf().toMatchTypeOf() - expectTypeOf().not.toMatchTypeOf() +expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 }) +expectTypeOf().toMatchTypeOf() +expectTypeOf().not.toMatchTypeOf() ``` ## extract - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - You can use `.extract` to narrow down types for further testing. +You can use `.extract` to narrow down types for further testing. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - type ResponsiveProp = T | T[] | { xs?: T; sm?: T; md?: T } +type ResponsiveProp = T | T[] | { xs?: T; sm?: T; md?: T } - interface CSSProperties { margin?: string; padding?: string } +interface CSSProperties { margin?: string; padding?: string } - function getResponsiveProp(_props: T): ResponsiveProp { - return {} - } +function getResponsiveProp(_props: T): ResponsiveProp { + return {} +} - const cssProperties: CSSProperties = { margin: '1px', padding: '2px' } +const cssProperties: CSSProperties = { margin: '1px', padding: '2px' } - expectTypeOf(getResponsiveProp(cssProperties)) - .extract<{ xs?: any }>() // extracts the last type from a union - .toEqualTypeOf<{ xs?: CSSProperties; sm?: CSSProperties; md?: CSSProperties }>() +expectTypeOf(getResponsiveProp(cssProperties)) + .extract<{ xs?: any }>() // extracts the last type from a union + .toEqualTypeOf<{ xs?: CSSProperties; sm?: CSSProperties; md?: CSSProperties }>() - expectTypeOf(getResponsiveProp(cssProperties)) - .extract() // extracts an array from a union - .toEqualTypeOf() - ``` +expectTypeOf(getResponsiveProp(cssProperties)) + .extract() // extracts an array from a union + .toEqualTypeOf() +``` - ::: warning - If no type is found in the union, `.extract` will return `never`. - ::: +::: warning +If no type is found in the union, `.extract` will return `never`. +::: ## exclude - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - You can use `.exclude` to remove types from a union for further testing. +You can use `.exclude` to remove types from a union for further testing. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - type ResponsiveProp = T | T[] | { xs?: T; sm?: T; md?: T } +type ResponsiveProp = T | T[] | { xs?: T; sm?: T; md?: T } - interface CSSProperties { margin?: string; padding?: string } +interface CSSProperties { margin?: string; padding?: string } - function getResponsiveProp(_props: T): ResponsiveProp { - return {} - } +function getResponsiveProp(_props: T): ResponsiveProp { + return {} +} - const cssProperties: CSSProperties = { margin: '1px', padding: '2px' } +const cssProperties: CSSProperties = { margin: '1px', padding: '2px' } - expectTypeOf(getResponsiveProp(cssProperties)) - .exclude() - .exclude<{ xs?: unknown }>() // or just .exclude() - .toEqualTypeOf() - ``` +expectTypeOf(getResponsiveProp(cssProperties)) + .exclude() + .exclude<{ xs?: unknown }>() // or just .exclude() + .toEqualTypeOf() +``` - ::: warning - If no type is found in the union, `.exclude` will return `never`. - ::: +::: warning +If no type is found in the union, `.exclude` will return `never`. +::: ## returns - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - You can use `.returns` to extract return value of a function type. +You can use `.returns` to extract return value of a function type. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(() => {}).returns.toBeVoid() - expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2]) - ``` +expectTypeOf(() => {}).returns.toBeVoid() +expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2]) +``` - ::: warning - If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. - ::: +::: warning +If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. +::: ## parameters - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - You can extract function arguments with `.parameters` to perform assertions on its value. Parameters are returned as an array. +You can extract function arguments with `.parameters` to perform assertions on its value. Parameters are returned as an array. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - type NoParam = () => void - type HasParam = (s: string) => void +type NoParam = () => void +type HasParam = (s: string) => void - expectTypeOf().parameters.toEqualTypeOf<[]>() - expectTypeOf().parameters.toEqualTypeOf<[string]>() - ``` +expectTypeOf().parameters.toEqualTypeOf<[]>() +expectTypeOf().parameters.toEqualTypeOf<[string]>() +``` - ::: warning - If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. - ::: +::: warning +If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. +::: - ::: tip - You can also use [`.toBeCallableWith`](#tobecallablewith) matcher as a more expressive assertion. - ::: +::: tip +You can also use [`.toBeCallableWith`](#tobecallablewith) matcher as a more expressive assertion. +::: ## parameter - - **Type:** `(nth: number) => ExpectTypeOf` +- **Type:** `(nth: number) => ExpectTypeOf` - You can extract a certain function argument with `.parameter(number)` call to perform other assertions on it. +You can extract a certain function argument with `.parameter(number)` call to perform other assertions on it. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - function foo(a: number, b: string) { - return [a, b] - } +function foo(a: number, b: string) { + return [a, b] +} - expectTypeOf(foo).parameter(0).toBeNumber() - expectTypeOf(foo).parameter(1).toBeString() - ``` +expectTypeOf(foo).parameter(0).toBeNumber() +expectTypeOf(foo).parameter(1).toBeString() +``` - ::: warning - If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. - ::: +::: warning +If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. +::: ## constructorParameters - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - You can extract constructor parameters as an array of values and perform assertions on them with this method. +You can extract constructor parameters as an array of values and perform assertions on them with this method. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(Date).constructorParameters.toEqualTypeOf<[] | [string | number | Date]>() - ``` +expectTypeOf(Date).constructorParameters.toEqualTypeOf<[] | [string | number | Date]>() +``` - ::: warning - If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. - ::: +::: warning +If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. +::: - ::: tip - You can also use [`.toBeConstructibleWith`](#tobeconstructiblewith) matcher as a more expressive assertion. - ::: +::: tip +You can also use [`.toBeConstructibleWith`](#tobeconstructiblewith) matcher as a more expressive assertion. +::: ## instance - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - This property gives access to matchers that can be performed on an instance of the provided class. +This property gives access to matchers that can be performed on an instance of the provided class. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(Date).instance.toHaveProperty('toISOString') - ``` +expectTypeOf(Date).instance.toHaveProperty('toISOString') +``` - ::: warning - If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. - ::: +::: warning +If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. +::: ## items - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - You can get array item type with `.items` to perform further assertions. +You can get array item type with `.items` to perform further assertions. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf([1, 2, 3]).items.toEqualTypeOf() - expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf() - ``` +expectTypeOf([1, 2, 3]).items.toEqualTypeOf() +expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf() +``` ## resolves - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - This matcher extracts resolved value of a `Promise`, so you can perform other assertions on it. +This matcher extracts resolved value of a `Promise`, so you can perform other assertions on it. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - async function asyncFunc() { - return 123 - } +async function asyncFunc() { + return 123 +} - expectTypeOf(asyncFunc).returns.resolves.toBeNumber() - expectTypeOf(Promise.resolve('string')).resolves.toBeString() - ``` +expectTypeOf(asyncFunc).returns.resolves.toBeNumber() +expectTypeOf(Promise.resolve('string')).resolves.toBeString() +``` - ::: warning - If used on a non-promise type, it will return `never`, so you won't be able to chain it with other matchers. - ::: +::: warning +If used on a non-promise type, it will return `never`, so you won't be able to chain it with other matchers. +::: ## guards - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - This matcher extracts guard value (e.g., `v is number`), so you can perform assertions on it. +This matcher extracts guard value (e.g., `v is number`), so you can perform assertions on it. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - function isString(v: any): v is string { - return typeof v === 'string' - } - expectTypeOf(isString).guards.toBeString() - ``` +function isString(v: any): v is string { + return typeof v === 'string' +} +expectTypeOf(isString).guards.toBeString() +``` - ::: warning - Returns `never`, if the value is not a guard function, so you won't be able to chain it with other matchers. - ::: +::: warning +Returns `never`, if the value is not a guard function, so you won't be able to chain it with other matchers. +::: ## asserts - - **Type:** `ExpectTypeOf` +- **Type:** `ExpectTypeOf` - This matcher extracts assert value (e.g., `assert v is number`), so you can perform assertions on it. +This matcher extracts assert value (e.g., `assert v is number`), so you can perform assertions on it. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - function assertNumber(v: any): asserts v is number { - if (typeof v !== 'number') - throw new TypeError('Nope !') - } +function assertNumber(v: any): asserts v is number { + if (typeof v !== 'number') + throw new TypeError('Nope !') +} - expectTypeOf(assertNumber).asserts.toBeNumber() - ``` +expectTypeOf(assertNumber).asserts.toBeNumber() +``` - ::: warning - Returns `never`, if the value is not an assert function, so you won't be able to chain it with other matchers. - ::: +::: warning +Returns `never`, if the value is not an assert function, so you won't be able to chain it with other matchers. +::: ## toBeAny - - **Type:** `() => void` +- **Type:** `() => void` - With this matcher you can check, if provided type is `any` type. If the type is too specific, the test will fail. +With this matcher you can check, if provided type is `any` type. If the type is too specific, the test will fail. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf().toBeAny() - expectTypeOf({} as any).toBeAny() - expectTypeOf('string').not.toBeAny() - ``` +expectTypeOf().toBeAny() +expectTypeOf({} as any).toBeAny() +expectTypeOf('string').not.toBeAny() +``` ## toBeUnknown - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is `unknown` type. +This matcher checks, if provided type is `unknown` type. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf().toBeUnknown() - expectTypeOf({} as unknown).toBeUnknown() - expectTypeOf('string').not.toBeUnknown() - ``` +expectTypeOf().toBeUnknown() +expectTypeOf({} as unknown).toBeUnknown() +expectTypeOf('string').not.toBeUnknown() +``` ## toBeNever - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is a `never` type. +This matcher checks, if provided type is a `never` type. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf().toBeNever() - expectTypeOf((): never => {}).returns.toBeNever() - ``` +expectTypeOf().toBeNever() +expectTypeOf((): never => {}).returns.toBeNever() +``` ## toBeFunction - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is a `function`. +This matcher checks, if provided type is a `function`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(42).not.toBeFunction() - expectTypeOf((): never => {}).toBeFunction() - ``` +expectTypeOf(42).not.toBeFunction() +expectTypeOf((): never => {}).toBeFunction() +``` ## toBeObject - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is an `object`. +This matcher checks, if provided type is an `object`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(42).not.toBeObject() - expectTypeOf({}).toBeObject() - ``` +expectTypeOf(42).not.toBeObject() +expectTypeOf({}).toBeObject() +``` ## toBeArray - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is `Array`. +This matcher checks, if provided type is `Array`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(42).not.toBeArray() - expectTypeOf([]).toBeArray() - expectTypeOf([1, 2]).toBeArray() - expectTypeOf([{}, 42]).toBeArray() - ``` +expectTypeOf(42).not.toBeArray() +expectTypeOf([]).toBeArray() +expectTypeOf([1, 2]).toBeArray() +expectTypeOf([{}, 42]).toBeArray() +``` ## toBeString - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is a `string`. +This matcher checks, if provided type is a `string`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(42).not.toBeString() - expectTypeOf('').toBeString() - expectTypeOf('a').toBeString() - ``` +expectTypeOf(42).not.toBeString() +expectTypeOf('').toBeString() +expectTypeOf('a').toBeString() +``` ## toBeBoolean - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is `boolean`. +This matcher checks, if provided type is `boolean`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(42).not.toBeBoolean() - expectTypeOf(true).toBeBoolean() - expectTypeOf().toBeBoolean() - ``` +expectTypeOf(42).not.toBeBoolean() +expectTypeOf(true).toBeBoolean() +expectTypeOf().toBeBoolean() +``` ## toBeVoid - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is `void`. +This matcher checks, if provided type is `void`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(() => {}).returns.toBeVoid() - expectTypeOf().toBeVoid() - ``` +expectTypeOf(() => {}).returns.toBeVoid() +expectTypeOf().toBeVoid() +``` ## toBeSymbol - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is a `symbol`. +This matcher checks, if provided type is a `symbol`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(Symbol(1)).toBeSymbol() - expectTypeOf().toBeSymbol() - ``` +expectTypeOf(Symbol(1)).toBeSymbol() +expectTypeOf().toBeSymbol() +``` ## toBeNull - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is `null`. +This matcher checks, if provided type is `null`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(null).toBeNull() - expectTypeOf().toBeNull() - expectTypeOf(undefined).not.toBeNull() - ``` +expectTypeOf(null).toBeNull() +expectTypeOf().toBeNull() +expectTypeOf(undefined).not.toBeNull() +``` ## toBeUndefined - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if provided type is `undefined`. +This matcher checks, if provided type is `undefined`. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(undefined).toBeUndefined() - expectTypeOf().toBeUndefined() - expectTypeOf(null).not.toBeUndefined() - ``` +expectTypeOf(undefined).toBeUndefined() +expectTypeOf().toBeUndefined() +expectTypeOf(null).not.toBeUndefined() +``` ## toBeNullable - - **Type:** `() => void` +- **Type:** `() => void` - This matcher checks, if you can use `null` or `undefined` with provided type. +This matcher checks, if you can use `null` or `undefined` with provided type. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf<1 | undefined>().toBeNullable() - expectTypeOf<1 | null>().toBeNullable() - expectTypeOf<1 | undefined | null>().toBeNullable() - ``` +expectTypeOf<1 | undefined>().toBeNullable() +expectTypeOf<1 | null>().toBeNullable() +expectTypeOf<1 | undefined | null>().toBeNullable() +``` ## toBeCallableWith - - **Type:** `() => void` +- **Type:** `() => void` - This matcher ensures you can call provided function with a set of parameters. +This matcher ensures you can call provided function with a set of parameters. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - type NoParam = () => void - type HasParam = (s: string) => void +type NoParam = () => void +type HasParam = (s: string) => void - expectTypeOf().toBeCallableWith() - expectTypeOf().toBeCallableWith('some string') - ``` +expectTypeOf().toBeCallableWith() +expectTypeOf().toBeCallableWith('some string') +``` - ::: warning - If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. - ::: +::: warning +If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. +::: ## toBeConstructibleWith - - **Type:** `() => void` +- **Type:** `() => void` - This matcher ensures you can create a new instance with a set of constructor parameters. +This matcher ensures you can create a new instance with a set of constructor parameters. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - expectTypeOf(Date).toBeConstructibleWith(new Date()) - expectTypeOf(Date).toBeConstructibleWith('01-01-2000') - ``` +expectTypeOf(Date).toBeConstructibleWith(new Date()) +expectTypeOf(Date).toBeConstructibleWith('01-01-2000') +``` - ::: warning - If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. - ::: +::: warning +If used on a non-function type, it will return `never`, so you won't be able to chain it with other matchers. +::: ## toHaveProperty - - **Type:** `(property: K) => ExpectTypeOf` +- **Type:** `(property: K) => ExpectTypeOf` - 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. +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. - ```ts - import { expectTypeOf } from 'vitest' +```ts +import { expectTypeOf } from 'vitest' - const obj = { a: 1, b: '' } +const obj = { a: 1, b: '' } - expectTypeOf(obj).toHaveProperty('a') - expectTypeOf(obj).not.toHaveProperty('c') +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() - ``` +expectTypeOf(obj).toHaveProperty('a').toBeNumber() +expectTypeOf(obj).toHaveProperty('b').toBeString() +expectTypeOf(obj).toHaveProperty('a').not.toBeString() +``` diff --git a/docs/guide/testing-types.md b/docs/guide/testing-types.md index ba7e0bf5106a..147a0035ceb2 100644 --- a/docs/guide/testing-types.md +++ b/docs/guide/testing-types.md @@ -111,7 +111,7 @@ assertType(answr) // ## Run typechecking -To enabled typechecking, just add `--typecheck` flag to your Vitest command in `package.json`: +Since Vitest 1.0, to enabled typechecking, just add [`--typecheck`](/config/#typecheck) flag to your Vitest command in `package.json`: ```json { @@ -123,15 +123,19 @@ To enabled typechecking, just add `--typecheck` flag to your Vitest command in ` Now you can run typecheck: -```sh -# npm +::: code-group +```bash [npm] npm run test - -# yarn +``` +```bash [yarn] yarn test - -# pnpm +``` +```bash [pnpm] pnpm run test ``` +```bash [bun] +bun test +``` +::: Vitest uses `tsc --noEmit` or `vue-tsc --noEmit`, depending on your configuration, so you can remove these scripts from your pipeline.