-
Notifications
You must be signed in to change notification settings - Fork 23
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
Changes from 2 commits
752d701
fd5035a
7d758f8
3a50ae3
4edefee
58ae918
4f7e4ad
dc5c85f
dcf0402
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`foo 1`] = ` | ||
"- First value | ||
+ Second value | ||
|
||
@@ -5,8 +5,9 @@ | ||
some | ||
some | ||
some | ||
some | ||
not | ||
+ so | ||
very | ||
long | ||
script" | ||
`; | ||
|
||
exports[`proxies "colors" option(s) 1`] = ` | ||
"[32m- First value[39m | ||
[31m+ Second value[39m | ||
|
||
[33m@@ -5,8 +5,9 @@[39m | ||
[33m[39m[2m some[22m | ||
[2m some[22m | ||
[2m some[22m | ||
[2m some[22m | ||
[2m not[22m | ||
[31m+ so[39m | ||
[2m very[22m | ||
[2m long[22m | ||
[2m script[22m" | ||
`; | ||
|
||
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" | ||
`; |
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); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// @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('foo', () => { | ||
expect(a).toMatchDiffSnapshot(b); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also make Flow happy? I'm fine with adding |
||
|
||
[ | ||
{ expand: true }, | ||
{ colors: true }, | ||
{ contextLines: 0 }, | ||
].forEach((options: any) => { | ||
test(`proxies "${Object.keys(options).join(', ')}" option(s)`, () => { | ||
expect(a).toMatchDiffSnapshot(b, options); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference types="jest"/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this valid in TS land? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newline pls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -64,4 +65,12 @@ function diffReactComponents(valueA: any, valueB: any, options: Options) { | |
}); | ||
} | ||
|
||
function toMatchDiffSnapshot(valueA: any, valueB: any, options: Options) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const difference = snapshotDiff(valueA, valueB, options); | ||
|
||
return snapshot.toMatchSnapshot.call(this, difference); | ||
} | ||
|
||
module.exports = snapshotDiff; | ||
module.exports.snapshotDiff = snapshotDiff; | ||
module.exports.toMatchDiffSnapshot = toMatchDiffSnapshot; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test('works with default options', () => {})