Skip to content

Commit 5d48312

Browse files
jeysalSimenB
authored andcommitted
decentralize pretty-format types (#7972)
1 parent c6280e4 commit 5d48312

7 files changed

Lines changed: 162 additions & 181 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
### Chore & Maintenance
2626

2727
- `[*]`: 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))
28-
- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809))
28+
- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809), [#7809](https://github.com/facebook/jest/pull/7972))
2929
- `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820))
3030
- `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818))
3131
- `[jest-regex-util]`: Migrate to TypeScript ([#7822](https://github.com/facebook/jest/pull/7822))

packages/jest-snapshot/src/mock_serializer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {PrettyFormat} from '@jest/types';
8+
import {NewPlugin} from 'pretty-format';
99

10-
export const serialize: PrettyFormat.NewPlugin['serialize'] = (
10+
export const serialize: NewPlugin['serialize'] = (
1111
val,
1212
config,
1313
indentation,
@@ -42,9 +42,8 @@ export const serialize: PrettyFormat.NewPlugin['serialize'] = (
4242
return '[MockFunction' + nameString + ']' + callsString;
4343
};
4444

45-
export const test: PrettyFormat.NewPlugin['test'] = val =>
46-
val && !!val._isMockFunction;
45+
export const test: NewPlugin['test'] = val => val && !!val._isMockFunction;
4746

48-
const plugin: PrettyFormat.NewPlugin = {serialize, test};
47+
const plugin: NewPlugin = {serialize, test};
4948

5049
export default plugin;

packages/jest-snapshot/src/plugins.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import prettyFormat from 'pretty-format';
9-
import {PrettyFormat} from '@jest/types';
8+
import prettyFormat, {Plugin, Plugins} from 'pretty-format';
109

1110
import jestMockSerializer from './mock_serializer';
1211

@@ -19,7 +18,7 @@ const {
1918
AsymmetricMatcher,
2019
} = prettyFormat.plugins;
2120

22-
let PLUGINS: PrettyFormat.Plugins = [
21+
let PLUGINS: Plugins = [
2322
ReactTestComponent,
2423
ReactElement,
2524
DOMElement,
@@ -30,7 +29,7 @@ let PLUGINS: PrettyFormat.Plugins = [
3029
];
3130

3231
// Prepend to list so the last added is the first tested.
33-
export const addSerializer = (plugin: PrettyFormat.Plugin) => {
32+
export const addSerializer = (plugin: Plugin) => {
3433
PLUGINS = [plugin].concat(PLUGINS);
3534
};
3635

packages/jest-types/src/PrettyFormat.ts

Lines changed: 0 additions & 117 deletions
This file was deleted.

packages/jest-types/src/index.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,9 @@
88
import * as Config from './Config';
99
import * as Console from './Console';
1010
import * as Matchers from './Matchers';
11-
import * as PrettyFormat from './PrettyFormat';
1211
import * as SourceMaps from './SourceMaps';
1312
import * as TestResult from './TestResult';
1413
import * as Global from './Global';
1514
import * as Environment from './Environment';
1615

17-
export {
18-
Config,
19-
Console,
20-
Matchers,
21-
PrettyFormat,
22-
SourceMaps,
23-
TestResult,
24-
Global,
25-
Environment,
26-
};
16+
export {Config, Console, Matchers, SourceMaps, TestResult, Global, Environment};

packages/pretty-format/src/index.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@
66
*/
77

88
import style from 'ansi-styles';
9-
import {
10-
Colors,
11-
Config,
12-
Options,
13-
OptionsReceived,
14-
NewPlugin,
15-
Plugin,
16-
Plugins,
17-
Refs,
18-
Theme,
19-
} from './types';
9+
import * as PrettyFormat from './types';
2010

2111
import {
2212
printIteratorEntries,
@@ -179,10 +169,10 @@ function printBasicValue(
179169
*/
180170
function printComplexValue(
181171
val: any,
182-
config: Config,
172+
config: PrettyFormat.Config,
183173
indentation: string,
184174
depth: number,
185-
refs: Refs,
175+
refs: PrettyFormat.Refs,
186176
hasCalledToJSON?: boolean,
187177
): string {
188178
if (refs.indexOf(val) !== -1) {
@@ -261,17 +251,19 @@ function printComplexValue(
261251
'}';
262252
}
263253

264-
function isNewPlugin(plugin: Plugin): plugin is NewPlugin {
265-
return (plugin as NewPlugin).serialize != null;
254+
function isNewPlugin(
255+
plugin: PrettyFormat.Plugin,
256+
): plugin is PrettyFormat.NewPlugin {
257+
return (plugin as PrettyFormat.NewPlugin).serialize != null;
266258
}
267259

268260
function printPlugin(
269-
plugin: Plugin,
261+
plugin: PrettyFormat.Plugin,
270262
val: any,
271-
config: Config,
263+
config: PrettyFormat.Config,
272264
indentation: string,
273265
depth: number,
274-
refs: Refs,
266+
refs: PrettyFormat.Refs,
275267
): string {
276268
let printed;
277269

@@ -306,7 +298,7 @@ function printPlugin(
306298
return printed;
307299
}
308300

309-
function findPlugin(plugins: Plugins, val: any) {
301+
function findPlugin(plugins: PrettyFormat.Plugins, val: any) {
310302
for (let p = 0; p < plugins.length; p++) {
311303
try {
312304
if (plugins[p].test(val)) {
@@ -322,10 +314,10 @@ function findPlugin(plugins: Plugins, val: any) {
322314

323315
function printer(
324316
val: any,
325-
config: Config,
317+
config: PrettyFormat.Config,
326318
indentation: string,
327319
depth: number,
328-
refs: Refs,
320+
refs: PrettyFormat.Refs,
329321
hasCalledToJSON?: boolean,
330322
): string {
331323
const plugin = findPlugin(config.plugins, val);
@@ -353,7 +345,7 @@ function printer(
353345
);
354346
}
355347

356-
const DEFAULT_THEME: Theme = {
348+
const DEFAULT_THEME: PrettyFormat.Theme = {
357349
comment: 'gray',
358350
content: 'reset',
359351
prop: 'yellow',
@@ -363,7 +355,7 @@ const DEFAULT_THEME: Theme = {
363355

364356
const DEFAULT_THEME_KEYS = Object.keys(DEFAULT_THEME);
365357

366-
const DEFAULT_OPTIONS: Options = {
358+
const DEFAULT_OPTIONS: PrettyFormat.Options = {
367359
callToJSON: true,
368360
escapeRegex: false,
369361
escapeString: true,
@@ -376,7 +368,7 @@ const DEFAULT_OPTIONS: Options = {
376368
theme: DEFAULT_THEME,
377369
};
378370

379-
function validateOptions(options: OptionsReceived) {
371+
function validateOptions(options: PrettyFormat.OptionsReceived) {
380372
Object.keys(options).forEach(key => {
381373
if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {
382374
throw new Error(`pretty-format: Unknown option "${key}".`);
@@ -402,7 +394,9 @@ function validateOptions(options: OptionsReceived) {
402394
}
403395
}
404396

405-
const getColorsHighlight = (options: OptionsReceived): Colors =>
397+
const getColorsHighlight = (
398+
options: PrettyFormat.OptionsReceived,
399+
): PrettyFormat.Colors =>
406400
DEFAULT_THEME_KEYS.reduce((colors, key) => {
407401
const value =
408402
options.theme && (options.theme as any)[key] !== undefined
@@ -423,28 +417,30 @@ const getColorsHighlight = (options: OptionsReceived): Colors =>
423417
return colors;
424418
}, Object.create(null));
425419

426-
const getColorsEmpty = (): Colors =>
420+
const getColorsEmpty = (): PrettyFormat.Colors =>
427421
DEFAULT_THEME_KEYS.reduce((colors, key) => {
428422
colors[key] = {close: '', open: ''};
429423
return colors;
430424
}, Object.create(null));
431425

432-
const getPrintFunctionName = (options?: OptionsReceived) =>
426+
const getPrintFunctionName = (options?: PrettyFormat.OptionsReceived) =>
433427
options && options.printFunctionName !== undefined
434428
? options.printFunctionName
435429
: DEFAULT_OPTIONS.printFunctionName;
436430

437-
const getEscapeRegex = (options?: OptionsReceived) =>
431+
const getEscapeRegex = (options?: PrettyFormat.OptionsReceived) =>
438432
options && options.escapeRegex !== undefined
439433
? options.escapeRegex
440434
: DEFAULT_OPTIONS.escapeRegex;
441435

442-
const getEscapeString = (options?: OptionsReceived) =>
436+
const getEscapeString = (options?: PrettyFormat.OptionsReceived) =>
443437
options && options.escapeString !== undefined
444438
? options.escapeString
445439
: DEFAULT_OPTIONS.escapeString;
446440

447-
const getConfig = (options?: OptionsReceived): Config => ({
441+
const getConfig = (
442+
options?: PrettyFormat.OptionsReceived,
443+
): PrettyFormat.Config => ({
448444
callToJSON:
449445
options && options.callToJSON !== undefined
450446
? options.callToJSON
@@ -486,7 +482,10 @@ function createIndent(indent: number): string {
486482
* @param val any potential JavaScript object
487483
* @param options Custom settings
488484
*/
489-
function prettyFormat(val: any, options?: OptionsReceived): string {
485+
function prettyFormat(
486+
val: any,
487+
options?: PrettyFormat.OptionsReceived,
488+
): string {
490489
if (options) {
491490
validateOptions(options);
492491
if (options.plugins) {
@@ -520,4 +519,17 @@ prettyFormat.plugins = {
520519
ReactTestComponent,
521520
};
522521

522+
/* eslint-disable-next-line no-redeclare */
523+
namespace prettyFormat {
524+
export type Colors = PrettyFormat.Colors;
525+
export type Config = PrettyFormat.Config;
526+
export type Options = PrettyFormat.Options;
527+
export type OptionsReceived = PrettyFormat.OptionsReceived;
528+
export type NewPlugin = PrettyFormat.NewPlugin;
529+
export type Plugin = PrettyFormat.Plugin;
530+
export type Plugins = PrettyFormat.Plugins;
531+
export type Refs = PrettyFormat.Refs;
532+
export type Theme = PrettyFormat.Theme;
533+
}
534+
523535
export = prettyFormat;

0 commit comments

Comments
 (0)