Skip to content

Commit

Permalink
Adds an option for having inline snapshot serializers use the printBa…
Browse files Browse the repository at this point in the history
…sicPrototype option to show literals as literals
  • Loading branch information
orta committed Jul 10, 2021
1 parent 7a64ede commit 752fa81
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 2 deletions.
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,
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
11 changes: 10 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,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;
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';
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

0 comments on commit 752fa81

Please sign in to comment.