Skip to content

Commit

Permalink
Add config for printFunctionName
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavovnicius committed Jul 10, 2019
1 parent a501718 commit 044e843
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ export const initialize = ({
});

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

patchJasmine();
const {expand, updateSnapshot} = globalConfig;
const {printFunctionName} = 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,
updateSnapshot,
});
setState({snapshotState, testPath});
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-snapshot/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type SnapshotStateOptions = {
getPrettier: () => null | any;
getBabelTraverse: () => Function;
expand?: boolean;
printFunctionName?: boolean;
};

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

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

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

export const serialize = (data: string): string =>
export const serialize = (data: string, printFunctionName: boolean): string =>
addExtraLineBreaks(
normalizeNewlines(
prettyFormat(data, {
escapeRegex: true,
plugins: getSerializers(),
printFunctionName: false,
printFunctionName,
}),
),
);
Expand Down
1 change: 1 addition & 0 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ export type ProjectConfig = {
modulePaths: Array<string>;
name: string;
prettierPath: string;
printFunctionName: boolean;
resetMocks: boolean;
resetModules: boolean;
resolver: Path | null | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const defaultConfig = {
notifyMode: 'failure-change',
preset: null,
prettierPath: 'prettier',
printFunctionName: false,
resetMocks: false,
resetModules: false,
restoreMocks: false,
Expand Down

0 comments on commit 044e843

Please sign in to comment.