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

Add config for printFunctionName #8668

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -122,12 +122,14 @@ export const initialize = ({
});

const {expand, updateSnapshot} = globalConfig;
const {prettyFormatSnapshotConfig} = config;
const snapshotResolver = buildSnapshotResolver(config);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
getBabelTraverse,
getPrettier,
prettyFormatSnapshotConfig,
updateSnapshot,
});
setState({snapshotState, testPath});
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.ts
Expand Up @@ -186,6 +186,7 @@ const groupOptions = (
modulePaths: options.modulePaths,
name: options.name,
prettierPath: options.prettierPath,
prettyFormatSnapshotConfig: options.prettyFormatSnapshotConfig,
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
Expand Up @@ -101,13 +101,15 @@ export default ({

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

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 _prettyFormatSnapshotConfig?: Config.PrettyFormatSnapshotConfig;

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._prettyFormatSnapshotConfig = options.prettyFormatSnapshotConfig;
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._prettyFormatSnapshotConfig);
const expected = isInline ? inlineSnapshot : this._snapshotData[key];
const pass = expected === receivedSerialized;
const hasSnapshot = isInline
Expand Down
23 changes: 19 additions & 4 deletions packages/jest-snapshot/src/utils.ts
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): 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: false,
printFunctionName,
}),
),
);
}

// unescape double quotes
export const unescape = (data: string): string => data.replace(/\\(")/g, '$1');
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-types/src/Config.ts
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,6 +397,7 @@ export type ProjectConfig = {
modulePaths: Array<string>;
name: string;
prettierPath: string;
prettyFormatSnapshotConfig: PrettyFormatSnapshotConfig;
resetMocks: boolean;
resetModules: boolean;
resolver: Path | null | undefined;
Expand Down
10 changes: 9 additions & 1 deletion packages/jest-validate/src/__tests__/fixtures/jestConfig.js
Expand Up @@ -42,6 +42,10 @@ const defaultConfig = {
notifyMode: 'failure-change',
preset: null,
prettierPath: 'prettier',
prettyFormatSnapshotConfig: {
escapeRegex: true,
printFunctionName: false,
},
resetMocks: false,
resetModules: false,
restoreMocks: false,
Expand Down Expand Up @@ -101,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 @@ -131,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