chore: use pretty-format types directly in jest typings (#41895)

This commit is contained in:
Simen Bekkhus
2020-01-29 13:45:40 -08:00
committed by GitHub
parent c4b6c8f6c3
commit 57dd2620f4
3 changed files with 28 additions and 76 deletions
+1 -43
View File
@@ -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 {
/**
+25 -32
View File
@@ -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]) {
+2 -1
View File
@@ -1,6 +1,7 @@
{
"private": true,
"dependencies": {
"jest-diff": "^25.1.0"
"jest-diff": "^25.1.0",
"pretty-format": "^25.1.0"
}
}