-
-
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
Document and test custom, async, inline snapshot matcher #10922
Merged
SimenB
merged 8 commits into
jestjs:master
from
eps1lon:docs/custom-async-inline-snapshot-matcher
Dec 7, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9c2e5e
docs: Example implementation for async inline snapshot matchers
eps1lon 1867543
Add e2e test for documented implementation
eps1lon dd47766
Fix typo
eps1lon f69e881
Explain example
eps1lon a1ad983
Actual snapshot
eps1lon de035f6
Fix typo in result number
eps1lon 22fbbd7
Explain ci flag
eps1lon ce36eac
Backport docs to jest 25
eps1lon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
e2e/__tests__/__snapshots__/customInlineSnapshotMatchers.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`works with custom inline snapshot matchers 1`] = ` | ||
FAIL __tests__/asynchronous.test.js | ||
✕ new async, inline snapshots | ||
✕ mismatching async, inline snapshots | ||
|
||
● new async, inline snapshots | ||
|
||
expect(received).toMatchInlineSnapshot() | ||
|
||
Snapshot name: \`new async, inline snapshots 1\` | ||
|
||
New snapshot was not written. The update flag must be explicitly passed to write a new snapshot. | ||
|
||
This is likely because this test is run in a continuous integration (CI) environment in which snapshots are not written by default. | ||
|
||
Received: "result #1" | ||
|
||
20 | | ||
21 | test('new async, inline snapshots', async () => { | ||
> 22 | await expect(async () => 'result #1').toMatchObservationInlineSnapshot(); | ||
| ^ | ||
23 | await expect(async () => 'result #2').toMatchObservationInlineSnapshot(); | ||
24 | }); | ||
25 | | ||
|
||
at Object.toMatchObservationInlineSnapshot (__tests__/asynchronous.test.js:22:41) | ||
|
||
● new async, inline snapshots | ||
|
||
expect(received).toMatchInlineSnapshot() | ||
|
||
Snapshot name: \`new async, inline snapshots 2\` | ||
|
||
New snapshot was not written. The update flag must be explicitly passed to write a new snapshot. | ||
|
||
This is likely because this test is run in a continuous integration (CI) environment in which snapshots are not written by default. | ||
|
||
Received: "result #2" | ||
|
||
21 | test('new async, inline snapshots', async () => { | ||
22 | await expect(async () => 'result #1').toMatchObservationInlineSnapshot(); | ||
> 23 | await expect(async () => 'result #2').toMatchObservationInlineSnapshot(); | ||
| ^ | ||
24 | }); | ||
25 | | ||
26 | test('mismatching async, inline snapshots', async () => { | ||
|
||
at Object.toMatchObservationInlineSnapshot (__tests__/asynchronous.test.js:23:41) | ||
|
||
● mismatching async, inline snapshots | ||
|
||
expect(received).toMatchInlineSnapshot(snapshot) | ||
|
||
Snapshot name: \`mismatching async, inline snapshots 1\` | ||
|
||
Snapshot: "result #?" | ||
Received: "result #1" | ||
|
||
25 | | ||
26 | test('mismatching async, inline snapshots', async () => { | ||
> 27 | await expect(async () => 'result #1').toMatchObservationInlineSnapshot( | ||
| ^ | ||
28 | \`"result #?"\`, | ||
29 | ); | ||
30 | await expect(async () => 'result #2').toMatchObservationInlineSnapshot( | ||
|
||
at Object.toMatchObservationInlineSnapshot (__tests__/asynchronous.test.js:27:41) | ||
|
||
● mismatching async, inline snapshots | ||
|
||
expect(received).toMatchInlineSnapshot(snapshot) | ||
|
||
Snapshot name: \`mismatching async, inline snapshots 2\` | ||
|
||
Snapshot: "result #?" | ||
Received: "result #2" | ||
|
||
28 | \`"result #?"\`, | ||
29 | ); | ||
> 30 | await expect(async () => 'result #2').toMatchObservationInlineSnapshot( | ||
| ^ | ||
31 | \`"result #?"\`, | ||
32 | ); | ||
33 | }); | ||
|
||
at Object.toMatchObservationInlineSnapshot (__tests__/asynchronous.test.js:30:41) | ||
|
||
› 4 snapshots failed. | ||
Snapshot Summary | ||
› 4 snapshots failed from 1 test suite. Inspect your code changes or re-run jest with \`-u\` to update them. | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {wrap} from 'jest-snapshot-serializer-raw'; | ||
import {extractSummary} from '../Utils'; | ||
import runJest from '../runJest'; | ||
|
||
test('works with custom inline snapshot matchers', () => { | ||
const {stderr} = runJest('custom-inline-snapshot-matchers', [ | ||
// Prevent adding new snapshots or rather changing the test. | ||
'--ci', | ||
'asynchronous.test.js', | ||
]); | ||
|
||
let {rest} = extractSummary(stderr); | ||
|
||
rest = rest | ||
.split('\n') | ||
.filter(line => line.indexOf('at Error (native)') < 0) | ||
.join('\n'); | ||
|
||
expect(wrap(rest)).toMatchSnapshot(); | ||
}); |
33 changes: 33 additions & 0 deletions
33
e2e/custom-inline-snapshot-matchers/__tests__/asynchronous.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
const {toMatchInlineSnapshot} = require('jest-snapshot'); | ||
|
||
expect.extend({ | ||
async toMatchObservationInlineSnapshot(fn, ...args) { | ||
this.error = new Error(); | ||
// This specific behavior can be implemented without a custom matcher. | ||
// In a real example one might want to observe some global value that `fn()` is affecting. | ||
// The difference between before and after `fn()` might then be persisted as a snapshot. | ||
const result = await fn(); | ||
|
||
return toMatchInlineSnapshot.call(this, result, ...args); | ||
}, | ||
}); | ||
|
||
test('new async, inline snapshots', async () => { | ||
await expect(async () => 'result #1').toMatchObservationInlineSnapshot(); | ||
await expect(async () => 'result #2').toMatchObservationInlineSnapshot(); | ||
}); | ||
|
||
test('mismatching async, inline snapshots', async () => { | ||
await expect(async () => 'result #1').toMatchObservationInlineSnapshot( | ||
`"result #?"`, | ||
); | ||
await expect(async () => 'result #2').toMatchObservationInlineSnapshot( | ||
`"result #?"`, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 wonder if Jest itself could instantiate an error and store it before calling this function? So that particular gotcha is gone. Not needed for now of course, and documenting for Jest 25 and 26 is great regardless
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.
Yeah, I did some shallow debugging but couldn't really figure out why it was working without
this.error = new Error()
for existing snapshots but not for new snapshots. I might revisit this though having a test in the repo is a sufficient guarantee for me now 😉