diff --git a/CHANGELOG.md b/CHANGELOG.md index 6343a947528b..71f92ec53f84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- `[jest-cli]` Adds 2 config options (`inlineSnapshotFormat` and `snapshotFormat`) which offers a way to override any of the formatting settings which come with [pretty-format](https://www.npmjs.com/package/pretty-format#usage-with-options). ([#11654](https://github.com/facebook/jest/pull/11654)) +- `[jest-cli]` Adds a new config options `snapshotFormat` which offers a way to override any of the formatting settings which come with [pretty-format](https://www.npmjs.com/package/pretty-format#usage-with-options). ([#11654](https://github.com/facebook/jest/pull/11654)) ### Fixes diff --git a/docs/Configuration.md b/docs/Configuration.md index 2e83c69888d5..598c8c400501 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -537,43 +537,6 @@ test('some test', () => { _Note: This option is only supported using the default `jest-circus`. test runner_ -### `inlineSnapshotFormat` \[object] - -Default: `undefined` - -Allows overriding specific snapshot formatting options documented in the [pretty-format readme](https://www.npmjs.com/package/pretty-format#usage-with-options). For example, this config would have the inline snapshot formatter not print a prefix for "Object" and "Array": - -```json -{ - "jest": { - "inlineSnapshotFormat": { - "printBasicPrototype": false - } - } -} -``` - -```ts -import {expect, test} from '@jest/globals'; - -test('does not show prototypes for object and array inline', () => { - const object = { - array: [{hello: 'Danger'}], - }; - expect(object).toMatchInlineSnapshot(` -{ - "array": [ - { - "hello": "Danger", - }, - ], -} - `); -}); -``` - -There is a corresponding [`snapshotFormat`](#snapshotformat-object) option for separate file snapshots. - ### `maxConcurrency` \[number] Default: `5` @@ -986,19 +949,37 @@ The number of seconds after which a test is considered as slow and reported as s Default: `undefined` -Allows overriding specific snapshot formatting options documented in the [pretty-format readme](https://www.npmjs.com/package/pretty-format#usage-with-options). For example, this config would have the snapshot formatter which prints with 4 spaces instead of 2: +Allows overriding specific snapshot formatting options documented in the [pretty-format readme](https://www.npmjs.com/package/pretty-format#usage-with-options). For example, this config would have the snapshot formatter not print a prefix for "Object" and "Array": ```json { "jest": { "snapshotFormat": { - "indent": 4 + "printBasicPrototype": false } } } ``` -There is a corresponding [`inlineSnapshotFormat`](#inlinesnapshotformat-object) option for inline snapshots. +```ts +import {expect, test} from '@jest/globals'; + +test('does not show prototypes for object and array inline', () => { + const object = { + array: [{hello: 'Danger'}], + }; + expect(object).toMatchInlineSnapshot(` +{ + "array": [ + { + "hello": "Danger", + }, + ], +} + `); +}); +``` + ### `snapshotResolver` \[string] diff --git a/e2e/snapshot-formatting-changes/__tests__/snapshot.test.js b/e2e/snapshot-formatting-changes/__tests__/snapshot.test.js index ea453006d281..9df05f7b4ee3 100644 --- a/e2e/snapshot-formatting-changes/__tests__/snapshot.test.js +++ b/e2e/snapshot-formatting-changes/__tests__/snapshot.test.js @@ -14,11 +14,11 @@ describe('snapshot serializer', () => { }; expect(object).toMatchInlineSnapshot(` { - "array": [ - { - "hello": "Danger", - }, - ], + "array": [ + { + "hello": "Danger", + }, + ], } `); }); diff --git a/e2e/snapshot-formatting-changes/package.json b/e2e/snapshot-formatting-changes/package.json index f47aed15e05f..04b08db87937 100644 --- a/e2e/snapshot-formatting-changes/package.json +++ b/e2e/snapshot-formatting-changes/package.json @@ -1,9 +1,6 @@ { "jest": { "testEnvironment": "node", - "inlineSnapshotFormat": { - "printBasicPrototype": false - }, "snapshotFormat": { "printBasicPrototype": false, "indent": 8 diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index 8e7b18624a30..d7487477e31d 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -155,7 +155,6 @@ export const initialize = async ({ const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { expand, - inlineSnapshotFormat: config.inlineSnapshotFormat, prettierPath: config.prettierPath, snapshotFormat: config.snapshotFormat, updateSnapshot, diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index 2e92084aee14..932e7737d904 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -67,7 +67,6 @@ const initialOptions: Config.InitialOptions = { throwOnModuleCollision: false, }, injectGlobals: true, - inlineSnapshotFormat: PRETTY_FORMAT_DEFAULTS, json: false, lastCommit: false, listTests: false, diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 071ac60e1ad3..364346ecf954 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -127,7 +127,6 @@ const groupOptions = ( forceExit: options.forceExit, globalSetup: options.globalSetup, globalTeardown: options.globalTeardown, - inlineSnapshotFormat: options.inlineSnapshotFormat, json: options.json, lastCommit: options.lastCommit, listTests: options.listTests, @@ -186,7 +185,6 @@ const groupOptions = ( globals: options.globals, haste: options.haste, injectGlobals: options.injectGlobals, - inlineSnapshotFormat: options.inlineSnapshotFormat, moduleDirectories: options.moduleDirectories, moduleFileExtensions: options.moduleFileExtensions, moduleLoader: options.moduleLoader, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 18c3afda778d..eee99b8f78be 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -970,7 +970,6 @@ export default async function normalize( case 'extensionsToTreatAsEsm': case 'extraGlobals': case 'globals': - case 'inlineSnapshotFormat': case 'findRelatedTests': case 'forceCoverageMatch': case 'forceExit': diff --git a/packages/jest-jasmine2/src/setup_jest_globals.ts b/packages/jest-jasmine2/src/setup_jest_globals.ts index 9bd324aa3c49..7fc6329aa317 100644 --- a/packages/jest-jasmine2/src/setup_jest_globals.ts +++ b/packages/jest-jasmine2/src/setup_jest_globals.ts @@ -106,12 +106,11 @@ export default async ({ patchJasmine(); const {expand, updateSnapshot} = globalConfig; - const {prettierPath, inlineSnapshotFormat, snapshotFormat} = config; + const {prettierPath, snapshotFormat} = config; const snapshotResolver = await buildSnapshotResolver(config, localRequire); const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { expand, - inlineSnapshotFormat, prettierPath, snapshotFormat, updateSnapshot, diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 8fe2a93473b8..7ee2197958ae 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -27,7 +27,6 @@ export type SnapshotStateOptions = { prettierPath: Config.Path; expand?: boolean; snapshotFormat: PrettyFormatOptions; - inlineSnapshotFormat: PrettyFormatOptions; }; export type SnapshotMatchOptions = { @@ -65,7 +64,6 @@ export default class SnapshotState { private _uncheckedKeys: Set; private _prettierPath: Config.Path; private _snapshotFormat: PrettyFormatOptions; - private _inlineSnapshotFormat: PrettyFormatOptions; added: number; expand: boolean; @@ -95,7 +93,6 @@ export default class SnapshotState { this.updated = 0; this._snapshotFormat = options.snapshotFormat; - this._inlineSnapshotFormat = options.inlineSnapshotFormat; } markSnapshotsAsCheckedForTest(testName: string): void { @@ -209,12 +206,8 @@ export default class SnapshotState { this._uncheckedKeys.delete(key); } - const customFormat = isInline - ? this._inlineSnapshotFormat - : this._snapshotFormat; - const receivedSerialized = addExtraLineBreaks( - serialize(received, undefined, customFormat), + serialize(received, undefined, this._snapshotFormat), ); const expected = isInline ? inlineSnapshot : this._snapshotData[key]; const pass = expected === receivedSerialized; diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index d6fb20a769b5..42b2487c620b 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -174,7 +174,6 @@ export type InitialOptions = Partial<{ globalTeardown: string | null | undefined; haste: HasteConfig; injectGlobals: boolean; - inlineSnapshotFormat: PrettyFormatOptions; reporters: Array; logHeapUsage: boolean; lastCommit: boolean; @@ -297,7 +296,6 @@ export type GlobalConfig = { json: boolean; globalSetup?: string; globalTeardown?: string; - inlineSnapshotFormat: PrettyFormatOptions; lastCommit: boolean; logHeapUsage: boolean; listTests: boolean; @@ -359,7 +357,6 @@ export type ProjectConfig = { globalTeardown?: string; globals: ConfigGlobals; haste: HasteConfig; - inlineSnapshotFormat: PrettyFormatOptions; injectGlobals: boolean; moduleDirectories: Array; moduleFileExtensions: Array; diff --git a/packages/test-utils/src/config.ts b/packages/test-utils/src/config.ts index 2182428494be..9a97135d8d77 100644 --- a/packages/test-utils/src/config.ts +++ b/packages/test-utils/src/config.ts @@ -27,7 +27,6 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = { forceExit: false, globalSetup: undefined, globalTeardown: undefined, - inlineSnapshotFormat: {}, json: false, lastCommit: false, listTests: false, @@ -86,7 +85,6 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = { globals: {}, haste: {}, injectGlobals: true, - inlineSnapshotFormat: {}, moduleDirectories: [], moduleFileExtensions: ['js'], moduleLoader: '/test_module_loader_path',