Skip to content

Commit f4c65f7

Browse files
avocadowastakenthymikee
authored andcommitted
Implement toMatchDiffSnapshot jest matcher. (#11)
1 parent 5f2e122 commit f4c65f7

File tree

8 files changed

+249
-1
lines changed

8 files changed

+249
-1
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ yarn add --dev snapshot-diff
1111

1212
## Usage
1313

14+
##### With default jest matcher
15+
1416
```js
1517
const snapshotDiff = require('snapshot-diff');
1618

17-
test('snapshot difference between 2 strings', () => {
19+
test('snapshot difference between 2 strings', () => {
1820
expect(snapshotDiff(a, b)).toMatchSnapshot();
1921
});
2022

@@ -31,7 +33,33 @@ test('snapshot difference between 2 React components state', () => {
3133
});
3234
```
3335

36+
##### With custom matcher
37+
38+
```js
39+
const { toMatchDiffSnapshot } = require('snapshot-diff');
40+
41+
expect.extend({ toMatchDiffSnapshot });
42+
43+
test('snapshot difference between 2 strings', () => {
44+
expect(a).toMatchDiffSnapshot(b);
45+
});
46+
47+
const React = require('react');
48+
const Component = require('./Component');
49+
50+
test('snapshot difference between 2 React components state', () => {
51+
expect(
52+
<Component test="say" />
53+
).toMatchDiffSnapshot(
54+
<Component test="my name" />
55+
);
56+
});
57+
```
58+
59+
60+
3461
Produced snapshot:
62+
3563
```diff
3664
exports[`snapshot difference between 2 strings 1`] = `
3765
"- First value
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`proxies "colors" option(s) 1`] = `
4+
"- First value
5+
+ Second value
6+
7+
@@ -5,8 +5,9 @@
8+
 some
9+
 some
10+
 some
11+
 some
12+
 not
13+
+ so
14+
 very
15+
 long
16+
 script"
17+
`;
18+
19+
exports[`proxies "contextLines" option(s) 1`] = `
20+
"- First value
21+
+ Second value
22+
23+
@@ -10,0 +10,1 @@
24+
+ so"
25+
`;
26+
27+
exports[`proxies "expand" option(s) 1`] = `
28+
"- First value
29+
+ Second value
30+
31+
32+
some
33+
some
34+
some
35+
some
36+
some
37+
some
38+
some
39+
not
40+
+ so
41+
very
42+
long
43+
script"
44+
`;
45+
46+
exports[`works with default options 1`] = `
47+
"- First value
48+
+ Second value
49+
50+
@@ -5,8 +5,9 @@
51+
some
52+
some
53+
some
54+
some
55+
not
56+
+ so
57+
very
58+
long
59+
script"
60+
`;

__tests__/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
test('public api', () => {
2+
const index = require('../src/index');
3+
4+
expect(index).toBeInstanceOf(Function);
5+
expect(index.snapshotDiff).toBe(index);
6+
expect(index.toMatchDiffSnapshot).toBeInstanceOf(Function);
7+
8+
const { snapshotDiff, toMatchDiffSnapshot } = require('../src/index');
9+
10+
expect(snapshotDiff).toBe(index);
11+
expect(toMatchDiffSnapshot).toBe(index.toMatchDiffSnapshot);
12+
});

__tests__/toMatchDiffSnapshot.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// @flow
2+
3+
const snapshotDiff = require('../src/index');
4+
5+
const a = `
6+
some
7+
some
8+
some
9+
some
10+
some
11+
some
12+
some
13+
not
14+
very
15+
long
16+
script
17+
`;
18+
const b = `
19+
some
20+
some
21+
some
22+
some
23+
some
24+
some
25+
some
26+
not
27+
so
28+
very
29+
long
30+
script
31+
`;
32+
33+
beforeAll(() => {
34+
expect.extend({ toMatchDiffSnapshot: snapshotDiff.toMatchDiffSnapshot });
35+
});
36+
37+
test('works with default options', () => {
38+
// $FlowFixMe
39+
expect(a).toMatchDiffSnapshot(b);
40+
});
41+
42+
[
43+
{ expand: true },
44+
{ colors: true },
45+
{ contextLines: 0 },
46+
].forEach((options: any) => {
47+
test(`proxies "${Object.keys(options).join(', ')}" option(s)`, () => {
48+
// $FlowFixMe
49+
expect(a).toMatchDiffSnapshot(b, options);
50+
});
51+
});

