docs: update Testing Types docs to use non-deprecated expect-type API and add toMatchObjectType documentation#8397
Conversation
✅ Deploy Preview for vitest-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
…chTypeOf Co-authored-by: sheremet-va <[email protected]>
|
Could you also add documentation to ## toMatchObjectType
- **Type:** `<T>() => void`
This matcher performs a strict check on object types, ensuring that the expected type matches the provided object type. It's stricter than [`toExtend`](#toextend) and is the recommended choice when working with object types as it's more likely to catch issues like readonly properties.
```ts
import { expectTypeOf } from 'vitest'
expectTypeOf({ a: 1, b: 2 }).toMatchObjectType<{ a: number }>() // preferred
expectTypeOf({ a: 1, b: 2 }).toExtend<{ a: number }>() // works but less strict
// Supports nested object checking
const user = {
name: 'John',
address: { city: 'New York', zip: '10001' }
}
expectTypeOf(user).toMatchObjectType<{ name: string; address: { city: string } }>()
```
::: warning
This matcher only works with plain object types. It will fail for union types and other complex types. For those cases, use [`toExtend`](#toextend) instead.
::: |
|
@copilot add documentation to ## toMatchObjectType
- **Type:** `<T>() => void`
This matcher performs a strict check on object types, ensuring that the expected type matches the provided object type. It's stricter than [`toExtend`](#toextend) and is the recommended choice when working with object types as it's more likely to catch issues like readonly properties.
```ts
import { expectTypeOf } from 'vitest'
expectTypeOf({ a: 1, b: 2 }).toMatchObjectType<{ a: number }>() // preferred
expectTypeOf({ a: 1, b: 2 }).toExtend<{ a: number }>() // works but less strict
// Supports nested object checking
const user = {
name: 'John',
address: { city: 'New York', zip: '10001' }
}
expectTypeOf(user).toMatchObjectType<{ name: string; address: { city: string } }>()
```
::: warning
This matcher only works with plain object types. It will fail for union types and other complex types. For those cases, use [`toExtend`](#toextend) instead.
::: |
Co-authored-by: sheremet-va <[email protected]>
... Added the |
|
Looks good to me - thanks! |
docs/api/expect-typeof.md
Outdated
| import { expectTypeOf } from 'vitest' | ||
|
|
||
| expectTypeOf({ a: 1, b: 2 }).toMatchObjectType<{ a: number }>() // preferred | ||
| expectTypeOf({ a: 1, b: 2 }).toExtend<{ a: number }>() // works but less strict |
Co-authored-by: sheremet-va <[email protected]>
The Testing Types documentation was using the deprecated
toMatchTypeOfmatcher from expect-type, which has been deprecated since v1.2.0 in favor oftoExtend. This PR updates all documentation to use the current API and adds comprehensive documentation fortoMatchObjectType.Changes Made
Updated deprecated API usage:
toMatchTypeOfwithtoExtendin code examples across/docs/guide/testing-types.mdand/docs/guide/features.mdtoMatchTypeOfdocumentation with clear migration guidance/test/config/test/config-types.test-d.tsto use current APIAdded new documentation:
toExtendsection with examples and usage patternstoMatchObjectTypesection with detailed documentation, examples, and usage guidelinestoExtendandtoMatchObjectTypeshowing when to use eachAPI Changes Documented
Before:
After:
The
toMatchObjectTypematcher provides stricter checking for object types and is recommended when working with plain objects as it's more likely to catch issues like readonly properties. For union types and complex types,toExtendshould be used instead.Fixes #8387.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.