diff --git a/CHANGELOG.md b/CHANGELOG.md index 32f215270ba9..5a227822b25b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[jest-config]` [**BREAKING**] Make `snapshotFormat` default to `escapeString: false` and `printBasicPrototype: false` ([#13036](https://github.com/facebook/jest/pull/13036)) - `[jest-environment-jsdom]` [**BREAKING**] Upgrade to `jsdom@20` ([#13037](https://github.com/facebook/jest/pull/13037)) +- `[pretty-format]` [**BREAKING**] Remove `ConvertAnsi` plugin in favour of `jest-serializer-ansi-escapes` ([#13040](https://github.com/facebook/jest/pull/13040)) ### Fixes diff --git a/docs/UpgradingToJest29.md b/docs/UpgradingToJest29.md index 4a1633aecc95..182bf0bcb934 100644 --- a/docs/UpgradingToJest29.md +++ b/docs/UpgradingToJest29.md @@ -31,3 +31,7 @@ If you want to keep the old behavior, you can set the `snapshotFormat` property ## JSDOM upgrade `jest-environment-jsdom` has upgraded `jsdom` from v19 to v20. Due to issues with `@types/jsdom`, if you extend this environment, you might run into type errors. See https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60999#discussioncomment-3158685. + +## `pretty-format` + +`ConvertAnsi` plugin is removed in favour of [`jest-serializer-ansi-escapes`](https://github.com/mrazauskas/jest-serializer-ansi-escapes). diff --git a/packages/pretty-format/package.json b/packages/pretty-format/package.json index 9811add9313b..d490ab3037e9 100644 --- a/packages/pretty-format/package.json +++ b/packages/pretty-format/package.json @@ -15,13 +15,11 @@ "types": "./build/index.d.ts", "default": "./build/index.js" }, - "./package.json": "./package.json", - "./ConvertAnsi": "./build/plugins/ConvertAnsi.js" + "./package.json": "./package.json" }, "author": "James Kyle ", "dependencies": { "@jest/schemas": "^29.0.0-alpha.0", - "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", "react-is": "^18.0.0" }, diff --git a/packages/pretty-format/src/__tests__/ConvertAnsi.test.ts b/packages/pretty-format/src/__tests__/ConvertAnsi.test.ts deleted file mode 100644 index aee6f3683f1d..000000000000 --- a/packages/pretty-format/src/__tests__/ConvertAnsi.test.ts +++ /dev/null @@ -1,58 +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. - */ - -import ansiStyle = require('ansi-styles'); -import prettyFormat, {plugins} from '../'; - -const {ConvertAnsi} = plugins; - -const prettyFormatResult = (val: string) => - prettyFormat(val, { - plugins: [ConvertAnsi], - }); - -describe('ConvertAnsi plugin', () => { - it('supports style.red', () => { - expect( - prettyFormatResult( - `${ansiStyle.red.open} foo content ${ansiStyle.red.close}`, - ), - ).toEqual('" foo content "'); - }); - - it('supports style.green', () => { - expect( - prettyFormatResult( - `${ansiStyle.green.open} foo content ${ansiStyle.green.close}`, - ), - ).toEqual('" foo content "'); - }); - - it('supports style.reset', () => { - expect( - prettyFormatResult( - `${ansiStyle.reset.open} foo content ${ansiStyle.reset.close}`, - ), - ).toEqual('" foo content "'); - }); - - it('supports style.bold', () => { - expect(prettyFormatResult(`${ansiStyle.bold.open} foo content`)).toEqual( - '" foo content"', - ); - }); - - it('supports style.dim', () => { - expect(prettyFormatResult(`${ansiStyle.dim.open} foo content`)).toEqual( - '" foo content"', - ); - }); - - it('does not support other colors', () => { - expect(prettyFormatResult(`${ansiStyle.blue.open}`)).toEqual('""'); - }); -}); diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index c2bce5ad3daa..7c26aaa05684 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -15,7 +15,6 @@ import { printObjectProperties, } from './collections'; import AsymmetricMatcher from './plugins/AsymmetricMatcher'; -import ConvertAnsi from './plugins/ConvertAnsi'; import DOMCollection from './plugins/DOMCollection'; import DOMElement from './plugins/DOMElement'; import Immutable from './plugins/Immutable'; @@ -536,7 +535,6 @@ export function format(val: unknown, options?: OptionsReceived): string { export const plugins = { AsymmetricMatcher, - ConvertAnsi, DOMCollection, DOMElement, Immutable, diff --git a/packages/pretty-format/src/plugins/ConvertAnsi.ts b/packages/pretty-format/src/plugins/ConvertAnsi.ts deleted file mode 100644 index a500dad114eb..000000000000 --- a/packages/pretty-format/src/plugins/ConvertAnsi.ts +++ /dev/null @@ -1,73 +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. - */ - -import ansiRegex = require('ansi-regex'); -import style = require('ansi-styles'); -import type {Config, NewPlugin, Printer, Refs} from '../types'; - -const toHumanReadableAnsi = (text: string) => - text.replace(ansiRegex(), match => { - switch (match) { - case style.red.close: - case style.green.close: - case style.cyan.close: - case style.gray.close: - case style.white.close: - case style.yellow.close: - case style.bgRed.close: - case style.bgGreen.close: - case style.bgYellow.close: - case style.inverse.close: - case style.dim.close: - case style.bold.close: - case style.reset.open: - case style.reset.close: - return ''; - case style.red.open: - return ''; - case style.green.open: - return ''; - case style.cyan.open: - return ''; - case style.gray.open: - return ''; - case style.white.open: - return ''; - case style.yellow.open: - return ''; - case style.bgRed.open: - return ''; - case style.bgGreen.open: - return ''; - case style.bgYellow.open: - return ''; - case style.inverse.open: - return ''; - case style.dim.open: - return ''; - case style.bold.open: - return ''; - default: - return ''; - } - }); - -export const test: NewPlugin['test'] = (val: unknown) => - typeof val === 'string' && !!val.match(ansiRegex()); - -export const serialize: NewPlugin['serialize'] = ( - val: string, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -) => printer(toHumanReadableAnsi(val), config, indentation, depth, refs); - -const plugin: NewPlugin = {serialize, test}; - -export default plugin; diff --git a/scripts/buildUtils.mjs b/scripts/buildUtils.mjs index 964dca659eb0..12c3fecbbf63 100644 --- a/scripts/buildUtils.mjs +++ b/scripts/buildUtils.mjs @@ -69,9 +69,6 @@ export function getPackages() { ...(pkg.name === 'expect' ? {'./build/matchers': './build/matchers.js'} : {}), - ...(pkg.name === 'pretty-format' - ? {'./ConvertAnsi': './build/plugins/ConvertAnsi.js'} - : {}), }, `Package "${pkg.name}" does not export correct files`, ); diff --git a/yarn.lock b/yarn.lock index 5b05876bb67e..452c528c23a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17945,7 +17945,6 @@ __metadata: "@types/react": ^17.0.3 "@types/react-is": ^17.0.0 "@types/react-test-renderer": 17.0.2 - ansi-regex: ^5.0.1 ansi-styles: ^5.0.0 expect: ^29.0.0-alpha.0 immutable: ^4.0.0