From e6b66e034a7cc7849753f21ea181897e14673026 Mon Sep 17 00:00:00 2001 From: Gustavo Bastos Date: Wed, 10 Jul 2019 15:36:00 +0200 Subject: [PATCH] Add config for printFunctionName --- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 1 + packages/jest-config/src/index.ts | 1 + packages/jest-jasmine2/src/setup_jest_globals.ts | 1 + 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 | 3 ++- 7 files changed, 12 insertions(+), 4 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..1bddf0a8e64a 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -128,6 +128,7 @@ export const initialize = ({ expand, getBabelTraverse, getPrettier, + printFunctionName: config.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..e30ff18b18df 100644 --- a/packages/jest-jasmine2/src/setup_jest_globals.ts +++ b/packages/jest-jasmine2/src/setup_jest_globals.ts @@ -108,6 +108,7 @@ export default ({ getBabelTraverse: () => require('@babel/traverse').default, getPrettier: () => config.prettierPath ? require(config.prettierPath) : null, + printFunctionName: config.printFunctionName, updateSnapshot, }); setState({snapshotState, testPath}); diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 8c01e9f3c94c..e36f992688ab 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; @@ -76,6 +78,7 @@ export default class SnapshotState { this.unmatched = 0; this._updateSnapshot = options.updateSnapshot; this.updated = 0; + this._printFunctionName = options.printFunctionName; } markSnapshotsAsCheckedForTest(testName: string) { @@ -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..ba5c5d220b64 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: any): 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..39d1695428b7 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -418,6 +418,7 @@ export type ProjectConfig = { transformIgnorePatterns: Array; watchPathIgnorePatterns: Array; unmockedModulePathPatterns: Array | null | undefined; + printFunctionName: boolean; }; export type Argv = Arguments< diff --git a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js index 607ad3f53c84..db2269252085 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, @@ -131,7 +132,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) =>