|
1 | | -import type { ChaiPlugin, MatcherState, SyncExpectationResult } from '@vitest/expect' |
| 1 | +import type { AsyncExpectationResult, ChaiPlugin, MatcherState, SyncExpectationResult } from '@vitest/expect' |
2 | 2 | import type { Test } from '@vitest/runner' |
3 | 3 | import { chai, createAssertionMessage, equals, iterableEquality, recordAsyncExpect, subsetEquality, wrapAssertion } from '@vitest/expect' |
4 | 4 | import { getNames } from '@vitest/runner/utils' |
@@ -268,100 +268,109 @@ function assertMatchResult(result: SyncExpectationResult): void { |
268 | 268 | } |
269 | 269 |
|
270 | 270 | /** |
271 | | - * Composable for building custom snapshot matchers via `expect.extend`. |
272 | | - * Call with `this` bound to the matcher state. Returns `{ pass, message }` |
273 | | - * compatible with the custom matcher return contract. |
274 | | - * |
275 | | - * @example |
276 | | - * ```ts |
277 | | - * import { toMatchSnapshot } from 'vitest/runtime' |
278 | | - * |
279 | | - * expect.extend({ |
280 | | - * toMatchTrimmedSnapshot(received: string) { |
281 | | - * return toMatchSnapshot.call(this, received.slice(0, 10)) |
282 | | - * }, |
283 | | - * }) |
284 | | - * ``` |
| 271 | + * Composable snapshot matcher helpers for building custom snapshot matchers |
| 272 | + * with `expect.extend`. |
285 | 273 | * |
286 | 274 | * @experimental |
287 | 275 | * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers |
288 | 276 | */ |
289 | | -export function toMatchSnapshot( |
290 | | - this: MatcherState, |
291 | | - received: unknown, |
292 | | - propertiesOrHint?: object | string, |
293 | | - hint?: string, |
294 | | -): SyncExpectationResult { |
295 | | - return toMatchSnapshotImpl({ |
296 | | - assertion: this.__vitest_assertion__, |
297 | | - received, |
298 | | - ...normalizeArguments(propertiesOrHint, hint), |
299 | | - }) |
300 | | -} |
| 277 | +export const Snapshots = { |
| 278 | + /** |
| 279 | + * Composable for building custom snapshot matchers via `expect.extend`. |
| 280 | + * Call with `this` bound to the matcher state. Returns `{ pass, message }` |
| 281 | + * compatible with the custom matcher return contract. |
| 282 | + * |
| 283 | + * @example |
| 284 | + * ```ts |
| 285 | + * import { Snapshots } from 'vitest/runtime' |
| 286 | + * |
| 287 | + * expect.extend({ |
| 288 | + * toMatchTrimmedSnapshot(received: string) { |
| 289 | + * return Snapshots.toMatchSnapshot.call(this, received.slice(0, 10)) |
| 290 | + * }, |
| 291 | + * }) |
| 292 | + * ``` |
| 293 | + * |
| 294 | + * @experimental |
| 295 | + * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers |
| 296 | + */ |
| 297 | + toMatchSnapshot( |
| 298 | + this: MatcherState, |
| 299 | + received: unknown, |
| 300 | + propertiesOrHint?: object | string, |
| 301 | + hint?: string, |
| 302 | + ): SyncExpectationResult { |
| 303 | + return toMatchSnapshotImpl({ |
| 304 | + assertion: this.__vitest_assertion__, |
| 305 | + received, |
| 306 | + ...normalizeArguments(propertiesOrHint, hint), |
| 307 | + }) |
| 308 | + }, |
301 | 309 |
|
302 | | -/** |
303 | | - * Composable for building custom inline snapshot matchers via `expect.extend`. |
304 | | - * Call with `this` bound to the matcher state. Returns `{ pass, message }` |
305 | | - * compatible with the custom matcher return contract. |
306 | | - * |
307 | | - * @example |
308 | | - * ```ts |
309 | | - * import { toMatchInlineSnapshot } from 'vitest/runtime' |
310 | | - * |
311 | | - * expect.extend({ |
312 | | - * toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) { |
313 | | - * return toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot) |
314 | | - * }, |
315 | | - * }) |
316 | | - * ``` |
317 | | - * |
318 | | - * @experimental |
319 | | - * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers |
320 | | - */ |
321 | | -export function toMatchInlineSnapshot( |
322 | | - this: MatcherState, |
323 | | - received: unknown, |
324 | | - propertiesOrInlineSnapshot?: object | string, |
325 | | - inlineSnapshotOrHint?: string, |
326 | | - hint?: string, |
327 | | -): SyncExpectationResult { |
328 | | - return toMatchSnapshotImpl({ |
329 | | - assertion: this.__vitest_assertion__, |
330 | | - received, |
331 | | - isInline: true, |
332 | | - ...normalizeInlineArguments(propertiesOrInlineSnapshot, inlineSnapshotOrHint, hint), |
333 | | - }) |
334 | | -} |
| 310 | + /** |
| 311 | + * Composable for building custom inline snapshot matchers via `expect.extend`. |
| 312 | + * Call with `this` bound to the matcher state. Returns `{ pass, message }` |
| 313 | + * compatible with the custom matcher return contract. |
| 314 | + * |
| 315 | + * @example |
| 316 | + * ```ts |
| 317 | + * import { Snapshots } from 'vitest/runtime' |
| 318 | + * |
| 319 | + * expect.extend({ |
| 320 | + * toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) { |
| 321 | + * return Snapshots.toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot) |
| 322 | + * }, |
| 323 | + * }) |
| 324 | + * ``` |
| 325 | + * |
| 326 | + * @experimental |
| 327 | + * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers |
| 328 | + */ |
| 329 | + toMatchInlineSnapshot( |
| 330 | + this: MatcherState, |
| 331 | + received: unknown, |
| 332 | + propertiesOrInlineSnapshot?: object | string, |
| 333 | + inlineSnapshotOrHint?: string, |
| 334 | + hint?: string, |
| 335 | + ): SyncExpectationResult { |
| 336 | + return toMatchSnapshotImpl({ |
| 337 | + assertion: this.__vitest_assertion__, |
| 338 | + received, |
| 339 | + isInline: true, |
| 340 | + ...normalizeInlineArguments(propertiesOrInlineSnapshot, inlineSnapshotOrHint, hint), |
| 341 | + }) |
| 342 | + }, |
335 | 343 |
|
336 | | -/** |
337 | | - * Composable for building custom file snapshot matchers via `expect.extend`. |
338 | | - * Call with `this` bound to the matcher state. Returns a `Promise<{ pass, message }>` |
339 | | - * compatible with the custom matcher return contract. |
340 | | - * |
341 | | - * @example |
342 | | - * ```ts |
343 | | - * import { toMatchFileSnapshot } from 'vitest/runtime' |
344 | | - * |
345 | | - * expect.extend({ |
346 | | - * async toMatchTrimmedFileSnapshot(received: string, file: string) { |
347 | | - * return toMatchFileSnapshot.call(this, received.slice(0, 10), file) |
348 | | - * }, |
349 | | - * }) |
350 | | - * ``` |
351 | | - * |
352 | | - * @experimental |
353 | | - * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers |
354 | | - */ |
355 | | -export function toMatchFileSnapshot( |
356 | | - this: MatcherState, |
357 | | - received: unknown, |
358 | | - filepath: string, |
359 | | - hint?: string, |
360 | | -): Promise<SyncExpectationResult> { |
361 | | - return toMatchFileSnapshotImpl({ |
362 | | - assertion: this.__vitest_assertion__, |
363 | | - received, |
364 | | - filepath, |
365 | | - hint, |
366 | | - }) |
| 344 | + /** |
| 345 | + * Composable for building custom file snapshot matchers via `expect.extend`. |
| 346 | + * Call with `this` bound to the matcher state. Returns a `Promise<{ pass, message }>` |
| 347 | + * compatible with the custom matcher return contract. |
| 348 | + * |
| 349 | + * @example |
| 350 | + * ```ts |
| 351 | + * import { Snapshots } from 'vitest/runtime' |
| 352 | + * |
| 353 | + * expect.extend({ |
| 354 | + * async toMatchTrimmedFileSnapshot(received: string, file: string) { |
| 355 | + * return Snapshots.toMatchFileSnapshot.call(this, received.slice(0, 10), file) |
| 356 | + * }, |
| 357 | + * }) |
| 358 | + * ``` |
| 359 | + * |
| 360 | + * @experimental |
| 361 | + * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers |
| 362 | + */ |
| 363 | + toMatchFileSnapshot( |
| 364 | + this: MatcherState, |
| 365 | + received: unknown, |
| 366 | + filepath: string, |
| 367 | + hint?: string, |
| 368 | + ): AsyncExpectationResult { |
| 369 | + return toMatchFileSnapshotImpl({ |
| 370 | + assertion: this.__vitest_assertion__, |
| 371 | + received, |
| 372 | + filepath, |
| 373 | + hint, |
| 374 | + }) |
| 375 | + }, |
367 | 376 | } |
0 commit comments