From 71cb36ef013ad577ff8f809d5650c77861b542dd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 15 Sep 2020 00:00:22 +0200 Subject: [PATCH] chore: migrate pretty-format to ESM --- CHANGELOG.md | 2 + docs/JestPlatform.md | 2 +- .../jest-circus/src/formatNodeAssertErrors.ts | 2 +- packages/jest-circus/src/utils.ts | 2 +- packages/jest-config/src/Deprecated.ts | 2 +- packages/jest-diff/README.md | 2 +- packages/jest-diff/src/index.ts | 7 +- .../jest-each/src/__tests__/array.test.ts | 2 +- packages/jest-each/src/table/array.ts | 2 +- packages/jest-each/src/table/template.ts | 2 +- packages/jest-each/src/validation.ts | 2 +- .../src/expectationResultFactory.ts | 2 +- packages/jest-jasmine2/src/isError.ts | 2 +- packages/jest-leak-detector/src/index.ts | 2 +- .../src/__tests__/index.test.ts | 2 +- packages/jest-matcher-utils/src/index.ts | 7 +- .../src/__tests__/index.test.ts | 2 +- .../src/__tests__/dedentLines.test.ts | 4 +- .../src/__tests__/mock_serializer.test.ts | 2 +- .../src/__tests__/printSnapshot.test.ts | 2 +- packages/jest-snapshot/src/plugins.ts | 14 ++- packages/jest-snapshot/src/printSnapshot.ts | 2 +- packages/jest-snapshot/src/utils.ts | 2 +- .../src/__tests__/fixtures/jestConfig.ts | 3 +- packages/jest-validate/src/utils.ts | 2 +- packages/pretty-format/README.md | 41 ++++----- .../src/__tests__/AsymmetricMatcher.test.ts | 7 +- .../src/__tests__/ConvertAnsi.test.ts | 4 +- .../src/__tests__/DOMCollection.test.ts | 4 +- .../src/__tests__/DOMElement.test.ts | 4 +- .../src/__tests__/Immutable.test.ts | 4 +- .../src/__tests__/ReactElement.test.ts | 4 +- .../src/__tests__/prettyFormat.test.ts | 2 +- .../src/__tests__/react.test.tsx | 7 +- .../src/__tests__/setPrettyPrint.ts | 2 +- packages/pretty-format/src/index.ts | 91 +++++++++---------- .../src/alignedAnsiStyleSerializer.ts | 4 +- 37 files changed, 129 insertions(+), 120 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97a12828567f..9c5202419a15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ ### Chore & Maintenance +- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515)) + ### Performance ## 26.4.2 diff --git a/docs/JestPlatform.md b/docs/JestPlatform.md index 648acb758943..5ed250b1ef0a 100644 --- a/docs/JestPlatform.md +++ b/docs/JestPlatform.md @@ -160,7 +160,7 @@ Exports a function that converts any JavaScript value into a human-readable stri ### Example ```javascript -const prettyFormat = require('pretty-format'); +const {format: prettyFormat} = require('pretty-format'); const val = {object: {}}; val.circularReference = val; diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 10ffc8d1e25f..b487970c908f 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -14,7 +14,7 @@ import { printReceived, } from 'jest-matcher-utils'; import chalk = require('chalk'); -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; interface AssertionErrorWithStack extends AssertionError { stack: string; diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index b1e9f8aeb95d..3693392f0f23 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -12,7 +12,7 @@ import isGeneratorFn from 'is-generator-fn'; import co from 'co'; import dedent = require('dedent'); import StackUtils = require('stack-utils'); -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import type {AssertionResult, Status} from '@jest/test-result'; import {ROOT_DESCRIBE_BLOCK_NAME, getState} from './state'; diff --git a/packages/jest-config/src/Deprecated.ts b/packages/jest-config/src/Deprecated.ts index 493a44770692..df4bee369667 100644 --- a/packages/jest-config/src/Deprecated.ts +++ b/packages/jest-config/src/Deprecated.ts @@ -6,7 +6,7 @@ */ import chalk = require('chalk'); -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import type {DeprecatedOptions} from 'jest-validate'; const format = (value: unknown) => prettyFormat(value, {min: true}); diff --git a/packages/jest-diff/README.md b/packages/jest-diff/README.md index f63843ce8f3c..5fcaca167f1e 100644 --- a/packages/jest-diff/README.md +++ b/packages/jest-diff/README.md @@ -165,7 +165,7 @@ You might call this function for case insensitive or Unicode equivalence compari ### Example of diffLinesUnified2 ```js -import format from 'pretty-format'; +import {format} from 'pretty-format'; const a = { text: 'Ignore indentation in serialized object', diff --git a/packages/jest-diff/src/index.ts b/packages/jest-diff/src/index.ts index fd0e680eea03..d63f8c750820 100644 --- a/packages/jest-diff/src/index.ts +++ b/packages/jest-diff/src/index.ts @@ -5,7 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -import prettyFormat = require('pretty-format'); +import { + format as prettyFormat, + plugins as prettyFormatPlugins, +} from 'pretty-format'; import chalk = require('chalk'); import getType = require('jest-get-type'); import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic'; @@ -33,7 +36,7 @@ const { Immutable, ReactElement, ReactTestComponent, -} = prettyFormat.plugins; +} = prettyFormatPlugins; const PLUGINS = [ ReactTestComponent, diff --git a/packages/jest-each/src/__tests__/array.test.ts b/packages/jest-each/src/__tests__/array.test.ts index 19136ae65f81..b7fd6af0925a 100644 --- a/packages/jest-each/src/__tests__/array.test.ts +++ b/packages/jest-each/src/__tests__/array.test.ts @@ -6,7 +6,7 @@ * */ -import pretty = require('pretty-format'); +import {format as pretty} from 'pretty-format'; import each from '../'; const noop = () => {}; diff --git a/packages/jest-each/src/table/array.ts b/packages/jest-each/src/table/array.ts index 61eeb1e58ee2..b226d4d6231f 100644 --- a/packages/jest-each/src/table/array.ts +++ b/packages/jest-each/src/table/array.ts @@ -7,7 +7,7 @@ */ import * as util from 'util'; -import pretty = require('pretty-format'); +import {format as pretty} from 'pretty-format'; import type {Global} from '@jest/types'; import type {EachTests} from '../bind'; diff --git a/packages/jest-each/src/table/template.ts b/packages/jest-each/src/table/template.ts index 936410189a7d..1daa1fd8f7b6 100644 --- a/packages/jest-each/src/table/template.ts +++ b/packages/jest-each/src/table/template.ts @@ -6,7 +6,7 @@ * */ -import pretty = require('pretty-format'); +import {format as pretty} from 'pretty-format'; import {isPrimitive} from 'jest-get-type'; import type {Global} from '@jest/types'; import type {EachTests} from '../bind'; diff --git a/packages/jest-each/src/validation.ts b/packages/jest-each/src/validation.ts index 7fb2df3c3cf2..b38f88668ab4 100644 --- a/packages/jest-each/src/validation.ts +++ b/packages/jest-each/src/validation.ts @@ -7,7 +7,7 @@ */ import chalk = require('chalk'); -import pretty = require('pretty-format'); +import {format as pretty} from 'pretty-format'; import type {Global} from '@jest/types'; type TemplateData = Global.TemplateData; diff --git a/packages/jest-jasmine2/src/expectationResultFactory.ts b/packages/jest-jasmine2/src/expectationResultFactory.ts index d4533fdf3dcc..bb78d3f2014d 100644 --- a/packages/jest-jasmine2/src/expectationResultFactory.ts +++ b/packages/jest-jasmine2/src/expectationResultFactory.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import type {FailedAssertion} from '@jest/test-result'; function messageFormatter({error, message, passed}: Options) { diff --git a/packages/jest-jasmine2/src/isError.ts b/packages/jest-jasmine2/src/isError.ts index 1eb0aef83ace..05f68725e4e5 100644 --- a/packages/jest-jasmine2/src/isError.ts +++ b/packages/jest-jasmine2/src/isError.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; export default function isError( // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types diff --git a/packages/jest-leak-detector/src/index.ts b/packages/jest-leak-detector/src/index.ts index e7bd026edc21..efd544270fd8 100644 --- a/packages/jest-leak-detector/src/index.ts +++ b/packages/jest-leak-detector/src/index.ts @@ -10,7 +10,7 @@ import {setFlagsFromString} from 'v8'; import {runInNewContext} from 'vm'; import {promisify} from 'util'; -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import {isPrimitive} from 'jest-get-type'; const tick = promisify(setImmediate); diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.ts b/packages/jest-matcher-utils/src/__tests__/index.test.ts index fbba12527ebc..fd71e442ca05 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -7,7 +7,7 @@ */ import chalk = require('chalk'); -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import {alignedAnsiStyleSerializer} from '@jest/test-utils'; import { MatcherHintOptions, diff --git a/packages/jest-matcher-utils/src/index.ts b/packages/jest-matcher-utils/src/index.ts index 3013c77ebc53..580fc06f2839 100644 --- a/packages/jest-matcher-utils/src/index.ts +++ b/packages/jest-matcher-utils/src/index.ts @@ -18,7 +18,10 @@ import diffDefault, { diffStringsUnified, } from 'jest-diff'; import getType = require('jest-get-type'); -import prettyFormat = require('pretty-format'); +import { + format as prettyFormat, + plugins as prettyFormatPlugins, +} from 'pretty-format'; import Replaceable from './Replaceable'; import deepCyclicCopyReplaceable from './deepCyclicCopyReplaceable'; @@ -29,7 +32,7 @@ const { Immutable, ReactElement, ReactTestComponent, -} = prettyFormat.plugins; +} = prettyFormatPlugins; const PLUGINS = [ ReactTestComponent, diff --git a/packages/jest-serializer/src/__tests__/index.test.ts b/packages/jest-serializer/src/__tests__/index.test.ts index 61bea44cbb7a..ba9225900d4e 100644 --- a/packages/jest-serializer/src/__tests__/index.test.ts +++ b/packages/jest-serializer/src/__tests__/index.test.ts @@ -8,7 +8,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; import * as fs from 'graceful-fs'; -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import serializer from '..'; diff --git a/packages/jest-snapshot/src/__tests__/dedentLines.test.ts b/packages/jest-snapshot/src/__tests__/dedentLines.test.ts index 43cdcb8447ed..429b74fec8da 100644 --- a/packages/jest-snapshot/src/__tests__/dedentLines.test.ts +++ b/packages/jest-snapshot/src/__tests__/dedentLines.test.ts @@ -5,11 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -import format = require('pretty-format'); +import {plugins as builtinPlugins, format} from 'pretty-format'; import {dedentLines} from '../dedentLines'; const $$typeof = Symbol.for('react.test.json'); -const plugins = [format.plugins.ReactTestComponent]; +const plugins = [builtinPlugins.ReactTestComponent]; const formatLines2 = val => format(val, {indent: 2, plugins}).split('\n'); const formatLines0 = val => format(val, {indent: 0, plugins}).split('\n'); diff --git a/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts b/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts index 993ee3aa9963..8c3f10084a83 100644 --- a/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts +++ b/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import plugin from '../mock_serializer'; diff --git a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts index 85bd10a85786..49e83020e14e 100644 --- a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts +++ b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts @@ -8,7 +8,7 @@ import ansiRegex = require('ansi-regex'); import styles = require('ansi-styles'); import chalk = require('chalk'); -import format = require('pretty-format'); +import {format} from 'pretty-format'; import jestSnapshot = require('../index'); import { diff --git a/packages/jest-snapshot/src/plugins.ts b/packages/jest-snapshot/src/plugins.ts index 579c638a84cd..762633f08e0b 100644 --- a/packages/jest-snapshot/src/plugins.ts +++ b/packages/jest-snapshot/src/plugins.ts @@ -5,7 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -import prettyFormat = require('pretty-format'); +import { + Plugin as PrettyFormatPlugin, + Plugins as PrettyFormatPlugins, + plugins as prettyFormatPlugins, +} from 'pretty-format'; import jestMockSerializer from './mock_serializer'; @@ -16,9 +20,9 @@ const { ReactElement, ReactTestComponent, AsymmetricMatcher, -} = prettyFormat.plugins; +} = prettyFormatPlugins; -let PLUGINS: prettyFormat.Plugins = [ +let PLUGINS: PrettyFormatPlugins = [ ReactTestComponent, ReactElement, DOMElement, @@ -29,8 +33,8 @@ let PLUGINS: prettyFormat.Plugins = [ ]; // Prepend to list so the last added is the first tested. -export const addSerializer = (plugin: prettyFormat.Plugin): void => { +export const addSerializer = (plugin: PrettyFormatPlugin): void => { PLUGINS = [plugin].concat(PLUGINS); }; -export const getSerializers = (): prettyFormat.Plugins => PLUGINS; +export const getSerializers = (): PrettyFormatPlugins => PLUGINS; diff --git a/packages/jest-snapshot/src/printSnapshot.ts b/packages/jest-snapshot/src/printSnapshot.ts index ddd5814b0b14..a5f5cd55023f 100644 --- a/packages/jest-snapshot/src/printSnapshot.ts +++ b/packages/jest-snapshot/src/printSnapshot.ts @@ -33,7 +33,7 @@ import { getLabelPrinter, matcherHint, } from 'jest-matcher-utils'; -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import { aBackground2, diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index da5867ce2121..6ad1a434647b 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -10,7 +10,7 @@ import * as fs from 'graceful-fs'; import naturalCompare = require('natural-compare'); import chalk = require('chalk'); import type {Config} from '@jest/types'; -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import {getSerializers} from './plugins'; import type {SnapshotData} from './types'; diff --git a/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts b/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts index 8e1438a5e810..e96dc664470a 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts +++ b/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts @@ -127,7 +127,8 @@ const validConfig = { watchman: true, }; -const format = (value: string) => require('pretty-format')(value, {min: true}); +const format = (value: string) => + require('pretty-format').format(value, {min: true}); const deprecatedConfig = { preprocessorIgnorePatterns: (config: Record) => diff --git a/packages/jest-validate/src/utils.ts b/packages/jest-validate/src/utils.ts index 48d2beabe980..ef3c8879eada 100644 --- a/packages/jest-validate/src/utils.ts +++ b/packages/jest-validate/src/utils.ts @@ -6,7 +6,7 @@ */ import chalk = require('chalk'); -import prettyFormat = require('pretty-format'); +import {format as prettyFormat} from 'pretty-format'; import leven from 'leven'; const BULLET: string = chalk.bold('\u25cf'); diff --git a/packages/pretty-format/README.md b/packages/pretty-format/README.md index ca445cfaf7b2..34c273d2232f 100755 --- a/packages/pretty-format/README.md +++ b/packages/pretty-format/README.md @@ -14,11 +14,11 @@ $ yarn add pretty-format ## Usage ```js -const prettyFormat = require('pretty-format'); // CommonJS +const {format} = require('pretty-format'); // CommonJS ``` ```js -import prettyFormat from 'pretty-format'; // ES2015 modules +import {format} from 'pretty-format'; // ES2015 modules ``` ```js @@ -28,7 +28,7 @@ val[Symbol('foo')] = 'foo'; val.map = new Map([['prop', 'value']]); val.array = [-0, Infinity, NaN]; -console.log(prettyFormat(val)); +console.log(format(val)); /* Object { "array": Array [ @@ -51,7 +51,7 @@ Object { ```js function onClick() {} -console.log(prettyFormat(onClick)); +console.log(format(onClick)); /* [Function onClick] */ @@ -59,7 +59,7 @@ console.log(prettyFormat(onClick)); const options = { printFunctionName: false, }; -console.log(prettyFormat(onClick, options)); +console.log(format(onClick, options)); /* [Function] */ @@ -100,9 +100,8 @@ The `pretty-format` package provides some built-in plugins, including: ```js // CommonJS -const prettyFormat = require('pretty-format'); -const ReactElement = prettyFormat.plugins.ReactElement; -const ReactTestComponent = prettyFormat.plugins.ReactTestComponent; +const {plugins} = require('pretty-format'); +const {ReactElement, ReactTestComponent} = plugins; const React = require('react'); const renderer = require('react-test-renderer'); @@ -110,8 +109,8 @@ const renderer = require('react-test-renderer'); ```js // ES2015 modules and destructuring assignment -import prettyFormat from 'pretty-format'; -const {ReactElement, ReactTestComponent} = prettyFormat.plugins; +import {plugins} from 'pretty-format'; +const {ReactElement, ReactTestComponent} = plugins; import React from 'react'; import renderer from 'react-test-renderer'; @@ -121,11 +120,11 @@ import renderer from 'react-test-renderer'; const onClick = () => {}; const element = React.createElement('button', {onClick}, 'Hello World'); -const formatted1 = prettyFormat(element, { +const formatted1 = format(element, { plugins: [ReactElement], printFunctionName: false, }); -const formatted2 = prettyFormat(renderer.create(element).toJSON(), { +const formatted2 = format(renderer.create(element).toJSON(), { plugins: [ReactTestComponent], printFunctionName: false, }); @@ -167,7 +166,7 @@ For **all** test files, you can specify modules in Jest configuration. They prec A plugin is a JavaScript object. -If `options` has a `plugins` array: for the first plugin whose `test(val)` method returns a truthy value, then `prettyFormat(val, options)` returns the result from either: +If `options` has a `plugins` array: for the first plugin whose `test(val)` method returns a truthy value, then `format(val, options)` returns the result from either: - `serialize(val, …)` method of the **improved** interface (available in **version 21** or later) - `print(val, …)` method of the **original** interface (if plugin does not have `serialize` method) @@ -290,7 +289,7 @@ const val = { ```js console.log( - prettyFormat(val, { + format(val, { plugins: [plugin], }), ); @@ -313,7 +312,7 @@ Object { ```js console.log( - prettyFormat(val, { + format(val, { indent: 4, plugins: [plugin], }), @@ -337,7 +336,7 @@ Object { ```js console.log( - prettyFormat(val, { + format(val, { maxDepth: 1, plugins: [plugin], }), @@ -352,7 +351,7 @@ Object { ```js console.log( - prettyFormat(val, { + format(val, { min: true, plugins: [plugin], }), @@ -411,7 +410,7 @@ const val = { render() {}, }; -prettyFormat(val, { +format(val, { plugins: [plugin], }); /* @@ -421,7 +420,7 @@ Object { } */ -prettyFormat(val); +format(val); /* Object { "onClick": [Function onClick], @@ -433,7 +432,7 @@ Object { This plugin **ignores** the `printFunctionName` option. That limitation of the original `print` interface is a reason to use the improved `serialize` interface, described above. ```js -prettyFormat(val, { +format(val, { plugins: [pluginOld], printFunctionName: false, }); @@ -444,7 +443,7 @@ Object { } */ -prettyFormat(val, { +format(val, { printFunctionName: false, }); /* diff --git a/packages/pretty-format/src/__tests__/AsymmetricMatcher.test.ts b/packages/pretty-format/src/__tests__/AsymmetricMatcher.test.ts index e31c763efe7b..8bc869ba1eea 100644 --- a/packages/pretty-format/src/__tests__/AsymmetricMatcher.test.ts +++ b/packages/pretty-format/src/__tests__/AsymmetricMatcher.test.ts @@ -5,11 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -import {OptionsReceived} from '../types'; +import type {OptionsReceived} from '../types'; +import {plugins, format as prettyFormat} from '../'; -import prettyFormat from '../'; - -const {AsymmetricMatcher} = prettyFormat.plugins; +const {AsymmetricMatcher} = plugins; let options: OptionsReceived; function fnNameFor(func: (...any: Array) => unknown) { diff --git a/packages/pretty-format/src/__tests__/ConvertAnsi.test.ts b/packages/pretty-format/src/__tests__/ConvertAnsi.test.ts index 6e74956c34f5..68c860b081a7 100644 --- a/packages/pretty-format/src/__tests__/ConvertAnsi.test.ts +++ b/packages/pretty-format/src/__tests__/ConvertAnsi.test.ts @@ -6,9 +6,9 @@ */ import ansiStyle = require('ansi-styles'); -import prettyFormat = require('../'); +import {plugins, format as prettyFormat} from '../'; -const {ConvertAnsi} = prettyFormat.plugins; +const {ConvertAnsi} = plugins; const prettyFormatResult = (val: string) => prettyFormat(val, { diff --git a/packages/pretty-format/src/__tests__/DOMCollection.test.ts b/packages/pretty-format/src/__tests__/DOMCollection.test.ts index 011951457900..b42c2b6b73db 100644 --- a/packages/pretty-format/src/__tests__/DOMCollection.test.ts +++ b/packages/pretty-format/src/__tests__/DOMCollection.test.ts @@ -8,10 +8,10 @@ */ /* eslint-env browser*/ -import prettyFormat from '../'; +import {plugins} from '../'; import setPrettyPrint from './setPrettyPrint'; -const {DOMCollection, DOMElement} = prettyFormat.plugins; +const {DOMCollection, DOMElement} = plugins; setPrettyPrint([DOMCollection, DOMElement]); diff --git a/packages/pretty-format/src/__tests__/DOMElement.test.ts b/packages/pretty-format/src/__tests__/DOMElement.test.ts index 6ad40955532e..2774cb432dbe 100644 --- a/packages/pretty-format/src/__tests__/DOMElement.test.ts +++ b/packages/pretty-format/src/__tests__/DOMElement.test.ts @@ -8,10 +8,10 @@ */ /* eslint-env browser*/ -import prettyFormat from '../'; +import {plugins, format as prettyFormat} from '../'; import setPrettyPrint from './setPrettyPrint'; -const {DOMElement} = prettyFormat.plugins; +const {DOMElement} = plugins; setPrettyPrint([DOMElement]); diff --git a/packages/pretty-format/src/__tests__/Immutable.test.ts b/packages/pretty-format/src/__tests__/Immutable.test.ts index 651c8da8bd41..6e95682af718 100644 --- a/packages/pretty-format/src/__tests__/Immutable.test.ts +++ b/packages/pretty-format/src/__tests__/Immutable.test.ts @@ -8,10 +8,10 @@ import React from 'react'; import Immutable from 'immutable'; -import prettyFormat from '..'; +import {plugins} from '..'; import setPrettyPrint from './setPrettyPrint'; -const {Immutable: ImmutablePlugin, ReactElement} = prettyFormat.plugins; +const {Immutable: ImmutablePlugin, ReactElement} = plugins; setPrettyPrint([ReactElement, ImmutablePlugin]); diff --git a/packages/pretty-format/src/__tests__/ReactElement.test.ts b/packages/pretty-format/src/__tests__/ReactElement.test.ts index 8946c4e3027a..911048067c38 100644 --- a/packages/pretty-format/src/__tests__/ReactElement.test.ts +++ b/packages/pretty-format/src/__tests__/ReactElement.test.ts @@ -6,10 +6,10 @@ */ import React from 'react'; -import prettyFormat from '..'; +import {plugins} from '../'; import setPrettyPrint from './setPrettyPrint'; -const {ReactElement} = prettyFormat.plugins; +const {ReactElement} = plugins; setPrettyPrint([ReactElement]); diff --git a/packages/pretty-format/src/__tests__/prettyFormat.test.ts b/packages/pretty-format/src/__tests__/prettyFormat.test.ts index 0189abdfa742..f533b31582fc 100644 --- a/packages/pretty-format/src/__tests__/prettyFormat.test.ts +++ b/packages/pretty-format/src/__tests__/prettyFormat.test.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import prettyFormat from '../'; +import {format as prettyFormat} from '../'; function returnArguments(..._args: Array) { return arguments; diff --git a/packages/pretty-format/src/__tests__/react.test.tsx b/packages/pretty-format/src/__tests__/react.test.tsx index c79cef239e77..3dd18801b937 100644 --- a/packages/pretty-format/src/__tests__/react.test.tsx +++ b/packages/pretty-format/src/__tests__/react.test.tsx @@ -7,15 +7,14 @@ import React from 'react'; import renderer from 'react-test-renderer'; - -import {OptionsReceived} from '../types'; -import prettyFormat from '..'; +import type {OptionsReceived} from '../types'; +import {plugins, format as prettyFormat} from '..'; const elementSymbol = Symbol.for('react.element'); const fragmentSymbol = Symbol.for('react.fragment'); const suspenseSymbol = Symbol.for('react.suspense'); const testSymbol = Symbol.for('react.test.json'); -const {ReactElement, ReactTestComponent} = prettyFormat.plugins; +const {ReactElement, ReactTestComponent} = plugins; const formatElement = (element: unknown, options?: OptionsReceived) => prettyFormat(element, {plugins: [ReactElement], ...options}); diff --git a/packages/pretty-format/src/__tests__/setPrettyPrint.ts b/packages/pretty-format/src/__tests__/setPrettyPrint.ts index f36e542450ec..33f99986c915 100644 --- a/packages/pretty-format/src/__tests__/setPrettyPrint.ts +++ b/packages/pretty-format/src/__tests__/setPrettyPrint.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import prettyFormat from '../'; +import {format as prettyFormat} from '../'; import type {OptionsReceived, Plugins} from '../types'; declare global { diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index 1960daf96b41..09a8fc49f153 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -8,7 +8,30 @@ /* eslint-disable local/ban-types-eventually */ import style = require('ansi-styles'); -import type * as PrettyFormat from './types'; +import type { + Colors, + Config, + NewPlugin, + Options, + OptionsReceived, + Plugin, + Plugins, + Refs, + Theme, +} from './types'; + +export type { + Colors, + Config, + Options, + OptionsReceived, + OldPlugin, + NewPlugin, + Plugin, + Plugins, + Refs, + Theme, +} from './types'; import { printIteratorEntries, @@ -178,10 +201,10 @@ function printBasicValue( */ function printComplexValue( val: any, - config: PrettyFormat.Config, + config: Config, indentation: string, depth: number, - refs: PrettyFormat.Refs, + refs: Refs, hasCalledToJSON?: boolean, ): string { if (refs.indexOf(val) !== -1) { @@ -260,19 +283,17 @@ function printComplexValue( '}'; } -function isNewPlugin( - plugin: PrettyFormat.Plugin, -): plugin is PrettyFormat.NewPlugin { - return (plugin as PrettyFormat.NewPlugin).serialize != null; +function isNewPlugin(plugin: Plugin): plugin is NewPlugin { + return (plugin as NewPlugin).serialize != null; } function printPlugin( - plugin: PrettyFormat.Plugin, + plugin: Plugin, val: any, - config: PrettyFormat.Config, + config: Config, indentation: string, depth: number, - refs: PrettyFormat.Refs, + refs: Refs, ): string { let printed; @@ -307,7 +328,7 @@ function printPlugin( return printed; } -function findPlugin(plugins: PrettyFormat.Plugins, val: unknown) { +function findPlugin(plugins: Plugins, val: unknown) { for (let p = 0; p < plugins.length; p++) { try { if (plugins[p].test(val)) { @@ -323,10 +344,10 @@ function findPlugin(plugins: PrettyFormat.Plugins, val: unknown) { function printer( val: unknown, - config: PrettyFormat.Config, + config: Config, indentation: string, depth: number, - refs: PrettyFormat.Refs, + refs: Refs, hasCalledToJSON?: boolean, ): string { const plugin = findPlugin(config.plugins, val); @@ -354,7 +375,7 @@ function printer( ); } -const DEFAULT_THEME: PrettyFormat.Theme = { +const DEFAULT_THEME: Theme = { comment: 'gray', content: 'reset', prop: 'yellow', @@ -366,7 +387,7 @@ const DEFAULT_THEME_KEYS = Object.keys(DEFAULT_THEME) as Array< keyof typeof DEFAULT_THEME >; -const DEFAULT_OPTIONS: PrettyFormat.Options = { +const DEFAULT_OPTIONS: Options = { callToJSON: true, escapeRegex: false, escapeString: true, @@ -379,7 +400,7 @@ const DEFAULT_OPTIONS: PrettyFormat.Options = { theme: DEFAULT_THEME, }; -function validateOptions(options: PrettyFormat.OptionsReceived) { +function validateOptions(options: OptionsReceived) { Object.keys(options).forEach(key => { if (!DEFAULT_OPTIONS.hasOwnProperty(key)) { throw new Error(`pretty-format: Unknown option "${key}".`); @@ -405,9 +426,7 @@ function validateOptions(options: PrettyFormat.OptionsReceived) { } } -const getColorsHighlight = ( - options: PrettyFormat.OptionsReceived, -): PrettyFormat.Colors => +const getColorsHighlight = (options: OptionsReceived): Colors => DEFAULT_THEME_KEYS.reduce((colors, key) => { const value = options.theme && options.theme[key] !== undefined @@ -428,30 +447,28 @@ const getColorsHighlight = ( return colors; }, Object.create(null)); -const getColorsEmpty = (): PrettyFormat.Colors => +const getColorsEmpty = (): Colors => DEFAULT_THEME_KEYS.reduce((colors, key) => { colors[key] = {close: '', open: ''}; return colors; }, Object.create(null)); -const getPrintFunctionName = (options?: PrettyFormat.OptionsReceived) => +const getPrintFunctionName = (options?: OptionsReceived) => options && options.printFunctionName !== undefined ? options.printFunctionName : DEFAULT_OPTIONS.printFunctionName; -const getEscapeRegex = (options?: PrettyFormat.OptionsReceived) => +const getEscapeRegex = (options?: OptionsReceived) => options && options.escapeRegex !== undefined ? options.escapeRegex : DEFAULT_OPTIONS.escapeRegex; -const getEscapeString = (options?: PrettyFormat.OptionsReceived) => +const getEscapeString = (options?: OptionsReceived) => options && options.escapeString !== undefined ? options.escapeString : DEFAULT_OPTIONS.escapeString; -const getConfig = ( - options?: PrettyFormat.OptionsReceived, -): PrettyFormat.Config => ({ +const getConfig = (options?: OptionsReceived): Config => ({ callToJSON: options && options.callToJSON !== undefined ? options.callToJSON @@ -493,10 +510,7 @@ function createIndent(indent: number): string { * @param val any potential JavaScript object * @param options Custom settings */ -function prettyFormat( - val: unknown, - options?: PrettyFormat.OptionsReceived, -): string { +export function format(val: unknown, options?: OptionsReceived): string { if (options) { validateOptions(options); if (options.plugins) { @@ -520,7 +534,7 @@ function prettyFormat( return printComplexValue(val, getConfig(options), '', 0, []); } -prettyFormat.plugins = { +export const plugins = { AsymmetricMatcher, ConvertAnsi, DOMCollection, @@ -529,18 +543,3 @@ prettyFormat.plugins = { ReactElement, ReactTestComponent, }; - -namespace prettyFormat { - export type Colors = PrettyFormat.Colors; - export type Config = PrettyFormat.Config; - export type Options = PrettyFormat.Options; - export type OptionsReceived = PrettyFormat.OptionsReceived; - export type OldPlugin = PrettyFormat.OldPlugin; - 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/test-utils/src/alignedAnsiStyleSerializer.ts b/packages/test-utils/src/alignedAnsiStyleSerializer.ts index bc92b21d4969..e007e5e0aacd 100644 --- a/packages/test-utils/src/alignedAnsiStyleSerializer.ts +++ b/packages/test-utils/src/alignedAnsiStyleSerializer.ts @@ -7,9 +7,9 @@ import ansiRegex = require('ansi-regex'); import style = require('ansi-styles'); -import prettyFormat = require('pretty-format'); +import type {NewPlugin} from 'pretty-format'; -export const alignedAnsiStyleSerializer: prettyFormat.NewPlugin = { +export const alignedAnsiStyleSerializer: NewPlugin = { serialize(val: string): string { // Return the string itself, not escaped nor enclosed in double quote marks. return val.replace(ansiRegex(), match => {