Skip to content

Commit 4355912

Browse files
ferrannpthymikee
authored andcommitted
Fix contextLines (which needs options merged) + update snapshots. (#8)
1 parent b458de0 commit 4355912

File tree

6 files changed

+238
-211
lines changed

6 files changed

+238
-211
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ snapshotDiff(valueA: any, valueB: any, options?: Options) => string
7878
### Options
7979
* `expand: boolean` (default: `false`) – expand the diff, so the whole information is preserved
8080
* `colors: boolean` (default: `false`) – preserve color information from Jest diff
81+
* `contextLines: number` (default: 5) - number of context lines to be shown at the beginning and at the end of a snapshot
8182

8283
---
8384

__tests__/__snapshots__/snapshotDiff.test.js.snap

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ exports[`can colorize diff 1`] = `
44
"- First value
55
+ Second value
66
7-
 
8-
   some
9-
   some
10-
   some
11-
   some
12-
   some
13-
   some
14-
   some
15-
   not
16-
+  so
17-
   very
18-
   long
19-
   script"
7+
@@ -5,8 +5,9 @@
8+
 some
9+
 some
10+
 some
11+
 some
12+
 not
13+
+ so
14+
 very
15+
 long
16+
 script"
2017
`;
2118

2219
exports[`can expand diff 1`] = `
@@ -42,19 +39,23 @@ exports[`can use contextLines on diff 1`] = `
4239
"- First value
4340
+ Second value
4441
45-
46-
some
47-
some
48-
some
49-
some
50-
some
51-
some
52-
some
53-
not
54-
+ so
55-
very
56-
long
57-
script"
42+
@@ -10,0 +10,1 @@
43+
+ so"
44+
`;
45+
46+
exports[`can use contextLines with React components 1`] = `
47+
"- <Component test=\\"say\\" />
48+
+ <Component test=\\"my name\\" />
49+
50+
@@ -6,1 +6,1 @@
51+
- say
52+
+ my name
53+
@@ -9,1 +9,1 @@
54+
- say
55+
+ my name
56+
@@ -32,1 +32,1 @@
57+
- say
58+
+ my name"
5859
`;
5960

6061
exports[`collapses diffs and strips ansi by default 1`] = `

__tests__/snapshotDiff.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ test('can colorize diff', () => {
129129
});
130130

131131
test('can use contextLines on diff', () => {
132-
expect(snapshotDiff(a, b, {contextLines: 0})).toMatchSnapshot();
132+
expect(snapshotDiff(a, b, { contextLines: 0 })).toMatchSnapshot();
133133
});
134134

135135
test('diffs short strings', () => {
@@ -147,3 +147,11 @@ test('detects React components', () => {
147147
snapshotDiff(<Component test="say" />, <Component test="my name" />)
148148
).toMatchSnapshot();
149149
});
150+
151+
test('can use contextLines with React components', () => {
152+
expect(
153+
snapshotDiff(<Component test="say" />, <Component test="my name" />, {
154+
contextLines: 0,
155+
})
156+
).toMatchSnapshot();
157+
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"react-test-renderer": ">=15"
2020
},
2121
"dependencies": {
22-
"jest-diff": "^20.0.3",
22+
"jest-diff": "test",
2323
"pretty-format": "^20.0.3",
2424
"strip-ansi": "^4.0.0"
2525
},
@@ -31,7 +31,7 @@
3131
"eslint": "^4.3.0",
3232
"eslint-config-callstack-io": "^0.4.0",
3333
"flow-bin": "^0.51.0",
34-
"jest": "^20.0.4",
34+
"jest": "test",
3535
"react": "^15.6.1",
3636
"react-test-renderer": "^15.6.1"
3737
}

src/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@ const defaultOptions = {
2020
contextLines: -1, // Forces to use default from Jest
2121
};
2222

23-
const snapshotDiff = (
24-
valueA: any,
25-
valueB: any,
26-
options?: Options = defaultOptions
27-
): string => {
23+
const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
2824
let difference;
25+
const mergedOptions = Object.assign({}, defaultOptions, options);
2926

3027
if (isReactComponent(valueA) && isReactComponent(valueB)) {
31-
difference = diffReactComponents(valueA, valueB, options);
28+
difference = diffReactComponents(valueA, valueB, mergedOptions);
3229
} else {
33-
difference = diffStrings(valueA, valueB, options);
30+
difference = diffStrings(valueA, valueB, mergedOptions);
3431
}
3532

36-
if (!options.colors) {
33+
if (!mergedOptions.colors) {
3734
const stripAnsi = require('strip-ansi');
3835
return stripAnsi(difference);
3936
}

0 commit comments

Comments
 (0)