-
-
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 4 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,96 @@ 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); | ||
} | ||
}); | ||
|
||
test('handles property matchers with deep expect.objectContaining', () => { | ||
const filename = 'handle-property-matchers-with-name.test.js'; | ||
const template = makeTemplate(`test('handles property matchers with deep expect.objectContaining', () => { | ||
expect({ user: { createdAt: $1, name: 'Jest' }}).toMatchSnapshot({ user: expect.objectContaining({ createdAt: expect.any(Date) }) }); | ||
}); | ||
`); | ||
|
||
{ | ||
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 deep expect.objectContaining 1".', | ||
); | ||
expect(stderr).toMatch('Snapshots: 1 failed, 1 total'); | ||
expect(status).toBe(1); | ||
} | ||
}); |
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