Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds options for configuring the snapshot and inline snapshot serializers #11654

Merged
merged 16 commits into from Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

### Features

- `[jest-cli]` Adds an option (`inlineSnapshotFormatter` with `simple`) for having inline snapshot serializers use the printBasicPrototype option to show literals as literals. ([#11654](https://github.com/facebook/jest/pull/11654))


### Fixes

### Chore & Maintenance
Expand Down
25 changes: 25 additions & 0 deletions 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",
},
],
}
`);
});
});
6 changes: 6 additions & 0 deletions e2e/snapshot-inline-basic-formatting/package.json
@@ -0,0 +1,6 @@
{
"jest": {
"testEnvironment": "node",
"inlineSnapshotFormatter": "simple"
}
}
Expand Up @@ -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,
});
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-config/src/ValidConfig.ts
Expand Up @@ -66,6 +66,10 @@ const initialOptions: Config.InitialOptions = {
throwOnModuleCollision: false,
},
injectGlobals: true,
inlineSnapshotFormatter: multipleValidOptions(
'exposed prototypes',
'simple',
) as any,
orta marked this conversation as resolved.
Show resolved Hide resolved
json: false,
lastCommit: false,
listTests: false,
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion packages/jest-snapshot/src/State.ts
Expand Up @@ -25,6 +25,7 @@ export type SnapshotStateOptions = {
updateSnapshot: Config.SnapshotUpdateState;
prettierPath: Config.Path;
expand?: boolean;
preferSimpleForInline?: boolean;
};

export type SnapshotMatchOptions = {
Expand Down Expand Up @@ -61,6 +62,7 @@ export default class SnapshotState {
private _inlineSnapshots: Array<InlineSnapshot>;
private _uncheckedKeys: Set<string>;
private _prettierPath: Config.Path;
private _preferSimpleInlineSnapshots: boolean;

added: number;
expand: boolean;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -201,7 +204,14 @@ export default class SnapshotState {
this._uncheckedKeys.delete(key);
}

const receivedSerialized = addExtraLineBreaks(serialize(received));
// There is an option to allow for not showing 'Object' and 'Array' on
// snapshots, which we'd like to allow for opting into with inline snapshots.
const hasOptedInToSimpleInline =
isInline && this._preferSimpleInlineSnapshots;

const receivedSerialized = addExtraLineBreaks(
serialize(received, undefined, hasOptedInToSimpleInline),
);
const expected = isInline ? inlineSnapshot : this._snapshotData[key];
const pass = expected === receivedSerialized;
const hasSnapshot = expected !== undefined;
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-snapshot/src/utils.ts
Expand Up @@ -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,
}),
);
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-types/src/Config.ts
Expand Up @@ -171,6 +171,7 @@ export type InitialOptions = Partial<{
globalTeardown: string | null | undefined;
haste: HasteConfig;
injectGlobals: boolean;
inlineSnapshotFormatter: 'exposed prototypes' | 'simple';
orta marked this conversation as resolved.
Show resolved Hide resolved
reporters: Array<string | ReporterConfig>;
logHeapUsage: boolean;
lastCommit: boolean;
Expand Down Expand Up @@ -292,6 +293,7 @@ export type GlobalConfig = {
json: boolean;
globalSetup?: string;
globalTeardown?: string;
inlineSnapshotFormatter: 'exposed prototypes' | 'simple';
lastCommit: boolean;
logHeapUsage: boolean;
listTests: boolean;
Expand Down Expand Up @@ -352,6 +354,7 @@ export type ProjectConfig = {
globalTeardown?: string;
globals: ConfigGlobals;
haste: HasteConfig;
inlineSnapshotFormatter: 'exposed prototypes' | 'simple';
injectGlobals: boolean;
moduleDirectories: Array<string>;
moduleFileExtensions: Array<string>;
Expand Down
2 changes: 2 additions & 0 deletions packages/test-utils/src/config.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down