Skip to content

Commit a22ed65

Browse files
authored
Adds options for configuring the snapshot and inline snapshot serializers (#11654)
1 parent d99534b commit a22ed65

File tree

18 files changed

+140
-15
lines changed

18 files changed

+140
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
- `[jest-cli]` Adds a new config options `snapshotFormat` which offers a way to override any of the formatting settings which come with [pretty-format](https://www.npmjs.com/package/pretty-format#usage-with-options). ([#11654](https://github.com/facebook/jest/pull/11654))
6+
57
### Fixes
68

79
- `[jest-environment-node]` Add `Event` and `EventTarget` to node global environment. ([#11705](https://github.com/facebook/jest/issues/11705))

docs/Configuration.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,41 @@ Default: `5`
945945

946946
The number of seconds after which a test is considered as slow and reported as such in the results.
947947

948+
### `snapshotFormat` \[object]
949+
950+
Default: `undefined`
951+
952+
Allows overriding specific snapshot formatting options documented in the [pretty-format readme](https://www.npmjs.com/package/pretty-format#usage-with-options). For example, this config would have the snapshot formatter not print a prefix for "Object" and "Array":
953+
954+
```json
955+
{
956+
"jest": {
957+
"snapshotFormat": {
958+
"printBasicPrototype": false
959+
}
960+
}
961+
}
962+
```
963+
964+
```ts
965+
import {expect, test} from '@jest/globals';
966+
967+
test('does not show prototypes for object and array inline', () => {
968+
const object = {
969+
array: [{hello: 'Danger'}],
970+
};
971+
expect(object).toMatchInlineSnapshot(`
972+
{
973+
"array": [
974+
{
975+
"hello": "Danger",
976+
},
977+
],
978+
}
979+
`);
980+
});
981+
```
982+
948983
### `snapshotResolver` \[string]
949984

950985
Default: `undefined`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`snapshot serializer uses 8 chars for indent, and shows no prototypes for object and array in a snapshot: no prototypes 1`] = `
4+
{
5+
"array": [
6+
{
7+
"hello": "Danger",
8+
},
9+
],
10+
}
11+
`;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
'use strict';
9+
10+
describe('snapshot serializer', () => {
11+
it('does not show prototypes for object and array inline', () => {
12+
const object = {
13+
array: [{hello: 'Danger'}],
14+
};
15+
expect(object).toMatchInlineSnapshot(`
16+
{
17+
"array": [
18+
{
19+
"hello": "Danger",
20+
},
21+
],
22+
}
23+
`);
24+
});
25+
26+
it('uses 8 chars for indent, and shows no prototypes for object and array in a snapshot', () => {
27+
const object = {
28+
array: [{hello: 'Danger'}],
29+
};
30+
expect(object).toMatchSnapshot('no prototypes');
31+
});
32+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node",
4+
"snapshotFormat": {
5+
"printBasicPrototype": false,
6+
"indent": 8
7+
}
8+
}
9+
}

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export const initialize = async ({
156156
const snapshotState = new SnapshotState(snapshotPath, {
157157
expand,
158158
prettierPath: config.prettierPath,
159+
snapshotFormat: config.snapshotFormat,
159160
updateSnapshot,
160161
});
161162
// @ts-expect-error: snapshotState is a jest extension of `expect`

packages/jest-config/src/ValidConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import type {Config} from '@jest/types';
99
import {replacePathSepForRegex} from 'jest-regex-util';
1010
import {multipleValidOptions} from 'jest-validate';
11+
import {DEFAULT_OPTIONS as PRETTY_FORMAT_DEFAULTS} from 'pretty-format';
1112
import {NODE_MODULES} from './constants';
1213

1314
const NODE_MODULES_REGEXP = replacePathSepForRegex(NODE_MODULES);
@@ -109,6 +110,7 @@ const initialOptions: Config.InitialOptions = {
109110
skipFilter: false,
110111
skipNodeResolution: false,
111112
slowTestThreshold: 5,
113+
snapshotFormat: PRETTY_FORMAT_DEFAULTS,
112114
snapshotResolver: '<rootDir>/snapshotResolver.js',
113115
snapshotSerializers: ['my-serializer-module'],
114116
testEnvironment: 'jest-environment-jsdom',

packages/jest-config/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const groupOptions = (
149149
runTestsByPath: options.runTestsByPath,
150150
silent: options.silent,
151151
skipFilter: options.skipFilter,
152+
snapshotFormat: options.snapshotFormat,
152153
testFailureExitCode: options.testFailureExitCode,
153154
testNamePattern: options.testNamePattern,
154155
testPathPattern: options.testPathPattern,
@@ -204,6 +205,7 @@ const groupOptions = (
204205
skipFilter: options.skipFilter,
205206
skipNodeResolution: options.skipNodeResolution,
206207
slowTestThreshold: options.slowTestThreshold,
208+
snapshotFormat: options.snapshotFormat,
207209
snapshotResolver: options.snapshotResolver,
208210
snapshotSerializers: options.snapshotSerializers,
209211
testEnvironment: options.testEnvironment,

packages/jest-config/src/normalize.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ export default async function normalize(
997997
case 'skipFilter':
998998
case 'skipNodeResolution':
999999
case 'slowTestThreshold':
1000+
case 'snapshotFormat':
10001001
case 'testEnvironment':
10011002
case 'testEnvironmentOptions':
10021003
case 'testFailureExitCode':

packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ exports[`prints the config object 1`] = `
4141
"skipFilter": false,
4242
"skipNodeResolution": false,
4343
"slowTestThreshold": 5,
44+
"snapshotFormat": {},
4445
"snapshotSerializers": [],
4546
"testEnvironment": "node",
4647
"testEnvironmentOptions": {},
@@ -94,6 +95,7 @@ exports[`prints the config object 1`] = `
9495
"runTestsByPath": false,
9596
"silent": false,
9697
"skipFilter": false,
98+
"snapshotFormat": {},
9799
"testFailureExitCode": 1,
98100
"testNamePattern": "",
99101
"testPathPattern": "",

0 commit comments

Comments
 (0)