index.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. import type {Config} from '@jest/types';
  8. import type {MatcherContext} from 'expect';
  9. import type {MatcherFunctionWithContext} from 'expect';
  10. import {Plugin as Plugin_2} from 'pretty-format';
  11. import {Plugins} from 'pretty-format';
  12. import type {PrettyFormatOptions} from 'pretty-format';
  13. export declare const addSerializer: (plugin: Plugin_2) => void;
  14. export declare const buildSnapshotResolver: (
  15. config: Config.ProjectConfig,
  16. localRequire?: Promise<LocalRequire> | LocalRequire,
  17. ) => Promise<SnapshotResolver>;
  18. export declare const cleanup: (
  19. fileSystem: FileSystem_2,
  20. update: Config.SnapshotUpdateState,
  21. snapshotResolver: SnapshotResolver,
  22. testPathIgnorePatterns?: Config.ProjectConfig['testPathIgnorePatterns'],
  23. ) => {
  24. filesRemoved: number;
  25. filesRemovedList: Array<string>;
  26. };
  27. export declare interface Context extends MatcherContext {
  28. snapshotState: SnapshotState;
  29. }
  30. export declare const EXTENSION = 'snap';
  31. declare interface FileSystem_2 {
  32. exists(path: string): boolean;
  33. matchFiles(pattern: RegExp | string): Array<string>;
  34. }
  35. export declare const getSerializers: () => Plugins;
  36. export declare const isSnapshotPath: (path: string) => boolean;
  37. declare type LocalRequire = (module: string) => unknown;
  38. declare type SaveStatus = {
  39. deleted: boolean;
  40. saved: boolean;
  41. };
  42. declare type SnapshotFormat = Omit<PrettyFormatOptions, 'compareKeys'>;
  43. export declare interface SnapshotMatchers<R extends void | Promise<void>, T> {
  44. /**
  45. * This ensures that a value matches the most recent snapshot with property matchers.
  46. * Check out [the Snapshot Testing guide](https://jestjs.io/docs/snapshot-testing) for more information.
  47. */
  48. toMatchSnapshot(hint?: string): R;
  49. /**
  50. * This ensures that a value matches the most recent snapshot.
  51. * Check out [the Snapshot Testing guide](https://jestjs.io/docs/snapshot-testing) for more information.
  52. */
  53. toMatchSnapshot<U extends Record<keyof T, unknown>>(
  54. propertyMatchers: Partial<U>,
  55. hint?: string,
  56. ): R;
  57. /**
  58. * This ensures that a value matches the most recent snapshot with property matchers.
  59. * Instead of writing the snapshot value to a .snap file, it will be written into the source code automatically.
  60. * Check out [the Snapshot Testing guide](https://jestjs.io/docs/snapshot-testing) for more information.
  61. */
  62. toMatchInlineSnapshot(snapshot?: string): R;
  63. /**
  64. * This ensures that a value matches the most recent snapshot with property matchers.
  65. * Instead of writing the snapshot value to a .snap file, it will be written into the source code automatically.
  66. * Check out [the Snapshot Testing guide](https://jestjs.io/docs/snapshot-testing) for more information.
  67. */
  68. toMatchInlineSnapshot<U extends Record<keyof T, unknown>>(
  69. propertyMatchers: Partial<U>,
  70. snapshot?: string,
  71. ): R;
  72. /**
  73. * Used to test that a function throws a error matching the most recent snapshot when it is called.
  74. */
  75. toThrowErrorMatchingSnapshot(hint?: string): R;
  76. /**
  77. * Used to test that a function throws a error matching the most recent snapshot when it is called.
  78. * Instead of writing the snapshot value to a .snap file, it will be written into the source code automatically.
  79. */
  80. toThrowErrorMatchingInlineSnapshot(snapshot?: string): R;
  81. }
  82. declare type SnapshotMatchOptions = {
  83. readonly testName: string;
  84. readonly received: unknown;
  85. readonly key?: string;
  86. readonly inlineSnapshot?: string;
  87. readonly isInline: boolean;
  88. readonly error?: Error;
  89. };
  90. export declare type SnapshotResolver = {
  91. /** Resolves from `testPath` to snapshot path. */
  92. resolveSnapshotPath(testPath: string, snapshotExtension?: string): string;
  93. /** Resolves from `snapshotPath` to test path. */
  94. resolveTestPath(snapshotPath: string, snapshotExtension?: string): string;
  95. /** Example test path, used for preflight consistency check of the implementation above. */
  96. testPathForConsistencyCheck: string;
  97. };
  98. declare type SnapshotReturnOptions = {
  99. readonly actual: string;
  100. readonly count: number;
  101. readonly expected?: string;
  102. readonly key: string;
  103. readonly pass: boolean;
  104. };
  105. export declare class SnapshotState {
  106. private _counters;
  107. private _dirty;
  108. private _index;
  109. private readonly _updateSnapshot;
  110. private _snapshotData;
  111. private readonly _initialData;
  112. private readonly _snapshotPath;
  113. private _inlineSnapshots;
  114. private readonly _uncheckedKeys;
  115. private readonly _prettierPath;
  116. private readonly _rootDir;
  117. readonly snapshotFormat: SnapshotFormat;
  118. added: number;
  119. expand: boolean;
  120. matched: number;
  121. unmatched: number;
  122. updated: number;
  123. constructor(snapshotPath: string, options: SnapshotStateOptions);
  124. markSnapshotsAsCheckedForTest(testName: string): void;
  125. private _addSnapshot;
  126. clear(): void;
  127. save(): SaveStatus;
  128. getUncheckedCount(): number;
  129. getUncheckedKeys(): Array<string>;
  130. removeUncheckedKeys(): void;
  131. match({
  132. testName,
  133. received,
  134. key,
  135. inlineSnapshot,
  136. isInline,
  137. error,
  138. }: SnapshotMatchOptions): SnapshotReturnOptions;
  139. fail(testName: string, _received: unknown, key?: string): string;
  140. }
  141. declare type SnapshotStateOptions = {
  142. readonly updateSnapshot: Config.SnapshotUpdateState;
  143. readonly prettierPath?: string | null;
  144. readonly expand?: boolean;
  145. readonly snapshotFormat: SnapshotFormat;
  146. readonly rootDir: string;
  147. };
  148. export declare const toMatchInlineSnapshot: MatcherFunctionWithContext<
  149. Context,
  150. [propertiesOrSnapshot?: object | string, inlineSnapshot?: string]
  151. >;
  152. export declare const toMatchSnapshot: MatcherFunctionWithContext<
  153. Context,
  154. [propertiesOrHint?: object | string, hint?: string]
  155. >;
  156. export declare const toThrowErrorMatchingInlineSnapshot: MatcherFunctionWithContext<
  157. Context,
  158. [inlineSnapshot?: string, fromPromise?: boolean]
  159. >;
  160. export declare const toThrowErrorMatchingSnapshot: MatcherFunctionWithContext<
  161. Context,
  162. [hint?: string, fromPromise?: boolean]
  163. >;
  164. export {};