Skip to content

decentralize pretty-format types #7972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
9 changes: 4 additions & 5 deletions packages/jest-snapshot/src/mock_serializer.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 3 additions & 4 deletions packages/jest-snapshot/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -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);
};

117 changes: 0 additions & 117 deletions packages/jest-types/src/PrettyFormat.ts

This file was deleted.

12 changes: 1 addition & 11 deletions packages/jest-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -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};
74 changes: 43 additions & 31 deletions packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
122 changes: 110 additions & 12 deletions packages/pretty-format/src/types.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
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<Plugin>;