Skip to content

Commit

Permalink
Switch to just one option consolidating the inline and file snapshot …
Browse files Browse the repository at this point in the history
…formatting
  • Loading branch information
orta committed Aug 21, 2021
1 parent f63e170 commit 7bf33b5
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 69 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Features

- `[jest-cli]` Adds 2 config options (`inlineSnapshotFormat` and `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))
- `[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))

### Fixes

Expand Down
61 changes: 21 additions & 40 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,43 +537,6 @@ test('some test', () => {

_Note: This option is only supported using the default `jest-circus`. test runner_

### `inlineSnapshotFormat` \[object]

Default: `undefined`

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 inline snapshot formatter not print a prefix for "Object" and "Array":

```json
{
"jest": {
"inlineSnapshotFormat": {
"printBasicPrototype": false
}
}
}
```

```ts
import {expect, test} from '@jest/globals';

test('does not show prototypes for object and array inline', () => {
const object = {
array: [{hello: 'Danger'}],
};
expect(object).toMatchInlineSnapshot(`
{
"array": [
{
"hello": "Danger",
},
],
}
`);
});
```

There is a corresponding [`snapshotFormat`](#snapshotformat-object) option for separate file snapshots.

### `maxConcurrency` \[number]

Default: `5`
Expand Down Expand Up @@ -986,19 +949,37 @@ The number of seconds after which a test is considered as slow and reported as s

Default: `undefined`

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 which prints with 4 spaces instead of 2:
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":

```json
{
"jest": {
"snapshotFormat": {
"indent": 4
"printBasicPrototype": false
}
}
}
```

There is a corresponding [`inlineSnapshotFormat`](#inlinesnapshotformat-object) option for inline snapshots.
```ts
import {expect, test} from '@jest/globals';

test('does not show prototypes for object and array inline', () => {
const object = {
array: [{hello: 'Danger'}],
};
expect(object).toMatchInlineSnapshot(`
{
"array": [
{
"hello": "Danger",
},
],
}
`);
});
```


### `snapshotResolver` \[string]

Expand Down
10 changes: 5 additions & 5 deletions e2e/snapshot-formatting-changes/__tests__/snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ describe('snapshot serializer', () => {
};
expect(object).toMatchInlineSnapshot(`
{
"array": [
{
"hello": "Danger",
},
],
"array": [
{
"hello": "Danger",
},
],
}
`);
});
Expand Down
3 changes: 0 additions & 3 deletions e2e/snapshot-formatting-changes/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"jest": {
"testEnvironment": "node",
"inlineSnapshotFormat": {
"printBasicPrototype": false
},
"snapshotFormat": {
"printBasicPrototype": false,
"indent": 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ export const initialize = async ({
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
inlineSnapshotFormat: config.inlineSnapshotFormat,
prettierPath: config.prettierPath,
snapshotFormat: config.snapshotFormat,
updateSnapshot,
Expand Down
1 change: 0 additions & 1 deletion packages/jest-config/src/ValidConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const initialOptions: Config.InitialOptions = {
throwOnModuleCollision: false,
},
injectGlobals: true,
inlineSnapshotFormat: PRETTY_FORMAT_DEFAULTS,
json: false,
lastCommit: false,
listTests: false,
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ const groupOptions = (
forceExit: options.forceExit,
globalSetup: options.globalSetup,
globalTeardown: options.globalTeardown,
inlineSnapshotFormat: options.inlineSnapshotFormat,
json: options.json,
lastCommit: options.lastCommit,
listTests: options.listTests,
Expand Down Expand Up @@ -186,7 +185,6 @@ const groupOptions = (
globals: options.globals,
haste: options.haste,
injectGlobals: options.injectGlobals,
inlineSnapshotFormat: options.inlineSnapshotFormat,
moduleDirectories: options.moduleDirectories,
moduleFileExtensions: options.moduleFileExtensions,
moduleLoader: options.moduleLoader,
Expand Down
1 change: 0 additions & 1 deletion packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,6 @@ export default async function normalize(
case 'extensionsToTreatAsEsm':
case 'extraGlobals':
case 'globals':
case 'inlineSnapshotFormat':
case 'findRelatedTests':
case 'forceCoverageMatch':
case 'forceExit':
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-jasmine2/src/setup_jest_globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ export default async ({

patchJasmine();
const {expand, updateSnapshot} = globalConfig;
const {prettierPath, inlineSnapshotFormat, snapshotFormat} = config;
const {prettierPath, snapshotFormat} = config;
const snapshotResolver = await buildSnapshotResolver(config, localRequire);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
inlineSnapshotFormat,
prettierPath,
snapshotFormat,
updateSnapshot,
Expand Down
9 changes: 1 addition & 8 deletions packages/jest-snapshot/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type SnapshotStateOptions = {
prettierPath: Config.Path;
expand?: boolean;
snapshotFormat: PrettyFormatOptions;
inlineSnapshotFormat: PrettyFormatOptions;
};

export type SnapshotMatchOptions = {
Expand Down Expand Up @@ -65,7 +64,6 @@ export default class SnapshotState {
private _uncheckedKeys: Set<string>;
private _prettierPath: Config.Path;
private _snapshotFormat: PrettyFormatOptions;
private _inlineSnapshotFormat: PrettyFormatOptions;

added: number;
expand: boolean;
Expand Down Expand Up @@ -95,7 +93,6 @@ export default class SnapshotState {
this.updated = 0;

this._snapshotFormat = options.snapshotFormat;
this._inlineSnapshotFormat = options.inlineSnapshotFormat;
}

markSnapshotsAsCheckedForTest(testName: string): void {
Expand Down Expand Up @@ -209,12 +206,8 @@ export default class SnapshotState {
this._uncheckedKeys.delete(key);
}

const customFormat = isInline
? this._inlineSnapshotFormat
: this._snapshotFormat;

const receivedSerialized = addExtraLineBreaks(
serialize(received, undefined, customFormat),
serialize(received, undefined, this._snapshotFormat),
);
const expected = isInline ? inlineSnapshot : this._snapshotData[key];
const pass = expected === receivedSerialized;
Expand Down
3 changes: 0 additions & 3 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export type InitialOptions = Partial<{
globalTeardown: string | null | undefined;
haste: HasteConfig;
injectGlobals: boolean;
inlineSnapshotFormat: PrettyFormatOptions;
reporters: Array<string | ReporterConfig>;
logHeapUsage: boolean;
lastCommit: boolean;
Expand Down Expand Up @@ -297,7 +296,6 @@ export type GlobalConfig = {
json: boolean;
globalSetup?: string;
globalTeardown?: string;
inlineSnapshotFormat: PrettyFormatOptions;
lastCommit: boolean;
logHeapUsage: boolean;
listTests: boolean;
Expand Down Expand Up @@ -359,7 +357,6 @@ export type ProjectConfig = {
globalTeardown?: string;
globals: ConfigGlobals;
haste: HasteConfig;
inlineSnapshotFormat: PrettyFormatOptions;
injectGlobals: boolean;
moduleDirectories: Array<string>;
moduleFileExtensions: Array<string>;
Expand Down
2 changes: 0 additions & 2 deletions packages/test-utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
forceExit: false,
globalSetup: undefined,
globalTeardown: undefined,
inlineSnapshotFormat: {},
json: false,
lastCommit: false,
listTests: false,
Expand Down Expand Up @@ -86,7 +85,6 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
globals: {},
haste: {},
injectGlobals: true,
inlineSnapshotFormat: {},
moduleDirectories: [],
moduleFileExtensions: ['js'],
moduleLoader: '/test_module_loader_path',
Expand Down

0 comments on commit 7bf33b5

Please sign in to comment.