Skip to content

Commit ca9c069

Browse files
Use jest-diff options to turn off colors in diff (#123)
Make use of the new jest-diff options which allow color output to be controlled. Use of example in documentation to turn off all color output. strip-ansi is now unnecessary and has been removed as a dependency. Fixes #113
1 parent e3b4816 commit ca9c069

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
"dependencies": {
2222
"jest-diff": "^25.1.0",
2323
"jest-snapshot": "^25.1.0",
24-
"pretty-format": "^25.1.0",
25-
"strip-ansi": "^6.0.0"
24+
"pretty-format": "^25.1.0"
2625
},
2726
"devDependencies": {
2827
"@babel/cli": "^7.7.0",

src/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
5151
difference = diffStrings(valueA, valueB, mergedOptions);
5252
}
5353

54-
if (!mergedOptions.colors) {
55-
const stripAnsi = require('strip-ansi');
56-
57-
difference = stripAnsi(difference);
58-
}
59-
6054
if (mergedOptions.stablePatchmarks && !mergedOptions.expand) {
6155
difference = difference.replace(
6256
/^@@ -[0-9]+,[0-9]+ \+[0-9]+,[0-9]+ @@$/gm,
@@ -67,12 +61,22 @@ const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
6761
return SNAPSHOT_TITLE + difference;
6862
};
6963

64+
// https://github.com/facebook/jest/tree/d81464622dc8857ba995ed04e121af2b3e8e33bc/packages/jest-diff#example-of-options-for-no-colors
65+
const noDiffColors = {
66+
aColor: identity,
67+
bColor: identity,
68+
changeColor: identity,
69+
commonColor: identity,
70+
patchColor: identity,
71+
};
72+
7073
function diffStrings(valueA: any, valueB: any, options: Options) {
7174
return diff(valueA, valueB, {
7275
expand: options.expand,
7376
contextLines: options.contextLines,
7477
aAnnotation: options.aAnnotation,
7578
bAnnotation: options.bAnnotation,
79+
...(!options.colors ? noDiffColors : {})
7680
});
7781
}
7882

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5833,13 +5833,6 @@ strip-ansi@^5.0.0, strip-ansi@^5.1.0, strip-ansi@^5.2.0:
58335833
dependencies:
58345834
ansi-regex "^4.1.0"
58355835

5836-
strip-ansi@^6.0.0:
5837-
version "6.0.0"
5838-
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.0.tgz#0b1571dd7669ccd4f3e06e14ef1eed26225ae532"
5839-
integrity sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==
5840-
dependencies:
5841-
ansi-regex "^5.0.0"
5842-
58435836
strip-bom@^3.0.0:
58445837
version "3.0.0"
58455838
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"

0 commit comments

Comments
 (0)