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 d5341fb
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 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
8 changes: 4 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,7 @@ export default class SnapshotState {
this.expand = options.expand || false;
this.added = 0;
this.matched = 0;
this._printFunctionName = options.printFunctionName || false;
this._prettyFormatSnapshotConfig = options.prettyFormatSnapshotConfig;
this.unmatched = 0;
this._updateSnapshot = options.updateSnapshot;
this.updated = 0;
Expand Down Expand Up @@ -192,7 +192,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
21 changes: 18 additions & 3 deletions packages/jest-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,31 @@ export const getSnapshotData = (
const addExtraLineBreaks = (string: string): string =>
string.includes('\n') ? `\n${string}\n` : string;

export const serialize = (data: string, printFunctionName: boolean): string =>
addExtraLineBreaks(
export const serialize = (
data: string,
config?: Config.PrettyFormatSnapshotConfig,
): string => {
let escapeRegex = true;
let printFunctionName = false;

if (config && config.escapeRegex) {
escapeRegex = config.escapeRegex;
}

if (config && config.printFunctionName) {
printFunctionName = config.printFunctionName;
}

return addExtraLineBreaks(
normalizeNewlines(
prettyFormat(data, {
escapeRegex: true,
escapeRegex,
plugins: getSerializers(),
printFunctionName,
}),
),
);
}

// unescape double quotes
export const unescape = (data: string): string => data.replace(/\\(")/g, '$1');
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 = {
escapeRegex?: boolean;
printFunctionName?: 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
11 changes: 9 additions & 2 deletions 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 Expand Up @@ -102,6 +105,10 @@ const validConfig = {
notifyMode: 'failure-change',
preset: 'react-native',
prettierPath: '<rootDir>/node_modules/prettier',
prettyFormatSnapshotConfig: {
escapeRegex: true,
printFunctionName: false,
},
resetMocks: false,
resetModules: false,
restoreMocks: false,
Expand Down Expand Up @@ -132,7 +139,7 @@ const validConfig = {
watchman: true,
};

const format = (value: string) => require('pretty-format')(value, {min: true});
const format = (value: string) => require('pretty-format')(value, { min: true });

const deprecatedConfig = {
preprocessorIgnorePatterns: (config: Object) =>
Expand Down

0 comments on commit d5341fb

Please sign in to comment.