index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference types="jest"/>
2+
3+
type DiffOptions = {
4+
expand?: boolean,
5+
colors?: boolean,
6+
contextLines?: number,
7+
};
8+
9+
declare namespace jest {
10+
interface Matchers {
11+
toMatchDiffSnapshot(valueB: any, options?: DiffOptions): boolean
12+
}
13+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.5",
44
"description": "Diffing Jest snapshots utility",
55
"main": "build/index.js",
6+
"typings": "index.d.ts",
67
"license": "MIT",
78
"author": "Michał Pierzchała <[email protected]>",
89
"scripts": {
@@ -20,6 +21,7 @@
2021
},
2122
"dependencies": {
2223
"jest-diff": "test",
24+
"jest-snapshot": "test",
2325
"pretty-format": "^20.0.3",
2426
"strip-ansi": "^4.0.0"
2527
},

src/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict';
44

55
const diff = require('jest-diff');
6+
const snapshot = require('jest-snapshot');
67
const prettyFormat = require('pretty-format');
78

89
const { ReactElement } = prettyFormat.plugins;
@@ -64,4 +65,12 @@ function diffReactComponents(valueA: any, valueB: any, options: Options) {
6465
});
6566
}
6667

68+
function toMatchDiffSnapshot(valueA: any, valueB: any, options?: Options) {
69+
const difference = snapshotDiff(valueA, valueB, options);
70+
71+
return snapshot.toMatchSnapshot.call(this, difference);
72+
}
73+
6774
module.exports = snapshotDiff;
75+
module.exports.snapshotDiff = snapshotDiff;
76+
module.exports.toMatchDiffSnapshot = toMatchDiffSnapshot;

yarn.lock

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,16 @@ [email protected], jest-diff@test:
21242124
jest-matcher-utils "20.1.0-delta.5"
21252125
pretty-format "20.1.0-delta.5"
21262126

2127+
2128+
version "20.1.0-echo.1"
2129+
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-20.1.0-echo.1.tgz#7306889a65d58a86e5b6e3027f985c446d46c511"
2130+
dependencies:
2131+
chalk "^2.0.1"
2132+
diff "^3.2.0"
2133+
jest-get-type "20.1.0-echo.1"
2134+
jest-matcher-utils "20.1.0-echo.1"
2135+
pretty-format "20.1.0-echo.1"
2136+
21272137
21282138
version "20.1.0-delta.5"
21292139
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-20.1.0-delta.5.tgz#a3699e999a4be26beee576a41988bd9c782cb469"
@@ -2151,6 +2161,10 @@ [email protected]:
21512161
version "20.1.0-delta.5"
21522162
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-20.1.0-delta.5.tgz#3a6a0c38e2eb4da3082a4a0175ae0c4a18865c91"
21532163

2164+
2165+
version "20.1.0-echo.1"
2166+
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-20.1.0-echo.1.tgz#00e3a07d5feffc76236488aa4445bd18e66e71ac"
2167+
21542168
21552169
version "20.1.0-delta.5"
21562170
resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-20.1.0-delta.5.tgz#bc2cccc0970541d752af18688ca50235f95a1e06"
@@ -2182,6 +2196,14 @@ [email protected]:
21822196
jest-get-type "20.1.0-delta.5"
21832197
pretty-format "20.1.0-delta.5"
21842198

2199+
2200+
version "20.1.0-echo.1"
2201+
resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-20.1.0-echo.1.tgz#2bbb22b2e6269a87e73c152003dc91abb8ef0c85"
2202+
dependencies:
2203+
chalk "^2.0.1"
2204+
jest-get-type "20.1.0-echo.1"
2205+
pretty-format "20.1.0-echo.1"
2206+
21852207
21862208
version "20.1.0-delta.5"
21872209
resolved "https://registry.yarnpkg.com/jest-matchers/-/jest-matchers-20.1.0-delta.5.tgz#31f6f556b94709083012eb0b85bb384126b33651"
@@ -2200,10 +2222,22 @@ [email protected]:
22002222
micromatch "^2.3.11"
22012223
slash "^1.0.0"
22022224

