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 eba12e58e12c..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,12 +122,14 @@ export const initialize = ({ }); const {expand, updateSnapshot} = globalConfig; + const {prettyFormatSnapshotConfig} = config; const snapshotResolver = buildSnapshotResolver(config); const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { expand, getBabelTraverse, getPrettier, + prettyFormatSnapshotConfig, updateSnapshot, }); setState({snapshotState, testPath}); diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 9290126323df..39b01a3e9c6a 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -186,6 +186,7 @@ const groupOptions = ( modulePaths: options.modulePaths, name: options.name, prettierPath: options.prettierPath, + 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 e66580c8bba6..9449d8d33163 100644 --- a/packages/jest-jasmine2/src/setup_jest_globals.ts +++ b/packages/jest-jasmine2/src/setup_jest_globals.ts @@ -101,6 +101,7 @@ export default ({ patchJasmine(); const {expand, updateSnapshot} = globalConfig; + const {prettyFormatSnapshotConfig} = config; const snapshotResolver = buildSnapshotResolver(config); const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { @@ -108,6 +109,7 @@ export default ({ getBabelTraverse: () => require('@babel/traverse').default, getPrettier: () => config.prettierPath ? require(config.prettierPath) : null, + prettyFormatSnapshotConfig, updateSnapshot, }); setState({snapshotState, testPath}); diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 8c01e9f3c94c..3a9f99fcac89 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -25,6 +25,7 @@ export type SnapshotStateOptions = { getPrettier: () => null | any; getBabelTraverse: () => Function; expand?: boolean; + prettyFormatSnapshotConfig?: Config.PrettyFormatSnapshotConfig; }; export type SnapshotMatchOptions = { @@ -48,6 +49,7 @@ export default class SnapshotState { private _uncheckedKeys: Set; private _getBabelTraverse: () => Function; private _getPrettier: () => null | any; + private _prettyFormatSnapshotConfig?: Config.PrettyFormatSnapshotConfig; added: number; expand: boolean; @@ -73,6 +75,7 @@ export default class SnapshotState { this.expand = options.expand || false; this.added = 0; this.matched = 0; + this._prettyFormatSnapshotConfig = options.prettyFormatSnapshotConfig; this.unmatched = 0; this._updateSnapshot = options.updateSnapshot; this.updated = 0; @@ -189,7 +192,7 @@ export default class SnapshotState { this._uncheckedKeys.delete(key); } - const receivedSerialized = serialize(received); + 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 1a1524d2e039..97d2f6b2043f 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -128,16 +128,31 @@ export const getSnapshotData = ( const addExtraLineBreaks = (string: string): string => string.includes('\n') ? `\n${string}\n` : string; -export const serialize = (data: string): string => - addExtraLineBreaks( +export const serialize = ( + data: string, + config?: Config.PrettyFormatSnapshotConfig, +): string => { + let escapeRegex = true; + let printFunctionName = false; + + if (config && config.escapeRegex) { + escapeRegex = config.escapeRegex; + } + + if (config && config.printFunctionName) { + printFunctionName = config.printFunctionName; + } + + return addExtraLineBreaks( normalizeNewlines( prettyFormat(data, { - escapeRegex: true, + escapeRegex, plugins: getSerializers(), - printFunctionName: false, + printFunctionName, }), ), ); +} // unescape double quotes export const unescape = (data: string): string => data.replace(/\\(")/g, '$1'); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index f3974dcae59e..e2291f5f6cf3 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 = { + escapeRegex?: boolean; + printFunctionName?: boolean; +}; + export type ConfigGlobals = Record; export type DefaultOptions = { @@ -392,6 +397,7 @@ export type ProjectConfig = { modulePaths: Array; name: string; prettierPath: string; + 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 607ad3f53c84..19c25fe3f084 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js +++ b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js @@ -42,6 +42,10 @@ const defaultConfig = { notifyMode: 'failure-change', preset: null, prettierPath: 'prettier', + prettyFormatSnapshotConfig: { + escapeRegex: true, + printFunctionName: false, + }, resetMocks: false, resetModules: false, restoreMocks: false, @@ -101,6 +105,10 @@ const validConfig = { notifyMode: 'failure-change', preset: 'react-native', prettierPath: '/node_modules/prettier', + prettyFormatSnapshotConfig: { + escapeRegex: true, + printFunctionName: false, + }, resetMocks: false, resetModules: false, restoreMocks: false, @@ -131,7 +139,7 @@ const validConfig = { watchman: true, }; -const format = (value: string) => require('pretty-format')(value, {min: true}); +const format = (value: string) => require('pretty-format')(value, { min: true }); const deprecatedConfig = { preprocessorIgnorePatterns: (config: Object) =>