From c91dbcfb786c6d719fb5c15832cd7efa1c12a660 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 17 Jul 2022 22:34:44 +0200 Subject: [PATCH] feat(jest-config): set default `snapshotFormat` (#13036) --- CHANGELOG.md | 2 + docs/Configuration.md | 2 +- .../__snapshots__/showConfig.test.ts.snap | 8 ++++ .../snapshotSerializers.test.ts.snap | 14 +++---- .../toMatchInlineSnapshot.test.ts.snap | 38 +++++++++---------- e2e/__tests__/failureDetailsProperty.test.ts | 12 +++--- e2e/__tests__/snapshotMockFs.test.ts | 2 +- e2e/__tests__/toMatchInlineSnapshot.test.ts | 2 +- .../__snapshots__/intro.test.js.snap | 12 +++--- jest.config.mjs | 2 +- packages/jest-config/src/Defaults.ts | 1 + packages/jest-config/src/normalize.ts | 6 ++- packages/jest-types/src/Config.ts | 1 + 13 files changed, 59 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ae2a9be3ef0..3a3c6889c13a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-config]` [**BREAKING**] Make `snapshotFormat` default to `escapeString: false` and `printBasicPrototype: false` ([#13036](https://github.com/facebook/jest/pull/13036)) + ### Fixes ### Chore & Maintenance diff --git a/docs/Configuration.md b/docs/Configuration.md index ffc4b4f35b6a..75028672b31c 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1614,7 +1614,7 @@ The number of seconds after which a test is considered as slow and reported as s ### `snapshotFormat` \[object] -Default: `undefined` +Default: `{escapeString: false, printBasicPrototype: false}` Allows overriding specific snapshot formatting options documented in the [pretty-format readme](https://www.npmjs.com/package/pretty-format#usage-with-options), with the exceptions of `compareKeys` and `plugins`. For example, this config would have the snapshot formatter not print a prefix for "Object" and "Array": diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 82e8281b2cad..5bedd1a8bd54 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -58,6 +58,10 @@ exports[`--showConfig outputs config info and exits 1`] = ` "setupFilesAfterEnv": [], "skipFilter": false, "slowTestThreshold": 5, + "snapshotFormat": { + "escapeString": false, + "printBasicPrototype": false + }, "snapshotSerializers": [], "testEnvironment": "<>/jest-environment-node/build/index.js", "testEnvironmentOptions": {}, @@ -122,6 +126,10 @@ exports[`--showConfig outputs config info and exits 1`] = ` "rootDir": "<>", "runTestsByPath": false, "skipFilter": false, + "snapshotFormat": { + "escapeString": false, + "printBasicPrototype": false + }, "testFailureExitCode": 1, "testPathPattern": "", "testSequencer": "<>/jest-test-sequencer/build/index.js", diff --git a/e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap b/e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap index 0bac5f419d34..5b7debfa1087 100644 --- a/e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap +++ b/e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap @@ -3,8 +3,8 @@ exports[`Snapshot serializers renders snapshot 1`] = ` Object { "snapshot serializers works with array of strings in property matcher 1": " -Object { - "arrayOfStrings": Array [ +{ + "arrayOfStrings": [ "stream", ], } @@ -15,8 +15,8 @@ Object { /> ", "snapshot serializers works with expect.XXX within array in property matcher 1": " -Object { - "arrayOfStrings": Array [ +{ + "arrayOfStrings": [ Any, ], } @@ -26,7 +26,7 @@ Object { "snapshot serializers works with prepended plugins and default serializers 1": "
expect({apple: 'original value'}).toMatchInlineSnapshot(\` - Object { + { "apple": "original value", } \`)); @@ -13,7 +13,7 @@ exports[`basic support: initial write 1`] = ` exports[`basic support: snapshot mismatch 1`] = ` "test('inline snapshots', () => expect({apple: 'updated value'}).toMatchInlineSnapshot(\` - Object { + { "apple": "original value", } \`));" @@ -22,7 +22,7 @@ expect({apple: 'updated value'}).toMatchInlineSnapshot(\` exports[`basic support: snapshot passed 1`] = ` "test('inline snapshots', () => expect({apple: 'original value'}).toMatchInlineSnapshot(\` - Object { + { "apple": "original value", } \`)); @@ -32,7 +32,7 @@ exports[`basic support: snapshot passed 1`] = ` exports[`basic support: snapshot updated 1`] = ` "test('inline snapshots', () => expect({apple: 'updated value'}).toMatchInlineSnapshot(\` - Object { + { "apple": "updated value", } \`)); @@ -68,7 +68,7 @@ exports[`handles property matchers: initial write 1`] = ` expect({createdAt: new Date()}).toMatchInlineSnapshot( {createdAt: expect.any(Date)}, \` - Object { + { "createdAt": Any, } \`, @@ -82,7 +82,7 @@ exports[`handles property matchers: snapshot failed 1`] = ` expect({createdAt: "string"}).toMatchInlineSnapshot( {createdAt: expect.any(Date)}, \` - Object { + { "createdAt": Any, } \`, @@ -95,7 +95,7 @@ exports[`handles property matchers: snapshot passed 1`] = ` expect({createdAt: new Date()}).toMatchInlineSnapshot( {createdAt: expect.any(Date)}, \` - Object { + { "createdAt": Any, } \`, @@ -109,7 +109,7 @@ exports[`handles property matchers: snapshot updated 1`] = ` expect({createdAt: 'string'}).toMatchInlineSnapshot( {createdAt: expect.any(String)}, \` - Object { + { "createdAt": Any, } \`, @@ -122,7 +122,7 @@ exports[`indentation is correct in the presences of existing snapshots, when the "it('is true', () => { expect(true).toMatchInlineSnapshot(\`true\`); expect([1, 2, 3]).toMatchInlineSnapshot(\` - Array [ + [ 1, 2, 3, @@ -135,12 +135,12 @@ exports[`indentation is correct in the presences of existing snapshots, when the exports[`indentation is correct in the presences of existing snapshots: existing snapshot 1`] = ` "test('existing snapshot', () => { expect({hello: 'world'}).toMatchInlineSnapshot(\` - Object { + { "hello": "world", } \`); expect({hello: 'world'}).toMatchInlineSnapshot(\` - Object { + { "hello": "world", } \`); @@ -160,22 +160,22 @@ expect.extend({ }); test('inline snapshots', () => { expect({apple: 'value 1'}).toMatchCustomInlineSnapshot(\` - Object { + { "apple": "value 1", } \`); expect({apple: 'value 2'}).toMatchInlineSnapshot(\` - Object { + { "apple": "value 2", } \`); expect({apple: 'value 3'}).toMatchCustomInlineSnapshot2(\` - Object { + { "apple": "value 3", } \`); expect({apple: 'value 4'}).toMatchInlineSnapshot(\` - Object { + { "apple": "value 4", } \`); @@ -251,7 +251,7 @@ test('inline snapshots', () => { id: expect.any(Number), }, \` - Object { + { "createdAt": Any, "id": Any, "name": "LeBron James", @@ -259,7 +259,7 @@ test('inline snapshots', () => { \`, ); expect(user).toMatchUserInlineSnapshot(\` - Object { + { "createdAt": Any, "id": Any, "name": "LeBron James", @@ -278,7 +278,7 @@ expect.extend({ }); test('inline snapshots', () => { expect({apple: 'original value'}).toMatchCustomInlineSnapshot(\` - Object { + { "apple": "original value", } \`); @@ -289,7 +289,7 @@ test('inline snapshots', () => { exports[`writes snapshots with non-literals in expect(...) 1`] = ` "it('works with inline snapshots', () => { expect({a: 1}).toMatchInlineSnapshot(\` - Object { + { "a": 1, } \`); diff --git a/e2e/__tests__/failureDetailsProperty.test.ts b/e2e/__tests__/failureDetailsProperty.test.ts index 05dcc677cb81..ee301386e088 100644 --- a/e2e/__tests__/failureDetailsProperty.test.ts +++ b/e2e/__tests__/failureDetailsProperty.test.ts @@ -90,7 +90,7 @@ test('that the failureDetails property is set', () => { "actual": "", "error": Object { "matcherResult": Object { - "actual": "Object { + "actual": "{ "p1": "hello", "p2": "world", }", @@ -105,7 +105,7 @@ test('that the failureDetails property is set', () => { - Snapshot - 1 + Received + 1 - Object { + { "p1": "hello", - "p2": "sunshine", + "p2": "world", @@ -123,7 +123,7 @@ test('that the failureDetails property is set', () => { - Snapshot - 1 + Received + 1 - Object { + { "p1": "hello", - "p2": "sunshine", + "p2": "world", @@ -136,7 +136,7 @@ test('that the failureDetails property is set', () => { - Snapshot - 1 + Received + 1 - Object { + { "p1": "hello", - "p2": "sunshine", + "p2": "world", @@ -215,7 +215,7 @@ test('that the failureDetails property is set', () => { Array [ Object { "matcherResult": Object { - "actual": "Object { + "actual": "{ "p1": "hello", "p2": "world", }", @@ -230,7 +230,7 @@ test('that the failureDetails property is set', () => { - Snapshot - 1 + Received + 1 - Object { + { "p1": "hello", - "p2": "sunshine", + "p2": "world", diff --git a/e2e/__tests__/snapshotMockFs.test.ts b/e2e/__tests__/snapshotMockFs.test.ts index 299eb9933422..8622ce90ba53 100644 --- a/e2e/__tests__/snapshotMockFs.test.ts +++ b/e2e/__tests__/snapshotMockFs.test.ts @@ -29,7 +29,7 @@ test('store snapshot even if fs is mocked', () => { const content = require(snapshotFile); expect(content['snapshot 1']).toBe(` -Object { +{ "foo": "bar", } `); diff --git a/e2e/__tests__/toMatchInlineSnapshot.test.ts b/e2e/__tests__/toMatchInlineSnapshot.test.ts index b160f35e21e4..d85d50b15af5 100644 --- a/e2e/__tests__/toMatchInlineSnapshot.test.ts +++ b/e2e/__tests__/toMatchInlineSnapshot.test.ts @@ -364,7 +364,7 @@ test('indentation is correct in the presences of existing snapshots', () => { const test = ` test('existing snapshot', () => { expect({ hello: 'world' }).toMatchInlineSnapshot(\` - Object { + { "hello": "world", } \`); diff --git a/examples/react-native/__tests__/__snapshots__/intro.test.js.snap b/examples/react-native/__tests__/__snapshots__/intro.test.js.snap index 75cbe11b5833..3f393327f852 100644 --- a/examples/react-native/__tests__/__snapshots__/intro.test.js.snap +++ b/examples/react-native/__tests__/__snapshots__/intro.test.js.snap @@ -3,7 +3,7 @@ exports[`renders correctly 1`] = ` ', '/examples/*/'], snapshotFormat: { - escapeString: false, + printBasicPrototype: true, }, snapshotSerializers: [require.resolve('jest-serializer-ansi-escapes')], testPathIgnorePatterns: [ diff --git a/packages/jest-config/src/Defaults.ts b/packages/jest-config/src/Defaults.ts index cb85437b3593..16ed155a1d26 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -72,6 +72,7 @@ const defaultOptions: Config.DefaultOptions = { setupFilesAfterEnv: [], skipFilter: false, slowTestThreshold: 5, + snapshotFormat: {escapeString: false, printBasicPrototype: false}, snapshotSerializers: [], testEnvironment: 'jest-environment-node', testEnvironmentOptions: {}, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index e54011a8752a..52e67fc288a2 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -909,6 +909,11 @@ export default async function normalize( value = oldOptions[key]; break; } + case 'snapshotFormat': { + value = {...DEFAULT_CONFIG.snapshotFormat, ...oldOptions[key]}; + + break; + } case 'automock': case 'cache': case 'changedSince': @@ -952,7 +957,6 @@ export default async function normalize( case 'skipFilter': case 'skipNodeResolution': case 'slowTestThreshold': - case 'snapshotFormat': case 'testEnvironment': case 'testEnvironmentOptions': case 'testFailureExitCode': diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 1d3f19457b95..e7854413bfd6 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -190,6 +190,7 @@ export type DefaultOptions = { setupFilesAfterEnv: Array; skipFilter: boolean; slowTestThreshold: number; + snapshotFormat: SnapshotFormat; snapshotSerializers: Array; testEnvironment: string; testEnvironmentOptions: Record;