Skip to content

Commit

Permalink
Move printFunctionName to prettyFormat snapshot config
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavovnicius committed Jul 11, 2019
1 parent 044e843 commit 42b30fe
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ export const initialize = ({
});

const {expand, updateSnapshot} = globalConfig;
const {printFunctionName} = config;
const {prettyFormatSnapshotConfig} = config;
const snapshotResolver = buildSnapshotResolver(config);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
getBabelTraverse,
getPrettier,
printFunctionName,
prettyFormatSnapshotConfig,
updateSnapshot,
});
setState({snapshotState, testPath});
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const groupOptions = (
modulePaths: options.modulePaths,
name: options.name,
prettierPath: options.prettierPath,
printFunctionName: options.printFunctionName,
prettyFormatSnapshotConfig: options.prettyFormatSnapshotConfig,
resetMocks: options.resetMocks,
resetModules: options.resetModules,
resolver: options.resolver,
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/src/setup_jest_globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ export default ({

patchJasmine();
const {expand, updateSnapshot} = globalConfig;
const {printFunctionName} = config;
const {prettyFormatSnapshotConfig} = config;
const snapshotResolver = buildSnapshotResolver(config);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
getBabelTraverse: () => require('@babel/traverse').default,
getPrettier: () =>
config.prettierPath ? require(config.prettierPath) : null,
printFunctionName,
prettyFormatSnapshotConfig,
updateSnapshot,
});
setState({snapshotState, testPath});
Expand Down
11 changes: 7 additions & 4 deletions packages/jest-snapshot/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type SnapshotStateOptions = {
getPrettier: () => null | any;
getBabelTraverse: () => Function;
expand?: boolean;
printFunctionName?: boolean;
prettyFormatSnapshotConfig?: Config.PrettyFormatSnapshotConfig;
};

export type SnapshotMatchOptions = {
Expand All @@ -49,7 +49,7 @@ export default class SnapshotState {
private _uncheckedKeys: Set<string>;
private _getBabelTraverse: () => Function;
private _getPrettier: () => null | any;
private _printFunctionName: boolean;
private _prettyFormatSnapshotConfig: Config.PrettyFormatSnapshotConfig;

added: number;
expand: boolean;
Expand All @@ -75,7 +75,10 @@ export default class SnapshotState {
this.expand = options.expand || false;
this.added = 0;
this.matched = 0;
this._printFunctionName = options.printFunctionName || false;
this._prettyFormatSnapshotConfig = options.prettyFormatSnapshotConfig || {
escapeRegex: true,
printFunctionName: false,
};
this.unmatched = 0;
this._updateSnapshot = options.updateSnapshot;
this.updated = 0;
Expand Down Expand Up @@ -192,7 +195,7 @@ export default class SnapshotState {
this._uncheckedKeys.delete(key);
}

const receivedSerialized = serialize(received, this._printFunctionName);
const receivedSerialized = serialize(received, this._prettyFormatSnapshotConfig);
const expected = isInline ? inlineSnapshot : this._snapshotData[key];
const pass = expected === receivedSerialized;
const hasSnapshot = isInline
Expand Down
9 changes: 6 additions & 3 deletions packages/jest-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,16 @@ export const getSnapshotData = (
const addExtraLineBreaks = (string: string): string =>
string.includes('\n') ? `\n${string}\n` : string;

export const serialize = (data: string, printFunctionName: boolean): string =>
export const serialize = (
data: string,
prettyFormatSnapshotConfig: Config.PrettyFormatSnapshotConfig,
): string =>
addExtraLineBreaks(
normalizeNewlines(
prettyFormat(data, {
escapeRegex: true,
plugins: getSerializers(),
printFunctionName,
escapeRegex: prettyFormatSnapshotConfig.escapeRegex,
printFunctionName: prettyFormatSnapshotConfig.printFunctionName,
}),
),
);
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type HasteConfig = {
export type ReporterConfig = [string, Record<string, unknown>];
export type TransformerConfig = [string, Record<string, unknown>];

export type PrettyFormatSnapshotConfig = {
printFunctionName?: boolean;
escapeRegex?: boolean;
}

export type ConfigGlobals = Record<string, any>;

export type DefaultOptions = {
Expand Down Expand Up @@ -392,7 +397,7 @@ export type ProjectConfig = {
modulePaths: Array<string>;
name: string;
prettierPath: string;
printFunctionName: boolean;
prettyFormatSnapshotConfig: PrettyFormatSnapshotConfig;
resetMocks: boolean;
resetModules: boolean;
resolver: Path | null | undefined;
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-validate/src/__tests__/fixtures/jestConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const defaultConfig = {
notifyMode: 'failure-change',
preset: null,
prettierPath: 'prettier',
printFunctionName: false,
prettyFormatSnapshotConfig: {
escapeRegex: true,
printFunctionName: false,
},
resetMocks: false,
resetModules: false,
restoreMocks: false,
Expand Down

0 comments on commit 42b30fe

Please sign in to comment.