Skip to content

Commit 67e4c82

Browse files
maciej-kaSimenB
authored andcommitted
[WIP] feat: option to not escape strings in diff messages (jestjs#5661)
1 parent 5c8ec2f commit 67e4c82

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## master
22

3+
### Features
4+
5+
- `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661))
6+
37
### Fixes
48

59
- `[jest-haste-map]` [**BREAKING**] Replaced internal data structures to improve performance ([#6960](https://github.com/facebook/jest/pull/6960))

packages/pretty-format/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ console.log(prettyFormat(onClick, options));
7878
| :------------------ | :-------- | :--------- | :------------------------------------------------------ |
7979
| `callToJSON` | `boolean` | `true` | call `toJSON` method (if it exists) on objects |
8080
| `escapeRegex` | `boolean` | `false` | escape special characters in regular expressions |
81+
| `escapeString` | `boolean` | `true` | escape special characters in strings |
8182
| `highlight` | `boolean` | `false` | highlight syntax with colors in terminal (some plugins) |
8283
| `indent` | `number` | `2` | spaces in each level of indentation |
8384
| `maxDepth` | `number` | `Infinity` | levels to print in arrays, objects, elements, and so on |
@@ -215,6 +216,7 @@ Write `serialize` to return a string, given the arguments:
215216
| `callToJSON` | `boolean` | call `toJSON` method (if it exists) on objects |
216217
| `colors` | `Object` | escape codes for colors to highlight syntax |
217218
| `escapeRegex` | `boolean` | escape special characters in regular expressions |
219+
| `escapeString` | `boolean` | escape special characters in strings |
218220
| `indent` | `string` | spaces in each level of indentation |
219221
| `maxDepth` | `number` | levels to print in arrays, objects, elements, and so on |
220222
| `min` | `boolean` | minimize added space: no indentation nor line breaks |

packages/pretty-format/src/__tests__/pretty_format.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ describe('prettyFormat()', () => {
317317
expect(prettyFormat(val)).toEqual('"\\"\'\\\\"');
318318
});
319319

320+
it("doesn't escape string with {excapeString: false}", () => {
321+
const val = '"\'\\n';
322+
expect(prettyFormat(val, {escapeString: false})).toEqual('""\'\\n"');
323+
});
324+
320325
it('prints a string with escapes', () => {
321326
expect(prettyFormat('"-"')).toEqual('"\\"-\\""');
322327
expect(prettyFormat('\\ \\\\')).toEqual('"\\\\ \\\\\\\\"');

packages/pretty-format/src/index.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function printBasicValue(
102102
val: any,
103103
printFunctionName: boolean,
104104
escapeRegex: boolean,
105+
escapeString: boolean,
105106
): StringOrNull {
106107
if (val === true || val === false) {
107108
return '' + val;
@@ -119,7 +120,10 @@ function printBasicValue(
119120
return printNumber(val);
120121
}
121122
if (typeOf === 'string') {
122-
return '"' + val.replace(/"|\\/g, '\\$&') + '"';
123+
if (escapeString) {
124+
return '"' + val.replace(/"|\\/g, '\\$&') + '"';
125+
}
126+
return '"' + val + '"';
123127
}
124128
if (typeOf === 'function') {
125129
return printFunction(val, printFunctionName);
@@ -322,6 +326,7 @@ function printer(
322326
val,
323327
config.printFunctionName,
324328
config.escapeRegex,
329+
config.escapeString,
325330
);
326331
if (basicResult !== null) {
327332
return basicResult;
@@ -350,6 +355,7 @@ const DEFAULT_THEME_KEYS = Object.keys(DEFAULT_THEME);
350355
const DEFAULT_OPTIONS: Options = {
351356
callToJSON: true,
352357
escapeRegex: false,
358+
escapeString: true,
353359
highlight: false,
354360
indent: 2,
355361
maxDepth: Infinity,
@@ -424,6 +430,11 @@ const getEscapeRegex = (options?: OptionsReceived) =>
424430
? options.escapeRegex
425431
: DEFAULT_OPTIONS.escapeRegex;
426432

433+
const getEscapeString = (options?: OptionsReceived) =>
434+
options && options.escapeString !== undefined
435+
? options.escapeString
436+
: DEFAULT_OPTIONS.escapeString;
437+
427438
const getConfig = (options?: OptionsReceived): Config => ({
428439
callToJSON:
429440
options && options.callToJSON !== undefined
@@ -434,6 +445,7 @@ const getConfig = (options?: OptionsReceived): Config => ({
434445
? getColorsHighlight(options)
435446
: getColorsEmpty(),
436447
escapeRegex: getEscapeRegex(options),
448+
escapeString: getEscapeString(options),
437449
indent:
438450
options && options.min
439451
? ''
@@ -475,6 +487,7 @@ function prettyFormat(val: any, options?: OptionsReceived): string {
475487
val,
476488
getPrintFunctionName(options),
477489
getEscapeRegex(options),
490+
getEscapeString(options),
478491
);
479492
if (basicResult !== null) {
480493
return basicResult;

types/PrettyFormat.js

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type ThemeReceived = {|
3838
export type Options = {|
3939
callToJSON: boolean,
4040
escapeRegex: boolean,
41+
escapeString: boolean,
4142
highlight: boolean,
4243
indent: number,
4344
maxDepth: number,
@@ -50,6 +51,7 @@ export type Options = {|
5051
export type OptionsReceived = {|
5152
callToJSON?: boolean,
5253
escapeRegex?: boolean,
54+
escapeString?: boolean,
5355
highlight?: boolean,
5456
indent?: number,
5557
maxDepth?: number,
@@ -63,6 +65,7 @@ export type Config = {|
6365
callToJSON: boolean,
6466
colors: Colors,
6567
escapeRegex: boolean,
68+
escapeString: boolean,
6669
indent: string,
6770
maxDepth: number,
6871
min: boolean,

0 commit comments

Comments
 (0)