Skip to content
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

chore: migrate pretty-format to ESM #10515

Merged
merged 12 commits into from Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@
- `[jest-runtime]` [**BREAKING**] remove long-deprecated `jest.addMatchers`, `jest.resetModuleRegistry`, and `jest.runTimersToTime` ([#9853](https://github.com/facebook/jest/pull/9853))
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515))

### Chore & Maintenance

Expand Down
2 changes: 1 addition & 1 deletion docs/JestPlatform.md
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -141,6 +141,7 @@
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
},
"resolutions": {
"@testing-library/dom": "patch:@testing-library/dom@*#./patches/dom-testing.diff",
"@types/jest/jest-diff": "^25.1.0",
"@types/jest/pretty-format": "^25.1.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/formatNodeAssertErrors.ts
Expand Up @@ -14,7 +14,7 @@ import {
printExpected,
printReceived,
} from 'jest-matcher-utils';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';

interface AssertionErrorWithStack extends AssertionError {
stack: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/utils.ts
Expand Up @@ -13,7 +13,7 @@ import StackUtils = require('stack-utils');
import type {AssertionResult, Status} from '@jest/test-result';
import type {Circus} from '@jest/types';
import {ErrorWithStack, convertDescriptorToString, formatTime} from 'jest-util';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';
import {ROOT_DESCRIBE_BLOCK_NAME, getState} from './state';

const stackUtils = new StackUtils({cwd: 'A path that does not exist'});
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/Deprecated.ts
Expand Up @@ -7,7 +7,7 @@

import chalk = require('chalk');
import type {DeprecatedOptions} from 'jest-validate';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';

const format = (value: unknown) => prettyFormat(value, {min: true});

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-diff/README.md
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-diff/src/index.ts
Expand Up @@ -7,7 +7,7 @@

import chalk = require('chalk');
import getType = require('jest-get-type');
import prettyFormat = require('pretty-format');
import prettyFormat, {plugins as prettyFormatPlugins} from 'pretty-format';
import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
import {NO_DIFF_MESSAGE, SIMILAR_MESSAGE} from './constants';
import {diffLinesRaw, diffLinesUnified, diffLinesUnified2} from './diffLines';
Expand All @@ -33,7 +33,7 @@ const {
Immutable,
ReactElement,
ReactTestComponent,
} = prettyFormat.plugins;
} = prettyFormatPlugins;

const PLUGINS = [
ReactTestComponent,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-each/src/__tests__/array.test.ts
Expand Up @@ -6,7 +6,7 @@
*
*/

import pretty = require('pretty-format');
import pretty from 'pretty-format';
import each from '../';

const noop = () => {};
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-each/src/table/array.ts
Expand Up @@ -8,7 +8,7 @@

import * as util from 'util';
import type {Global} from '@jest/types';
import pretty = require('pretty-format');
import pretty from 'pretty-format';
import type {EachTests} from '../bind';

const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-each/src/table/template.ts
Expand Up @@ -8,7 +8,7 @@

import type {Global} from '@jest/types';
import {isPrimitive} from 'jest-get-type';
import pretty = require('pretty-format');
import pretty from 'pretty-format';
import type {EachTests} from '../bind';

type Template = Record<string, unknown>;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-each/src/validation.ts
Expand Up @@ -8,7 +8,7 @@

import chalk = require('chalk');
import type {Global} from '@jest/types';
import pretty = require('pretty-format');
import pretty from 'pretty-format';

type TemplateData = Global.TemplateData;

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/expectationResultFactory.ts
Expand Up @@ -6,7 +6,7 @@
*/

import type {FailedAssertion} from '@jest/test-result';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';

function messageFormatter({error, message, passed}: Options) {
if (passed) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/isError.ts
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';

export default function isError(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-leak-detector/src/index.ts
Expand Up @@ -11,7 +11,7 @@ import {promisify} from 'util';
import {setFlagsFromString} from 'v8';
import {runInNewContext} from 'vm';
import {isPrimitive} from 'jest-get-type';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';

const tick = promisify(setImmediate);

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-matcher-utils/src/__tests__/index.test.ts
Expand Up @@ -8,7 +8,7 @@

import chalk = require('chalk');
import {alignedAnsiStyleSerializer} from '@jest/test-utils';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';
import {
MatcherHintOptions,
diff,
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-matcher-utils/src/index.ts
Expand Up @@ -18,7 +18,7 @@ import diffDefault, {
diffStringsUnified,
} from 'jest-diff';
import getType = require('jest-get-type');
import prettyFormat = require('pretty-format');
import prettyFormat, {plugins as prettyFormatPlugins} from 'pretty-format';
import Replaceable from './Replaceable';
import deepCyclicCopyReplaceable from './deepCyclicCopyReplaceable';

Expand All @@ -29,7 +29,7 @@ const {
Immutable,
ReactElement,
ReactTestComponent,
} = prettyFormat.plugins;
} = prettyFormatPlugins;

const PLUGINS = [
ReactTestComponent,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-message-util/src/index.ts
Expand Up @@ -13,7 +13,7 @@ import micromatch = require('micromatch');
import slash = require('slash');
import StackUtils = require('stack-utils');
import type {Config, TestResult} from '@jest/types';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';
import type {Frame} from './types';

export type {Frame} from './types';
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-serializer/src/__tests__/index.test.ts
Expand Up @@ -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 prettyFormat from 'pretty-format';
import serializer from '..';

const objs = [
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-snapshot/src/__tests__/dedentLines.test.ts
Expand Up @@ -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');
Expand Down
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';
import plugin from '../mockSerializer';

test('mock with 0 calls and default name', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-snapshot/src/__tests__/printSnapshot.test.ts
Expand Up @@ -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 {
aBackground2,
aBackground3,
Expand Down
14 changes: 9 additions & 5 deletions packages/jest-snapshot/src/plugins.ts
Expand Up @@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use import type here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we import plugins below, so no

plugins as prettyFormatPlugins,
} from 'pretty-format';
import jestMockSerializer from './mockSerializer';

const {
Expand All @@ -15,9 +19,9 @@ const {
ReactElement,
ReactTestComponent,
AsymmetricMatcher,
} = prettyFormat.plugins;
} = prettyFormatPlugins;

let PLUGINS: prettyFormat.Plugins = [
let PLUGINS: PrettyFormatPlugins = [
ReactTestComponent,
ReactElement,
DOMElement,
Expand All @@ -28,8 +32,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;
2 changes: 1 addition & 1 deletion packages/jest-snapshot/src/printSnapshot.ts
Expand Up @@ -34,7 +34,7 @@ import {
getLabelPrinter,
matcherHint,
} from 'jest-matcher-utils';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';
import {
aBackground2,
aBackground3,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-snapshot/src/utils.ts
Expand Up @@ -10,7 +10,7 @@ import chalk = require('chalk');
import * as fs from 'graceful-fs';
import naturalCompare = require('natural-compare');
import type {Config} from '@jest/types';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';
import {getSerializers} from './plugins';
import type {SnapshotData} from './types';

Expand Down
3 changes: 2 additions & 1 deletion packages/jest-validate/src/__tests__/fixtures/jestConfig.ts
Expand Up @@ -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<string, any>) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-validate/src/utils.ts
Expand Up @@ -7,7 +7,7 @@

import chalk = require('chalk');
import leven from 'leven';
import prettyFormat = require('pretty-format');
import prettyFormat from 'pretty-format';

const BULLET: string = chalk.bold('\u25cf');
export const DEPRECATION = `${BULLET} Deprecation Warning`;
Expand Down
17 changes: 9 additions & 8 deletions packages/pretty-format/README.md
Expand Up @@ -14,11 +14,11 @@ $ yarn add pretty-format
## Usage

```js
const prettyFormat = require('pretty-format'); // CommonJS
const {format: prettyFormat} = require('pretty-format'); // CommonJS
```

```js
import prettyFormat from 'pretty-format'; // ES2015 modules
import {format as prettyFormat} from 'pretty-format'; // ES2015 modules
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm purposefully not using default import here, since real ESM only works with named exports (in node impl) as it's still CJS under the hood

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with just format in examples, without aliasing to prettyFormat. No need for extra mental overhead here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's the difference between "copy the example and run it" vs "copy example, but get weird errors". I think it's worth it to keep as is?

```

```js
Expand Down Expand Up @@ -102,17 +102,18 @@ The `pretty-format` package provides some built-in plugins, including:
// CommonJS
const React = require('react');
const renderer = require('react-test-renderer');
const prettyFormat = require('pretty-format');
const ReactElement = prettyFormat.plugins.ReactElement;
const ReactTestComponent = prettyFormat.plugins.ReactTestComponent;
const {format: prettyFormat, plugins} = require('pretty-format');

const {ReactElement, ReactTestComponent} = plugins;
```

```js
// ES2015 modules and destructuring assignment
import React from 'react';
import renderer from 'react-test-renderer';
// ES2015 modules and destructuring assignment
import prettyFormat from 'pretty-format';
const {ReactElement, ReactTestComponent} = prettyFormat.plugins;
import {plugins, format as prettyFormat} from 'pretty-format';

const {ReactElement, ReactTestComponent} = plugins;
```

```js
Expand Down
Expand Up @@ -5,10 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

import prettyFormat from '../';
import {OptionsReceived} from '../types';
import prettyFormat, {plugins} from '../';
import type {OptionsReceived} from '../types';

const {AsymmetricMatcher} = prettyFormat.plugins;
const {AsymmetricMatcher} = plugins;
let options: OptionsReceived;

function fnNameFor(func: (...any: Array<any>) => unknown) {
Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/src/__tests__/ConvertAnsi.test.ts
Expand Up @@ -6,9 +6,9 @@
*/

import ansiStyle = require('ansi-styles');
import prettyFormat = require('../');
import prettyFormat, {plugins} from '../';

const {ConvertAnsi} = prettyFormat.plugins;
const {ConvertAnsi} = plugins;

const prettyFormatResult = (val: string) =>
prettyFormat(val, {
Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/src/__tests__/DOMCollection.test.ts
Expand Up @@ -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]);

Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/src/__tests__/DOMElement.test.ts
Expand Up @@ -8,10 +8,10 @@
*/
/* eslint-env browser*/

import prettyFormat from '../';
import prettyFormat, {plugins} from '../';
import setPrettyPrint from './setPrettyPrint';

const {DOMElement} = prettyFormat.plugins;
const {DOMElement} = plugins;

setPrettyPrint([DOMElement]);

Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/src/__tests__/Immutable.test.ts
Expand Up @@ -9,10 +9,10 @@

import Immutable from 'immutable';
import React from 'react';
import prettyFormat from '..';
import {plugins} from '..';
import setPrettyPrint from './setPrettyPrint';

const {Immutable: ImmutablePlugin, ReactElement} = prettyFormat.plugins;
const {Immutable: ImmutablePlugin, ReactElement} = plugins;

setPrettyPrint([ReactElement, ImmutablePlugin]);

Expand Down