diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ae8ea7521dd..3a098e0daece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ ### Chore & Maintenance - `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808), [#7855](https://github.com/facebook/jest/pull/7855), [#7951](https://github.com/facebook/jest/pull/7951)) -- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809)) +- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809), [#7809](https://github.com/facebook/jest/pull/7972)) - `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820)) - `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818)) - `[jest-regex-util]`: Migrate to TypeScript ([#7822](https://github.com/facebook/jest/pull/7822)) diff --git a/packages/jest-snapshot/src/mock_serializer.ts b/packages/jest-snapshot/src/mock_serializer.ts index fbfaf7a1448c..c538e8468548 100644 --- a/packages/jest-snapshot/src/mock_serializer.ts +++ b/packages/jest-snapshot/src/mock_serializer.ts @@ -5,9 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -import {PrettyFormat} from '@jest/types'; +import {NewPlugin} from 'pretty-format'; -export const serialize: PrettyFormat.NewPlugin['serialize'] = ( +export const serialize: NewPlugin['serialize'] = ( val, config, indentation, @@ -42,9 +42,8 @@ export const serialize: PrettyFormat.NewPlugin['serialize'] = ( return '[MockFunction' + nameString + ']' + callsString; }; -export const test: PrettyFormat.NewPlugin['test'] = val => - val && !!val._isMockFunction; +export const test: NewPlugin['test'] = val => val && !!val._isMockFunction; -const plugin: PrettyFormat.NewPlugin = {serialize, test}; +const plugin: NewPlugin = {serialize, test}; export default plugin; diff --git a/packages/jest-snapshot/src/plugins.ts b/packages/jest-snapshot/src/plugins.ts index ebf50c0eb651..bd604c0c4286 100644 --- a/packages/jest-snapshot/src/plugins.ts +++ b/packages/jest-snapshot/src/plugins.ts @@ -5,8 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import prettyFormat from 'pretty-format'; -import {PrettyFormat} from '@jest/types'; +import prettyFormat, {Plugin, Plugins} from 'pretty-format'; import jestMockSerializer from './mock_serializer'; @@ -19,7 +18,7 @@ const { AsymmetricMatcher, } = prettyFormat.plugins; -let PLUGINS: PrettyFormat.Plugins = [ +let PLUGINS: Plugins = [ ReactTestComponent, ReactElement, DOMElement, @@ -30,7 +29,7 @@ let PLUGINS: PrettyFormat.Plugins = [ ]; // Prepend to list so the last added is the first tested. -export const addSerializer = (plugin: PrettyFormat.Plugin) => { +export const addSerializer = (plugin: Plugin) => { PLUGINS = [plugin].concat(PLUGINS); }; diff --git a/packages/jest-types/src/PrettyFormat.ts b/packages/jest-types/src/PrettyFormat.ts deleted file mode 100644 index 89d06d5bd418..000000000000 --- a/packages/jest-types/src/PrettyFormat.ts +++ /dev/null @@ -1,117 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -export type Colors = { - comment: {close: string; open: string}; - content: {close: string; open: string}; - prop: {close: string; open: string}; - tag: {close: string; open: string}; - value: {close: string; open: string}; -}; -type Indent = (arg0: string) => string; -export type Refs = Array; -type Print = (arg0: any) => string; - -export type Theme = { - comment: string; - content: string; - prop: string; - tag: string; - value: string; -}; - -type ThemeReceived = { - comment?: string; - content?: string; - prop?: string; - tag?: string; - value?: string; -}; - -export type Options = { - callToJSON: boolean; - escapeRegex: boolean; - escapeString: boolean; - highlight: boolean; - indent: number; - maxDepth: number; - min: boolean; - plugins: Plugins; - printFunctionName: boolean; - theme: Theme; -}; - -export type OptionsReceived = { - callToJSON?: boolean; - escapeRegex?: boolean; - escapeString?: boolean; - highlight?: boolean; - indent?: number; - maxDepth?: number; - min?: boolean; - plugins?: Plugins; - printFunctionName?: boolean; - theme?: ThemeReceived; -}; - -export type Config = { - callToJSON: boolean; - colors: Colors; - escapeRegex: boolean; - escapeString: boolean; - indent: string; - maxDepth: number; - min: boolean; - plugins: Plugins; - printFunctionName: boolean; - spacingInner: string; - spacingOuter: string; -}; - -export type Printer = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - hasCalledToJSON?: boolean, -) => string; - -type Test = (arg0: any) => boolean; - -export type NewPlugin = { - serialize: ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, - ) => string; - test: Test; -}; - -type PluginOptions = { - edgeSpacing: string; - min: boolean; - spacing: string; -}; - -type OldPlugin = { - print: ( - val: any, - print: Print, - indent: Indent, - options: PluginOptions, - colors: Colors, - ) => string; - test: Test; -}; - -export type Plugin = NewPlugin | OldPlugin; - -export type Plugins = Array; diff --git a/packages/jest-types/src/index.ts b/packages/jest-types/src/index.ts index f233cb2eb733..c99939515bae 100644 --- a/packages/jest-types/src/index.ts +++ b/packages/jest-types/src/index.ts @@ -8,19 +8,9 @@ import * as Config from './Config'; import * as Console from './Console'; import * as Matchers from './Matchers'; -import * as PrettyFormat from './PrettyFormat'; import * as SourceMaps from './SourceMaps'; import * as TestResult from './TestResult'; import * as Global from './Global'; import * as Environment from './Environment'; -export { - Config, - Console, - Matchers, - PrettyFormat, - SourceMaps, - TestResult, - Global, - Environment, -}; +export {Config, Console, Matchers, SourceMaps, TestResult, Global, Environment}; diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index 3a496046fdff..88455e20d7a7 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -6,17 +6,7 @@ */ import style from 'ansi-styles'; -import { - Colors, - Config, - Options, - OptionsReceived, - NewPlugin, - Plugin, - Plugins, - Refs, - Theme, -} from './types'; +import * as PrettyFormat from './types'; import { printIteratorEntries, @@ -179,10 +169,10 @@ function printBasicValue( */ function printComplexValue( val: any, - config: Config, + config: PrettyFormat.Config, indentation: string, depth: number, - refs: Refs, + refs: PrettyFormat.Refs, hasCalledToJSON?: boolean, ): string { if (refs.indexOf(val) !== -1) { @@ -261,17 +251,19 @@ function printComplexValue( '}'; } -function isNewPlugin(plugin: Plugin): plugin is NewPlugin { - return (plugin as NewPlugin).serialize != null; +function isNewPlugin( + plugin: PrettyFormat.Plugin, +): plugin is PrettyFormat.NewPlugin { + return (plugin as PrettyFormat.NewPlugin).serialize != null; } function printPlugin( - plugin: Plugin, + plugin: PrettyFormat.Plugin, val: any, - config: Config, + config: PrettyFormat.Config, indentation: string, depth: number, - refs: Refs, + refs: PrettyFormat.Refs, ): string { let printed; @@ -306,7 +298,7 @@ function printPlugin( return printed; } -function findPlugin(plugins: Plugins, val: any) { +function findPlugin(plugins: PrettyFormat.Plugins, val: any) { for (let p = 0; p < plugins.length; p++) { try { if (plugins[p].test(val)) { @@ -322,10 +314,10 @@ function findPlugin(plugins: Plugins, val: any) { function printer( val: any, - config: Config, + config: PrettyFormat.Config, indentation: string, depth: number, - refs: Refs, + refs: PrettyFormat.Refs, hasCalledToJSON?: boolean, ): string { const plugin = findPlugin(config.plugins, val); @@ -353,7 +345,7 @@ function printer( ); } -const DEFAULT_THEME: Theme = { +const DEFAULT_THEME: PrettyFormat.Theme = { comment: 'gray', content: 'reset', prop: 'yellow', @@ -363,7 +355,7 @@ const DEFAULT_THEME: Theme = { const DEFAULT_THEME_KEYS = Object.keys(DEFAULT_THEME); -const DEFAULT_OPTIONS: Options = { +const DEFAULT_OPTIONS: PrettyFormat.Options = { callToJSON: true, escapeRegex: false, escapeString: true, @@ -376,7 +368,7 @@ const DEFAULT_OPTIONS: Options = { theme: DEFAULT_THEME, }; -function validateOptions(options: OptionsReceived) { +function validateOptions(options: PrettyFormat.OptionsReceived) { Object.keys(options).forEach(key => { if (!DEFAULT_OPTIONS.hasOwnProperty(key)) { throw new Error(`pretty-format: Unknown option "${key}".`); @@ -402,7 +394,9 @@ function validateOptions(options: OptionsReceived) { } } -const getColorsHighlight = (options: OptionsReceived): Colors => +const getColorsHighlight = ( + options: PrettyFormat.OptionsReceived, +): PrettyFormat.Colors => DEFAULT_THEME_KEYS.reduce((colors, key) => { const value = options.theme && (options.theme as any)[key] !== undefined @@ -423,28 +417,30 @@ const getColorsHighlight = (options: OptionsReceived): Colors => return colors; }, Object.create(null)); -const getColorsEmpty = (): Colors => +const getColorsEmpty = (): PrettyFormat.Colors => DEFAULT_THEME_KEYS.reduce((colors, key) => { colors[key] = {close: '', open: ''}; return colors; }, Object.create(null)); -const getPrintFunctionName = (options?: OptionsReceived) => +const getPrintFunctionName = (options?: PrettyFormat.OptionsReceived) => options && options.printFunctionName !== undefined ? options.printFunctionName : DEFAULT_OPTIONS.printFunctionName; -const getEscapeRegex = (options?: OptionsReceived) => +const getEscapeRegex = (options?: PrettyFormat.OptionsReceived) => options && options.escapeRegex !== undefined ? options.escapeRegex : DEFAULT_OPTIONS.escapeRegex; -const getEscapeString = (options?: OptionsReceived) => +const getEscapeString = (options?: PrettyFormat.OptionsReceived) => options && options.escapeString !== undefined ? options.escapeString : DEFAULT_OPTIONS.escapeString; -const getConfig = (options?: OptionsReceived): Config => ({ +const getConfig = ( + options?: PrettyFormat.OptionsReceived, +): PrettyFormat.Config => ({ callToJSON: options && options.callToJSON !== undefined ? options.callToJSON @@ -486,7 +482,10 @@ function createIndent(indent: number): string { * @param val any potential JavaScript object * @param options Custom settings */ -function prettyFormat(val: any, options?: OptionsReceived): string { +function prettyFormat( + val: any, + options?: PrettyFormat.OptionsReceived, +): string { if (options) { validateOptions(options); if (options.plugins) { @@ -520,4 +519,17 @@ prettyFormat.plugins = { ReactTestComponent, }; +/* eslint-disable-next-line no-redeclare */ +namespace prettyFormat { + export type Colors = PrettyFormat.Colors; + export type Config = PrettyFormat.Config; + export type Options = PrettyFormat.Options; + export type OptionsReceived = PrettyFormat.OptionsReceived; + export type NewPlugin = PrettyFormat.NewPlugin; + export type Plugin = PrettyFormat.Plugin; + export type Plugins = PrettyFormat.Plugins; + export type Refs = PrettyFormat.Refs; + export type Theme = PrettyFormat.Theme; +} + export = prettyFormat; diff --git a/packages/pretty-format/src/types.ts b/packages/pretty-format/src/types.ts index 71f14783ed5f..89d06d5bd418 100644 --- a/packages/pretty-format/src/types.ts +++ b/packages/pretty-format/src/types.ts @@ -5,15 +5,113 @@ * LICENSE file in the root directory of this source tree. */ -import {PrettyFormat} from '@jest/types'; - -export type Colors = PrettyFormat.Colors; -export type Config = PrettyFormat.Config; -export type Options = PrettyFormat.Options; -export type OptionsReceived = PrettyFormat.OptionsReceived; -export type NewPlugin = PrettyFormat.NewPlugin; -export type Plugin = PrettyFormat.Plugin; -export type Plugins = PrettyFormat.Plugins; -export type Refs = PrettyFormat.Refs; -export type Theme = PrettyFormat.Theme; -export type Printer = PrettyFormat.Printer; +export type Colors = { + comment: {close: string; open: string}; + content: {close: string; open: string}; + prop: {close: string; open: string}; + tag: {close: string; open: string}; + value: {close: string; open: string}; +}; +type Indent = (arg0: string) => string; +export type Refs = Array; +type Print = (arg0: any) => string; + +export type Theme = { + comment: string; + content: string; + prop: string; + tag: string; + value: string; +}; + +type ThemeReceived = { + comment?: string; + content?: string; + prop?: string; + tag?: string; + value?: string; +}; + +export type Options = { + callToJSON: boolean; + escapeRegex: boolean; + escapeString: boolean; + highlight: boolean; + indent: number; + maxDepth: number; + min: boolean; + plugins: Plugins; + printFunctionName: boolean; + theme: Theme; +}; + +export type OptionsReceived = { + callToJSON?: boolean; + escapeRegex?: boolean; + escapeString?: boolean; + highlight?: boolean; + indent?: number; + maxDepth?: number; + min?: boolean; + plugins?: Plugins; + printFunctionName?: boolean; + theme?: ThemeReceived; +}; + +export type Config = { + callToJSON: boolean; + colors: Colors; + escapeRegex: boolean; + escapeString: boolean; + indent: string; + maxDepth: number; + min: boolean; + plugins: Plugins; + printFunctionName: boolean; + spacingInner: string; + spacingOuter: string; +}; + +export type Printer = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + hasCalledToJSON?: boolean, +) => string; + +type Test = (arg0: any) => boolean; + +export type NewPlugin = { + serialize: ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, + ) => string; + test: Test; +}; + +type PluginOptions = { + edgeSpacing: string; + min: boolean; + spacing: string; +}; + +type OldPlugin = { + print: ( + val: any, + print: Print, + indent: Indent, + options: PluginOptions, + colors: Colors, + ) => string; + test: Test; +}; + +export type Plugin = NewPlugin | OldPlugin; + +export type Plugins = Array;