diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 8f0c97935a..75823aba17 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -526,49 +526,7 @@ declare namespace jest { message: () => string; } - interface SnapshotSerializerOptions { - callToJSON?: boolean; - edgeSpacing?: string; - spacing?: string; - escapeRegex?: boolean; - highlight?: boolean; - indent?: number; - maxDepth?: number; - min?: boolean; - plugins?: SnapshotSerializerPlugin[]; - printFunctionName?: boolean; - theme?: SnapshotSerializerOptionsTheme; - - // see https://github.com/facebook/jest/blob/e56103cf142d2e87542ddfb6bd892bcee262c0e6/types/PrettyFormat.js - } - interface SnapshotSerializerOptionsTheme { - comment?: string; - content?: string; - prop?: string; - tag?: string; - value?: string; - } - interface SnapshotSerializerColor { - close: string; - open: string; - } - interface SnapshotSerializerColors { - comment: SnapshotSerializerColor; - content: SnapshotSerializerColor; - prop: SnapshotSerializerColor; - tag: SnapshotSerializerColor; - value: SnapshotSerializerColor; - } - interface SnapshotSerializerPlugin { - print( - val: any, - serialize: (val: any) => string, - indent: (str: string) => string, - opts: SnapshotSerializerOptions, - colors: SnapshotSerializerColors - ): string; - test(val: any): boolean; - } + type SnapshotSerializerPlugin = import('pretty-format').Plugin; interface InverseAsymmetricMatchers { /** diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 559b6848a3..264bc24639 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -622,64 +622,57 @@ expect.addSnapshotSerializer({ }); expect.addSnapshotSerializer({ - print(value, serialize, indent, opts, colors) { + serialize(value, config, indentation, depth, refs, printer) { let result = ''; - if (opts.callToJSON !== undefined && opts.callToJSON) { + if (config.callToJSON !== undefined && config.callToJSON) { result += ' '; } - result += opts.edgeSpacing; - result += opts.spacing; + result += config.spacingInner; + result += config.spacingOuter; - if (opts.escapeRegex !== undefined && opts.escapeRegex) { + if (config.escapeRegex !== undefined && config.escapeRegex) { result += ' '; } - if (opts.indent !== undefined) { - for (let i = 0; i < opts.indent; i += 1) { - result += '\t'; - } + if (indentation !== undefined) { + result += indentation; } - if (opts.maxDepth !== undefined) { - result = result.substring(0, opts.maxDepth); + if (config.maxDepth !== undefined) { + result = result.substring(0, config.maxDepth); } - if (opts.min !== undefined && opts.min) { + if (config.min !== undefined && config.min) { result += ' '; } - if (opts.plugins !== undefined) { - for (const plugin of opts.plugins) { + if (config.plugins !== undefined) { + for (const plugin of config.plugins) { expect.addSnapshotSerializer(plugin); } } - if (opts.printFunctionName !== undefined && opts.printFunctionName) { + if (config.printFunctionName !== undefined && config.printFunctionName) { result += ' '; } - if (opts.theme) { - if (opts.theme.comment !== undefined) { - result += opts.theme.comment; - } + return result; + }, + test: (value: {}) => value === value, +}); - if (opts.theme.content !== undefined) { - result += opts.theme.content; - } +// old API +expect.addSnapshotSerializer({ + print(value, serialize, indent, opts, colors) { + let result = ''; - if (opts.theme.prop !== undefined) { - result += opts.theme.prop; - } + result += opts.edgeSpacing; + result += opts.spacing; - if (opts.theme.tag !== undefined) { - result += opts.theme.tag; - } - - if (opts.theme.value !== undefined) { - result += opts.theme.value; - } + if (opts.min !== undefined && opts.min) { + result += ' '; } for (const color of [colors.comment, colors.content, colors.prop, colors.tag, colors.value]) { diff --git a/types/jest/package.json b/types/jest/package.json index 738eb49dfe..b317dd2fcf 100644 --- a/types/jest/package.json +++ b/types/jest/package.json @@ -1,6 +1,7 @@ { "private": true, "dependencies": { - "jest-diff": "^25.1.0" + "jest-diff": "^25.1.0", + "pretty-format": "^25.1.0" } }