From 044e84378780851e7855eee494afada581fddcb8 Mon Sep 17 00:00:00 2001 From: Gustavo Bastos Date: Wed, 10 Jul 2019 15:36:00 +0200 Subject: [PATCH 1/2] Add config for printFunctionName --- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 2 ++ packages/jest-config/src/index.ts | 1 + packages/jest-jasmine2/src/setup_jest_globals.ts | 2 ++ packages/jest-snapshot/src/State.ts | 5 ++++- packages/jest-snapshot/src/utils.ts | 4 ++-- packages/jest-types/src/Config.ts | 1 + packages/jest-validate/src/__tests__/fixtures/jestConfig.js | 1 + 7 files changed, 13 insertions(+), 3 deletions(-) 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..9b5207b85572 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 {printFunctionName} = config; const snapshotResolver = buildSnapshotResolver(config); const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { expand, getBabelTraverse, getPrettier, + printFunctionName, updateSnapshot, }); setState({snapshotState, testPath}); diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 9290126323df..be16dd559794 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, + printFunctionName: options.printFunctionName, 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..1aee589296de 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 {printFunctionName} = 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, + printFunctionName, updateSnapshot, }); setState({snapshotState, testPath}); diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 8c01e9f3c94c..b87062748d47 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; + printFunctionName?: boolean; }; export type SnapshotMatchOptions = { @@ -48,6 +49,7 @@ export default class SnapshotState { private _uncheckedKeys: Set; private _getBabelTraverse: () => Function; private _getPrettier: () => null | any; + private _printFunctionName: boolean; added: number; expand: boolean; @@ -73,6 +75,7 @@ export default class SnapshotState { this.expand = options.expand || false; this.added = 0; this.matched = 0; + this._printFunctionName = options.printFunctionName || false; 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._printFunctionName); 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..dc24e9ea48a1 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -128,13 +128,13 @@ export const getSnapshotData = ( const addExtraLineBreaks = (string: string): string => string.includes('\n') ? `\n${string}\n` : string; -export const serialize = (data: string): string => +export const serialize = (data: string, printFunctionName: boolean): string => addExtraLineBreaks( normalizeNewlines( prettyFormat(data, { escapeRegex: true, plugins: getSerializers(), - printFunctionName: false, + printFunctionName, }), ), ); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index f3974dcae59e..eadf0d4cdb11 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -392,6 +392,7 @@ export type ProjectConfig = { modulePaths: Array; name: string; prettierPath: string; + printFunctionName: boolean; 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..02fa70342d41 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js +++ b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js @@ -42,6 +42,7 @@ const defaultConfig = { notifyMode: 'failure-change', preset: null, prettierPath: 'prettier', + printFunctionName: false, resetMocks: false, resetModules: false, restoreMocks: false, From d5341fbcb33b2ba8656bb61cb914bbe35e5da401 Mon Sep 17 00:00:00 2001 From: Gustavo Bastos Date: Thu, 11 Jul 2019 10:11:04 +0200 Subject: [PATCH 2/2] Move printFunctionName to prettyFormat snapshot config --- .../jestAdapterInit.ts | 4 ++-- packages/jest-config/src/index.ts | 2 +- .../jest-jasmine2/src/setup_jest_globals.ts | 4 ++-- packages/jest-snapshot/src/State.ts | 8 +++---- packages/jest-snapshot/src/utils.ts | 21 ++++++++++++++++--- packages/jest-types/src/Config.ts | 7 ++++++- .../src/__tests__/fixtures/jestConfig.js | 11 ++++++++-- 7 files changed, 42 insertions(+), 15 deletions(-) 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..3a9f99fcac89 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,7 @@ export default class SnapshotState { this.expand = options.expand || false; this.added = 0; this.matched = 0; - this._printFunctionName = options.printFunctionName || false; + this._prettyFormatSnapshotConfig = options.prettyFormatSnapshotConfig; this.unmatched = 0; this._updateSnapshot = options.updateSnapshot; this.updated = 0; @@ -192,7 +192,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..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, printFunctionName: boolean): 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, }), ), ); +} // 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 eadf0d4cdb11..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,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..19c25fe3f084 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, @@ -102,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, @@ -132,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) =>