Skip to content

Implement toMatchDiffSnapshot jest matcher. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ yarn add --dev snapshot-diff

## Usage

##### With default jest matcher

```js
const snapshotDiff = require('snapshot-diff');

test('snapshot difference between 2 strings', () => {
test('snapshot difference between 2 strings', () => {
expect(snapshotDiff(a, b)).toMatchSnapshot();
});

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

##### With custom matcher

```js
const { toMatchDiffSnapshot } = require('snapshot-diff');

expect.extend({ toMatchDiffSnapshot });

test('snapshot difference between 2 strings', () => {
expect(a).toMatchDiffSnapshot(b);
});

const React = require('react');
const Component = require('./Component');

test('snapshot difference between 2 React components state', () => {
expect(
<Component test="say" />
).toMatchDiffSnapshot(
<Component test="my name" />
);
});
```



Produced snapshot:

```diff
exports[`snapshot difference between 2 strings 1`] = `
"- First value
Expand Down
60 changes: 60 additions & 0 deletions __tests__/__snapshots__/toMatchDiffSnapshot.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`proxies "colors" option(s) 1`] = `
"- First value
+ Second value

@@ -5,8 +5,9 @@
 some
 some
 some
 some
 not
+ so
 very
 long
 script"
`;

exports[`proxies "contextLines" option(s) 1`] = `
"- First value
+ Second value

@@ -10,0 +10,1 @@
+ so"
`;

exports[`proxies "expand" option(s) 1`] = `
"- First value
+ Second value


some
some
some
some
some
some
some
not
+ so
very
long
script"
`;

exports[`works with default options 1`] = `
"- First value
+ Second value

