From 752fa81b5d1930d23d2c39e3b764beeee500295b Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sat, 10 Jul 2021 18:12:54 +0100 Subject: [PATCH] Adds an option for having inline snapshot serializers use the printBasicPrototype option to show literals as literals --- .../__tests__/snapshot.test.js | 25 +++++++++++++++++++ .../package.json | 6 +++++ .../jestAdapterInit.ts | 1 + packages/jest-config/src/ValidConfig.ts | 4 +++ packages/jest-config/src/index.ts | 2 ++ packages/jest-snapshot/src/State.ts | 11 +++++++- packages/jest-snapshot/src/utils.ts | 7 +++++- packages/jest-types/src/Config.ts | 3 +++ packages/test-utils/src/config.ts | 2 ++ 9 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 e2e/snapshot-inline-basic-formatting/__tests__/snapshot.test.js create mode 100644 e2e/snapshot-inline-basic-formatting/package.json diff --git a/e2e/snapshot-inline-basic-formatting/__tests__/snapshot.test.js b/e2e/snapshot-inline-basic-formatting/__tests__/snapshot.test.js new file mode 100644 index 000000000000..59801e13e7ef --- /dev/null +++ b/e2e/snapshot-inline-basic-formatting/__tests__/snapshot.test.js @@ -0,0 +1,25 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ +'use strict'; + +describe('inline snapshot serializer', () => { + it('does not show prototypes for object and array', () => { + const object = { + array: [{hello: 'Danger'}], + }; + expect(object).toMatchInlineSnapshot(` +{ + "array": [ + { + "hello": "Danger", + }, + ], +} +`); + }); +}); diff --git a/e2e/snapshot-inline-basic-formatting/package.json b/e2e/snapshot-inline-basic-formatting/package.json new file mode 100644 index 000000000000..522b300e4599 --- /dev/null +++ b/e2e/snapshot-inline-basic-formatting/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "inlineSnapshotFormatter": "simple" + } +} 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 ed5f39923b11..7413dc3bec1b 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -155,6 +155,7 @@ export const initialize = async ({ const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { expand, + preferSimpleForInline: config.inlineSnapshotFormatter === 'simple', prettierPath: config.prettierPath, updateSnapshot, }); diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index 24fa9cffbe5c..1a1db0209681 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -66,6 +66,10 @@ const initialOptions: Config.InitialOptions = { throwOnModuleCollision: false, }, injectGlobals: true, + inlineSnapshotFormatter: multipleValidOptions( + 'exposed prototypes', + 'simple', + ) as any, json: false, lastCommit: false, listTests: false, diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index cc0e0a702d7a..79208ee50a87 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -127,6 +127,7 @@ const groupOptions = ( forceExit: options.forceExit, globalSetup: options.globalSetup, globalTeardown: options.globalTeardown, + inlineSnapshotFormatter: options.inlineSnapshotFormatter, json: options.json, lastCommit: options.lastCommit, listTests: options.listTests, @@ -184,6 +185,7 @@ const groupOptions = ( globals: options.globals, haste: options.haste, injectGlobals: options.injectGlobals, + inlineSnapshotFormatter: options.inlineSnapshotFormatter, moduleDirectories: options.moduleDirectories, moduleFileExtensions: options.moduleFileExtensions, moduleLoader: options.moduleLoader, diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 6e143ef2d10e..62febf9c13f0 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -25,6 +25,7 @@ export type SnapshotStateOptions = { updateSnapshot: Config.SnapshotUpdateState; prettierPath: Config.Path; expand?: boolean; + preferSimpleForInline?: boolean; }; export type SnapshotMatchOptions = { @@ -61,6 +62,7 @@ export default class SnapshotState { private _inlineSnapshots: Array; private _uncheckedKeys: Set; private _prettierPath: Config.Path; + private _preferSimpleInlineSnapshots: boolean; added: number; expand: boolean; @@ -88,6 +90,7 @@ export default class SnapshotState { this.unmatched = 0; this._updateSnapshot = options.updateSnapshot; this.updated = 0; + this._preferSimpleInlineSnapshots = options.preferSimpleForInline || false; } markSnapshotsAsCheckedForTest(testName: string): void { @@ -201,7 +204,13 @@ export default class SnapshotState { this._uncheckedKeys.delete(key); } - const receivedSerialized = addExtraLineBreaks(serialize(received)); + const hasOptedInToSimpleInline = + isInline && this._preferSimpleInlineSnapshots; + debugger; + console.log({hasOptedInToSimpleInline, isInline}); + const receivedSerialized = addExtraLineBreaks( + serialize(received, undefined, hasOptedInToSimpleInline), + ); const expected = isInline ? inlineSnapshot : this._snapshotData[key]; const pass = expected === receivedSerialized; const hasSnapshot = expected !== undefined; diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index fa8b22cb1d45..7eb6c45b3383 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -152,12 +152,17 @@ export const removeLinesBeforeExternalMatcherTrap = (stack: string): string => { const escapeRegex = true; const printFunctionName = false; -export const serialize = (val: unknown, indent = 2): string => +export const serialize = ( + val: unknown, + indent = 2, + printBasicPrototype: boolean = true, +): string => normalizeNewlines( prettyFormat(val, { escapeRegex, indent, plugins: getSerializers(), + printBasicPrototype, printFunctionName, }), ); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 68eaa4124a01..e0df4c9fbeb9 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -171,6 +171,7 @@ export type InitialOptions = Partial<{ globalTeardown: string | null | undefined; haste: HasteConfig; injectGlobals: boolean; + inlineSnapshotFormatter: 'exposed prototypes' | 'simple'; reporters: Array; logHeapUsage: boolean; lastCommit: boolean; @@ -292,6 +293,7 @@ export type GlobalConfig = { json: boolean; globalSetup?: string; globalTeardown?: string; + inlineSnapshotFormatter: 'exposed prototypes' | 'simple'; lastCommit: boolean; logHeapUsage: boolean; listTests: boolean; @@ -352,6 +354,7 @@ export type ProjectConfig = { globalTeardown?: string; globals: ConfigGlobals; haste: HasteConfig; + inlineSnapshotFormatter: 'exposed prototypes' | 'simple'; injectGlobals: boolean; moduleDirectories: Array; moduleFileExtensions: Array; diff --git a/packages/test-utils/src/config.ts b/packages/test-utils/src/config.ts index e1f54e2ee355..d4de7a34af8c 100644 --- a/packages/test-utils/src/config.ts +++ b/packages/test-utils/src/config.ts @@ -27,6 +27,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = { forceExit: false, globalSetup: undefined, globalTeardown: undefined, + inlineSnapshotFormatter: 'exposed prototypes', json: false, lastCommit: false, listTests: false, @@ -84,6 +85,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = { globals: {}, haste: {}, injectGlobals: true, + inlineSnapshotFormatter: 'exposed prototypes', moduleDirectories: [], moduleFileExtensions: ['js'], moduleLoader: '/test_module_loader_path',