File tree Expand file tree Collapse file tree 1 file changed +82
-0
lines changed Expand file tree Collapse file tree 1 file changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ # snapshot-diff
2+ Diffing snapshot utility for Jest. Takes two values, and return their difference as a string, ready to be snapshoted with ` toMatchSnapshot() ` .
3+ Especially helpful when testing the difference between different states of a React component.
4+
5+ ## Installation
6+ ``` bash
7+ yarn add --dev snapshot-diff
8+ ```
9+
10+ ## Usage
11+
12+ ``` js
13+ const snapshotDiff = require (' snapshot-diff' );
14+
15+ test (' snapshot difference between 2 strings' , () => {
16+ expect (snapshotDiff (a, b).toMatchSnapshot ();
17+ });
18+
19+ const React = require (' react' );
20+ const Component = require (' ./Component' );
21+
22+ test (' snapshot difference between 2 React components state' , () => {
23+ expect (
24+ snapshotDiff (
25+ < Component test= " say" / > ,
26+ < Component test= " my name" / >
27+ )
28+ ).toMatchSnapshot ();
29+ });
30+ ` ` `
31+
32+ Produced snapshot:
33+ ` ` ` diff
34+ exports [` diffs short strings 1` ] = `
35+ "- First value
36+ + Second value
37+
38+
39+ - abcx
40+ + abcy
41+ "
42+ ` ;
43+
44+ exports [` detects React components 1` ] = `
45+ "- <Component test=\\ "say\\ " />
46+ + <Component test=\\ "my name\\ " />
47+
48+ @@ -27,11 +27,11 @@
49+ <span />
50+ <span />
51+ <span />
52+ <span />
53+ <span>
54+ - say
55+ + my name
56+ </span>
57+ <span />
58+ <span />
59+ <span />
60+ <span />"
61+ ` ;
62+ ` ` `
63+
64+ ## API
65+
66+ ` ` ` js
67+ type Options = {
68+ expand?: boolean,
69+ colors?: boolean
70+ };
71+
72+ // default export
73+ snapshotDiff (valueA: any, valueB: any, options?: Options) => string
74+ ` ` `
75+
76+ ### Options
77+ * ` expand: boolean` (default: ` false ` ) – expand the diff, so the whole information is preserved
78+ * ` colors: boolean` (default: ` false ` ) – preserve color information from Jest diff
79+
80+ ---
81+
82+ Project is MIT-licensed. Pull Requests welcome!
You can’t perform that action at this time.
0 commit comments