-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Add snapshot property matchers #6210
Changes from 2 commits
6366b3e
46e7bbd
5fe1459
29f5c5d
4dcd25b
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 |
---|---|---|
|
@@ -159,3 +159,65 @@ test('accepts custom snapshot name', () => { | |
expect(status).toBe(0); | ||
} | ||
}); | ||
|
||
test('handles property matchers', () => { | ||
const filename = 'handle-property-matchers.test.js'; | ||
const template = makeTemplate(`test('handles property matchers', () => { | ||
expect({createdAt: $1}).toMatchSnapshot({createdAt: expect.any(Date)}); | ||
}); | ||
`); | ||
|
||
{ | ||
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. weird to use scopes here, although I get why you do it. why not just a 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. My thoughts as well - I was copying the style from the rest of the suite. I'll update the suite 👌 |
||
writeFiles(TESTS_DIR, {[filename]: template(['new Date()'])}); | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch('1 snapshot written from 1 test suite.'); | ||
expect(status).toBe(0); | ||
} | ||
|
||
{ | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch('Snapshots: 1 passed, 1 total'); | ||
expect(status).toBe(0); | ||
} | ||
|
||
{ | ||
writeFiles(TESTS_DIR, {[filename]: template(['"string"'])}); | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch( | ||
'Received value does not match snapshot properties for "handles property matchers 1".', | ||
); | ||
expect(stderr).toMatch('Snapshots: 1 failed, 1 total'); | ||
expect(status).toBe(1); | ||
} | ||
}); | ||
|
||
test('handles property matchers with custom name', () => { | ||
const filename = 'handle-property-matchers-with-name.test.js'; | ||
const template = makeTemplate(`test('handles property matchers with name', () => { | ||
expect({createdAt: $1}).toMatchSnapshot({createdAt: expect.any(Date)}, 'custom-name'); | ||
}); | ||
`); | ||
|
||
{ | ||
writeFiles(TESTS_DIR, {[filename]: template(['new Date()'])}); | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch('1 snapshot written from 1 test suite.'); | ||
expect(status).toBe(0); | ||
} | ||
|
||
{ | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch('Snapshots: 1 passed, 1 total'); | ||
expect(status).toBe(0); | ||
} | ||
|
||
{ | ||
writeFiles(TESTS_DIR, {[filename]: template(['"string"'])}); | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch( | ||
'Received value does not match snapshot properties for "handles property matchers with name: custom-name 1".', | ||
); | ||
expect(stderr).toMatch('Snapshots: 1 failed, 1 total'); | ||
expect(status).toBe(1); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import type { | |
} from 'types/Matchers'; | ||
|
||
import * as utils from 'jest-matcher-utils'; | ||
import {iterableEquality, subsetEquality, getObjectSubset} from './utils'; | ||
import matchers from './matchers'; | ||
import spyMatchers from './spy_matchers'; | ||
import toThrowMatchers, { | ||
|
@@ -223,7 +224,10 @@ const makeThrowingMatcher = ( | |
getState(), | ||
{ | ||
equals, | ||
getObjectSubset, | ||
isNot, | ||
iterableEquality, | ||
subsetEquality, | ||
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. Thoughts on exposing these to matchers and how? 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. maybe stick them in 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 had the same thought, will move 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. also needs to be doced, fwiw |
||
utils, | ||
}, | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,8 +48,13 @@ const cleanup = (hasteFS: HasteFS, update: SnapshotUpdateState) => { | |
}; | ||
}; | ||
|
||
const toMatchSnapshot = function(received: any, testName?: string) { | ||
const toMatchSnapshot = function( | ||
received: any, | ||
propertyMatchers?: any, | ||
testName?: string, | ||
) { | ||
this.dontThrow && this.dontThrow(); | ||
testName = typeof propertyMatchers === 'string' ? propertyMatchers : testName; | ||
|
||
const {currentTestName, isNot, snapshotState}: MatcherState = this; | ||
|
||
|
@@ -61,12 +66,43 @@ const toMatchSnapshot = function(received: any, testName?: string) { | |
throw new Error('Jest: snapshot state must be initialized.'); | ||
} | ||
|
||
const result = snapshotState.match( | ||
const fullTestName = | ||
testName && currentTestName | ||
? `${currentTestName}: ${testName}` | ||
: currentTestName || '', | ||
received, | ||
); | ||
: currentTestName || ''; | ||
|
||
if (typeof propertyMatchers === 'object') { | ||
const propertyPass = this.equals(received, propertyMatchers, [ | ||
this.iterableEquality, | ||
this.subsetEquality, | ||
]); | ||
|
||
if (!propertyPass) { | ||
const key = snapshotState.fail(fullTestName, received); | ||
|
||
const report = () => | ||
`${RECEIVED_COLOR('Received value')} does not match ` + | ||
`${EXPECTED_COLOR(`snapshot properties for "${key}"`)}.\n\n` + | ||
`Expected snapshot to match properties:\n` + | ||
` ${this.utils.printExpected(propertyMatchers)}` + | ||
`\nReceived:\n` + | ||
` ${this.utils.printReceived(received)}`; | ||
|
||
return { | ||
message: () => | ||
matcherHint('.toMatchSnapshot', 'value', 'properties') + | ||
'\n\n' + | ||
report(), | ||
name: 'toMatchSnapshot', | ||
pass: false, | ||
report, | ||
}; | ||
} else { | ||
Object.assign(received, propertyMatchers); | ||
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. This won't handle deep objects - should it be a deep merge instead? 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. Great catch, i'll add an integration test for this as well 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. Any follow up for this? 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 result = snapshotState.match(fullTestName, received); | ||
const {pass} = result; | ||
let {actual, expected} = result; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ const { | |
Immutable, | ||
ReactElement, | ||
ReactTestComponent, | ||
AsymmetricMatcher, | ||
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. @thymikee already handled all the hard stuff for me, thanks! |
||
} = prettyFormat.plugins; | ||
|
||
let PLUGINS: Array<Plugin> = [ | ||
|
@@ -27,6 +28,7 @@ let PLUGINS: Array<Plugin> = [ | |
DOMCollection, | ||
Immutable, | ||
jestMockSerializer, | ||
AsymmetricMatcher, | ||
]; | ||
|
||
// Prepend to list so the last added is the first tested. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,6 @@ | |
"Watch more videos|no description given": "Watch more videos", | ||
"Who's using Jest?|no description given": "Who's using Jest?", | ||
"Jest is used by teams of all sizes to test web applications, node.js services, mobile apps, and APIs.|no description given": "Jest is used by teams of all sizes to test web applications, node.js services, mobile apps, and APIs.", | ||
"More Jest Users|no description given": "More Jest Users", | ||
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. Unrelated but needed on master |
||
"Talks & Videos|no description given": "Talks & Videos", | ||
"We understand that reading through docs can be boring sometimes. Here is a community curated list of talks & videos around Jest.|no description given": "We understand that reading through docs can be boring sometimes. Here is a community curated list of talks & videos around Jest.", | ||
"Add your favorite talk|no description given": "Add your favorite talk", | ||
|
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.
I think it will be better to go with something like this:
Even though we explain this later