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 9b5207b85572..4b2e1704c08d 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -122,14 +122,14 @@ export const initialize = ({ }); const {expand, updateSnapshot} = globalConfig; - const {printFunctionName} = config; + const {prettyFormatSnapshotConfig} = config; const snapshotResolver = buildSnapshotResolver(config); const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { expand, getBabelTraverse, getPrettier, - printFunctionName, + prettyFormatSnapshotConfig, updateSnapshot, }); setState({snapshotState, testPath}); diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index be16dd559794..39b01a3e9c6a 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -186,7 +186,7 @@ const groupOptions = ( modulePaths: options.modulePaths, name: options.name, prettierPath: options.prettierPath, - printFunctionName: options.printFunctionName, + prettyFormatSnapshotConfig: options.prettyFormatSnapshotConfig, resetMocks: options.resetMocks, resetModules: options.resetModules, resolver: options.resolver, diff --git a/packages/jest-jasmine2/src/setup_jest_globals.ts b/packages/jest-jasmine2/src/setup_jest_globals.ts index 1aee589296de..9449d8d33163 100644 --- a/packages/jest-jasmine2/src/setup_jest_globals.ts +++ b/packages/jest-jasmine2/src/setup_jest_globals.ts @@ -101,7 +101,7 @@ export default ({ patchJasmine(); const {expand, updateSnapshot} = globalConfig; - const {printFunctionName} = config; + const {prettyFormatSnapshotConfig} = config; const snapshotResolver = buildSnapshotResolver(config); const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { @@ -109,7 +109,7 @@ export default ({ getBabelTraverse: () => require('@babel/traverse').default, getPrettier: () => config.prettierPath ? require(config.prettierPath) : null, - printFunctionName, + prettyFormatSnapshotConfig, updateSnapshot, }); setState({snapshotState, testPath}); diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index b87062748d47..eea55f69f84e 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -25,7 +25,7 @@ export type SnapshotStateOptions = { getPrettier: () => null | any; getBabelTraverse: () => Function; expand?: boolean; - printFunctionName?: boolean; + prettyFormatSnapshotConfig?: Config.PrettyFormatSnapshotConfig; }; export type SnapshotMatchOptions = { @@ -49,7 +49,7 @@ export default class SnapshotState { private _uncheckedKeys: Set; private _getBabelTraverse: () => Function; private _getPrettier: () => null | any; - private _printFunctionName: boolean; + private _prettyFormatSnapshotConfig: Config.PrettyFormatSnapshotConfig; added: number; expand: boolean; @@ -75,7 +75,10 @@ export default class SnapshotState { this.expand = options.expand || false; this.added = 0; this.matched = 0; - this._printFunctionName = options.printFunctionName || false; + this._prettyFormatSnapshotConfig = options.prettyFormatSnapshotConfig || { + escapeRegex: true, + printFunctionName: false, + }; this.unmatched = 0; this._updateSnapshot = options.updateSnapshot; this.updated = 0; @@ -192,7 +195,7 @@ export default class SnapshotState { this._uncheckedKeys.delete(key); } - const receivedSerialized = serialize(received, this._printFunctionName); + const receivedSerialized = serialize(received, this._prettyFormatSnapshotConfig); const expected = isInline ? inlineSnapshot : this._snapshotData[key]; const pass = expected === receivedSerialized; const hasSnapshot = isInline diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index dc24e9ea48a1..36a305d2b31c 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -128,13 +128,16 @@ export const getSnapshotData = ( const addExtraLineBreaks = (string: string): string => string.includes('\n') ? `\n${string}\n` : string; -export const serialize = (data: string, printFunctionName: boolean): string => +export const serialize = ( + data: string, + prettyFormatSnapshotConfig: Config.PrettyFormatSnapshotConfig, +): string => addExtraLineBreaks( normalizeNewlines( prettyFormat(data, { - escapeRegex: true, plugins: getSerializers(), - printFunctionName, + escapeRegex: prettyFormatSnapshotConfig.escapeRegex, + printFunctionName: prettyFormatSnapshotConfig.printFunctionName, }), ), ); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index eadf0d4cdb11..7f3e5bc29bb0 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -24,6 +24,11 @@ export type HasteConfig = { export type ReporterConfig = [string, Record]; export type TransformerConfig = [string, Record]; +export type PrettyFormatSnapshotConfig = { + printFunctionName?: boolean; + escapeRegex?: boolean; +} + export type ConfigGlobals = Record; export type DefaultOptions = { @@ -392,7 +397,7 @@ export type ProjectConfig = { modulePaths: Array; name: string; prettierPath: string; - printFunctionName: boolean; + prettyFormatSnapshotConfig: PrettyFormatSnapshotConfig; resetMocks: boolean; resetModules: boolean; resolver: Path | null | undefined; diff --git a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js index 02fa70342d41..27f01920e3da 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js +++ b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js @@ -42,7 +42,10 @@ const defaultConfig = { notifyMode: 'failure-change', preset: null, prettierPath: 'prettier', - printFunctionName: false, + prettyFormatSnapshotConfig: { + escapeRegex: true, + printFunctionName: false, + }, resetMocks: false, resetModules: false, restoreMocks: false,