@@ -5,8 +5,9 @@
some
some
some
some
not
+ so
very
long
script"
`;
12 changes: 12 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test('public api', () => {
const index = require('../src/index');

expect(index).toBeInstanceOf(Function);
expect(index.snapshotDiff).toBe(index);
expect(index.toMatchDiffSnapshot).toBeInstanceOf(Function);

const { snapshotDiff, toMatchDiffSnapshot } = require('../src/index');

expect(snapshotDiff).toBe(index);
expect(toMatchDiffSnapshot).toBe(index.toMatchDiffSnapshot);
});
51 changes: 51 additions & 0 deletions __tests__/toMatchDiffSnapshot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// @flow

const snapshotDiff = require('../src/index');

const a = `
some
some
some
some
some
some
some
not
very
long
script
`;
const b = `
some
some
some
some
some
some
some
not
so
very
long
script
`;

beforeAll(() => {
expect.extend({ toMatchDiffSnapshot: snapshotDiff.toMatchDiffSnapshot });
});

test('works with default options', () => {
// $FlowFixMe
expect(a).toMatchDiffSnapshot(b);
});

[
{ expand: true },
{ colors: true },
{ contextLines: 0 },
].forEach((options: any) => {
test(`proxies "${Object.keys(options).join(', ')}" option(s)`, () => {
// $FlowFixMe
expect(a).toMatchDiffSnapshot(b, options);
});
});
13 changes: 13 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference types="jest"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this valid in TS land?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. I took jest-immutable-matchers/index.d.ts as an example cause it was working without any configuration with my IDE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still in docs


type DiffOptions = {
expand?: boolean,
colors?: boolean,
contextLines?: number,
};

declare namespace jest {
interface Matchers {
toMatchDiffSnapshot(valueB: any, options?: DiffOptions): boolean
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.5",
"description": "Diffing Jest snapshots utility",
"main": "build/index.js",
"typings": "index.d.ts",
"license": "MIT",
"author": "Michał Pierzchała <[email protected]>",
"scripts": {
Expand All @@ -20,6 +21,7 @@
},
"dependencies": {
"jest-diff": "test",
"jest-snapshot": "test",
"pretty-format": "^20.0.3",
"strip-ansi": "^4.0.0"
},
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';

const diff = require('jest-diff');
const snapshot = require('jest-snapshot');
const prettyFormat = require('pretty-format');

const { ReactElement } = prettyFormat.plugins;
Expand Down Expand Up @@ -64,4 +65,12 @@ function diffReactComponents(valueA: any, valueB: any, options: Options) {
});
}

function toMatchDiffSnapshot(valueA: any, valueB: any, options?: Options) {
const difference = snapshotDiff(valueA, valueB, options);

return snapshot.toMatchSnapshot.call(this, difference);
}

module.exports = snapshotDiff;
module.exports.snapshotDiff = snapshotDiff;
module.exports.toMatchDiffSnapshot = toMatchDiffSnapshot;
73 changes: 73 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,16 @@ [email protected], jest-diff@test:
jest-matcher-utils "20.1.0-delta.5"
pretty-format "20.1.0-delta.5"

[email protected]:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-20.1.0-echo.1.tgz#7306889a65d58a86e5b6e3027f985c446d46c511"
dependencies:
chalk "^2.0.1"
diff "^3.2.0"
jest-get-type "20.1.0-echo.1"
jest-matcher-utils "20.1.0-echo.1"
pretty-format "20.1.0-echo.1"

[email protected]:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-20.1.0-delta.5.tgz#a3699e999a4be26beee576a41988bd9c782cb469"
Expand Down Expand Up @@ -2151,6 +2161,10 @@ [email protected]:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-20.1.0-delta.5.tgz#3a6a0c38e2eb4da3082a4a0175ae0c4a18865c91"

[email protected]:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-20.1.0-echo.1.tgz#00e3a07d5feffc76236488aa4445bd18e66e71ac"

[email protected]:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-20.1.0-delta.5.tgz#bc2cccc0970541d752af18688ca50235f95a1e06"
Expand Down Expand Up @@ -2182,6 +2196,14 @@ [email protected]:
jest-get-type "20.1.0-delta.5"
pretty-format "20.1.0-delta.5"

[email protected]:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-20.1.0-echo.1.tgz#2bbb22b2e6269a87e73c152003dc91abb8ef0c85"
dependencies:
chalk "^2.0.1"
jest-get-type "20.1.0-echo.1"
pretty-format "20.1.0-echo.1"

[email protected]:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest-matchers/-/jest-matchers-20.1.0-delta.5.tgz#31f6f556b94709083012eb0b85bb384126b33651"
Expand All @@ -2200,10 +2222,22 @@ [email protected]:
micromatch "^2.3.11"
slash "^1.0.0"

[email protected]:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-20.1.0-echo.1.tgz#31b6cced3e8cec6e1a74e7bcc989b987ea2b0ed5"
dependencies:
chalk "^2.0.1"
micromatch "^2.3.11"
slash "^1.0.0"

[email protected]:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-20.1.0-delta.5.tgz#220bc3bd2272dc57444f20602c65f15b769d7241"

[email protected]:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-20.1.0-echo.1.tgz#c37e47c954aa5090e887d50ba96f3b1d51d3bf90"

[email protected]:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest-regex-util/-/jest-regex-util-20.1.0-delta.5.tgz#ad30d52b16e505c793a16ca73aa37e4852b24858"
Expand Down Expand Up @@ -2255,6 +2289,17 @@ [email protected]:
natural-compare "^1.4.0"
pretty-format "20.1.0-delta.5"

jest-snapshot@test:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/jest-snapshot/-/jest-snapshot-20.1.0-echo.1.tgz#580c8064b6516a5a29ef6dfa14432c5071562155"
dependencies:
chalk "^2.0.1"
jest-diff "20.1.0-echo.1"
jest-matcher-utils "20.1.0-echo.1"
jest-util "20.1.0-echo.1"
natural-compare "^1.4.0"
pretty-format "20.1.0-echo.1"

[email protected]:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-20.1.0-delta.5.tgz#45f1247608b98cc0a0fe1ae643e747f2dd60e40f"
Expand All @@ -2267,6 +2312,18 @@ [email protected]:
leven "^2.1.0"
mkdirp "^0.5.1"

[email protected]:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-20.1.0-echo.1.tgz#adec8947191e31e2be5e243bffd28742e94f9401"
dependencies:
chalk "^2.0.1"
graceful-fs "^4.1.11"
jest-message-util "20.1.0-echo.1"
jest-mock "20.1.0-echo.1"
jest-validate "20.1.0-echo.1"
leven "^2.1.0"
mkdirp "^0.5.1"

[email protected]:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest-validate/-/jest-validate-20.1.0-delta.5.tgz#f5d1dffc5cf7acbfe353f0062e2a9548ef7f1388"
Expand All @@ -2276,6 +2333,15 @@ [email protected]:
leven "^2.1.0"
pretty-format "20.1.0-delta.5"

[email protected]:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/jest-validate/-/jest-validate-20.1.0-echo.1.tgz#eb1d795a9a67a3e2e1e838b16d7b8e298d597d51"
dependencies:
chalk "^2.0.1"
jest-get-type "20.1.0-echo.1"
leven "^2.1.0"
pretty-format "20.1.0-echo.1"

jest@test:
version "20.1.0-delta.5"
resolved "https://registry.yarnpkg.com/jest/-/jest-20.1.0-delta.5.tgz#7bca864f549fb4b40049a094ef2a0accb3097e92"
Expand Down Expand Up @@ -2808,6 +2874,13 @@ [email protected]:
ansi-regex "^3.0.0"
ansi-styles "^3.0.0"

[email protected]:
version "20.1.0-echo.1"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-20.1.0-echo.1.tgz#3b97907461d90a06b2e7531185cb1b529eb186f3"
dependencies:
ansi-regex "^3.0.0"
ansi-styles "^3.0.0"

pretty-format@^20.0.3:
version "20.0.3"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-20.0.3.tgz#020e350a560a1fe1a98dc3beb6ccffb386de8b14"
Expand Down