Skip to content

Commit ab13484

Browse files
feat: add match named snapshot (jestjs#14045)
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
1 parent 43b0ae8 commit ab13484

File tree

21 files changed

+1562
-49
lines changed

21 files changed

+1562
-49
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
### Features
44

55
- `[jest-cli]` Include type definitions to generated config files ([#14078](https://github.com/facebook/jest/pull/14078))
6-
- `[jest-snapshot]` Support arrays as property matchers ([#14025](https://github.com/facebook/jest/pull/14025))
76
- `[jest-core, jest-circus, jest-reporter, jest-runner]` Added support for reporting about start individual test cases using jest-circus ([#14174](https://github.com/jestjs/jest/pull/14174))
7+
- `[jest-snapshot]` Support arrays as property matchers ([#14025](https://github.com/facebook/jest/pull/14025))
8+
- `[jest-snapshot]` Add `toMatchNamedSnapshot` to bring snapshot support to concurrent mode ([#14045](https://github.com/facebook/jest/pull/14045))
89

910
### Fixes
1011

docs/ExpectAPI.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,16 @@ You can provide an optional `propertyMatchers` object argument, which has asymme
744744

745745
You can provide an optional `hint` string argument that is appended to the test name. Although Jest always appends a number at the end of a snapshot name, short descriptive hints might be more useful than numbers to differentiate **multiple** snapshots in a **single** `it` or `test` block. Jest sorts snapshots by name in the corresponding `.snap` file.
746746

747+
### `.toMatchNamedSnapshot(snapshotName, propertyMatchers?)`
748+
749+
This ensures that a value matches the most recent snapshot. Check out [the Snapshot Testing guide](SnapshotTesting.md) for more information.
750+
751+
You can provide an optional `propertyMatchers` object argument, which has asymmetric matchers as values of a subset of expected properties, **if** the received value will be an **object** instance. It is like `toMatchObject` with flexible criteria for a subset of properties, followed by a snapshot test as exact criteria for the rest of the properties.
752+
753+
By setting the `snapshotName` explicitly (as opposed to letting Jest infer the name from context) it can be guaranteed to be consistent across test runs, whatever the context at the time of evaluation. Jest always appends a number at the end of a snapshot name.
754+
755+
Jest sorts snapshots by name in the corresponding `.snap` file.
756+
747757
### `.toMatchInlineSnapshot(propertyMatchers?, inlineSnapshot)`
748758

749759
Ensures that a value matches the most recent snapshot.
@@ -876,6 +886,12 @@ exports[`drinking flavors throws on octopus 1`] = `"yuck, octopus flavor"`;
876886

877887
Check out [React Tree Snapshot Testing](/blog/2016/07/27/jest-14) for more information on snapshot testing.
878888

889+
### `.toThrowErrorMatchingNamedSnapshot(snapshotName)`
890+
891+
Use `.toThrowErrorMatchingNamedSnapshot` to test that a function throws an error matching the most recent snapshot when it is called.
892+
893+
By setting the `snapshotName` explicitly (as opposed to letting Jest infer the name from context) it can be guaranteed to be consistent across test runs, whatever the context at the time of evaluation. Jest always appends a number at the end of a snapshot name.
894+
879895
### `.toThrowErrorMatchingInlineSnapshot(inlineSnapshot)`
880896

881897
Use `.toThrowErrorMatchingInlineSnapshot` to test that a function throws an error matching the most recent snapshot when it is called.

docs/SnapshotTesting.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ More information on how snapshot testing works and why we built it can be found
5252

5353
:::
5454

55+
:::tip
56+
57+
The `toMatchSnapshot` matcher infers the name based on the test function context when the snapshot is evaluated. This can cause snapshots in tests that are run concurrently to have different names on each test run. If you want to guarantee consistent names, you can use the `toMatchNamedSnapshot` matcher.
58+
59+
:::
60+
5561
### Updating Snapshots
5662

5763
It's straightforward to spot when a snapshot test fails after a bug has been introduced. When that happens, go ahead and fix the issue and make sure your snapshot tests are passing again. Now, let's talk about the case when a snapshot test is failing due to an intentional implementation change.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`support-reject 1`] = `
4+
"// Jest Snapshot v1, https://goo.gl/fbAQLP
5+
6+
exports[\`support-reject 1\`] = \`"octopus"\`;
7+
"
8+
`;

0 commit comments

Comments
 (0)