2225+
2226+
version "20.1.0-echo.1"
2227+
resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-20.1.0-echo.1.tgz#31b6cced3e8cec6e1a74e7bcc989b987ea2b0ed5"
2228+
dependencies:
2229+
chalk "^2.0.1"
2230+
micromatch "^2.3.11"
2231+
slash "^1.0.0"
2232+
22032233
22042234
version "20.1.0-delta.5"
22052235
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-20.1.0-delta.5.tgz#220bc3bd2272dc57444f20602c65f15b769d7241"
22062236

2237+
2238+
version "20.1.0-echo.1"
2239+
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-20.1.0-echo.1.tgz#c37e47c954aa5090e887d50ba96f3b1d51d3bf90"
2240+
22072241
22082242
version "20.1.0-delta.5"
22092243
resolved "https://registry.yarnpkg.com/jest-regex-util/-/jest-regex-util-20.1.0-delta.5.tgz#ad30d52b16e505c793a16ca73aa37e4852b24858"
@@ -2255,6 +2289,17 @@ [email protected]:
22552289
natural-compare "^1.4.0"
22562290
pretty-format "20.1.0-delta.5"
22572291

2292+
jest-snapshot@test:
2293+
version "20.1.0-echo.1"
2294+
resolved "https://registry.yarnpkg.com/jest-snapshot/-/jest-snapshot-20.1.0-echo.1.tgz#580c8064b6516a5a29ef6dfa14432c5071562155"
2295+
dependencies:
2296+
chalk "^2.0.1"
2297+
jest-diff "20.1.0-echo.1"
2298+
jest-matcher-utils "20.1.0-echo.1"
2299+
jest-util "20.1.0-echo.1"
2300+
natural-compare "^1.4.0"
2301+
pretty-format "20.1.0-echo.1"
2302+
22582303
22592304
version "20.1.0-delta.5"
22602305
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-20.1.0-delta.5.tgz#45f1247608b98cc0a0fe1ae643e747f2dd60e40f"
@@ -2267,6 +2312,18 @@ [email protected]:
22672312
leven "^2.1.0"
22682313
mkdirp "^0.5.1"
22692314

2315+
2316+
version "20.1.0-echo.1"
2317+
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-20.1.0-echo.1.tgz#adec8947191e31e2be5e243bffd28742e94f9401"
2318+
dependencies:
2319+
chalk "^2.0.1"
2320+
graceful-fs "^4.1.11"
2321+
jest-message-util "20.1.0-echo.1"
2322+
jest-mock "20.1.0-echo.1"
2323+
jest-validate "20.1.0-echo.1"
2324+
leven "^2.1.0"
2325+
mkdirp "^0.5.1"
2326+
22702327
22712328
version "20.1.0-delta.5"
22722329
resolved "https://registry.yarnpkg.com/jest-validate/-/jest-validate-20.1.0-delta.5.tgz#f5d1dffc5cf7acbfe353f0062e2a9548ef7f1388"
@@ -2276,6 +2333,15 @@ [email protected]:
22762333
leven "^2.1.0"
22772334
pretty-format "20.1.0-delta.5"
22782335

2336+
2337+
version "20.1.0-echo.1"
2338+
resolved "https://registry.yarnpkg.com/jest-validate/-/jest-validate-20.1.0-echo.1.tgz#eb1d795a9a67a3e2e1e838b16d7b8e298d597d51"
2339+
dependencies:
2340+
chalk "^2.0.1"
2341+
jest-get-type "20.1.0-echo.1"
2342+
leven "^2.1.0"
2343+
pretty-format "20.1.0-echo.1"
2344+
22792345
jest@test:
22802346
version "20.1.0-delta.5"
22812347
resolved "https://registry.yarnpkg.com/jest/-/jest-20.1.0-delta.5.tgz#7bca864f549fb4b40049a094ef2a0accb3097e92"
@@ -2808,6 +2874,13 @@ [email protected]:
28082874
ansi-regex "^3.0.0"
28092875
ansi-styles "^3.0.0"
28102876

2877+
2878+
version "20.1.0-echo.1"
2879+
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-20.1.0-echo.1.tgz#3b97907461d90a06b2e7531185cb1b529eb186f3"
2880+
dependencies:
2881+
ansi-regex "^3.0.0"
2882+
ansi-styles "^3.0.0"
2883+
28112884
pretty-format@^20.0.3:
28122885
version "20.0.3"
28132886
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-20.0.3.tgz#020e350a560a1fe1a98dc3beb6ccffb386de8b14"

0 commit comments

Comments
 (0)