Skip to content
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

feat: add match named snapshot #14045

Merged
merged 33 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bd8de10
Add toMatchNamedSnapshot
bakamitai456 Mar 31, 2023
c5fe519
add to match named snapshot to expect
bakamitai456 Mar 31, 2023
35bbdeb
add test
bakamitai456 Mar 31, 2023
6841acf
add e2e testing
bakamitai456 Apr 3, 2023
7dfe361
add to throw error matching named snapshot with e2e
bakamitai456 Apr 4, 2023
b684d88
add unit test named snapshot
bakamitai456 Apr 4, 2023
9445a2f
add e2e toMatchNamedSnapshot with retries
bakamitai456 Apr 4, 2023
6fb5b85
add snapshot file
bakamitai456 Apr 5, 2023
f28f669
add types test
bakamitai456 Apr 5, 2023
14cf63d
add e2e concurrent testing
bakamitai456 Apr 5, 2023
ab462ea
add document
bakamitai456 Apr 7, 2023
c76bf56
fix lint
bakamitai456 Apr 9, 2023
c6ca50e
fixes changelog
bakamitai456 Apr 9, 2023
198fe2e
fixes code style
bakamitai456 Apr 10, 2023
79d3d7d
required snapshot name
bakamitai456 Apr 10, 2023
6b1a6ed
fixes properties
bakamitai456 Apr 10, 2023
735e503
edit e2e testing
bakamitai456 Apr 10, 2023
ed4e605
add e2e type test
bakamitai456 Apr 10, 2023
e3dc8fc
add more e2e type test
bakamitai456 Apr 10, 2023
7e7527a
remove unused overload
bakamitai456 Apr 10, 2023
cbbec0a
add validation on snapshot name
bakamitai456 Apr 10, 2023
77ae436
support array in properties matcher
bakamitai456 Apr 10, 2023
b7b81a1
remove unused function
bakamitai456 Apr 10, 2023
6327019
Apply suggestions from code review
bakamitai456 Apr 10, 2023
58acde3
remove fromPromise type
bakamitai456 Apr 11, 2023
23d8053
update tip on snapshot testing document
bakamitai456 Apr 16, 2023
78ddf16
Fixes alphabetic order
bakamitai456 Apr 16, 2023
435bdc8
Fixes docs
bakamitai456 Apr 16, 2023
9fccf7d
Update docs/SnapshotTesting.md
bakamitai456 Apr 16, 2023
3037070
Throw error when duplicate snapshot name
bakamitai456 Jun 14, 2023
5e58009
fix test
bakamitai456 Jun 15, 2023
acb42b0
fix the test
bakamitai456 Jun 16, 2023
f397e96
Merge branch 'main' into add-match-named-snapshot
SimenB Jun 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
### Features

- `[jest-cli]` Include type definitions to generated config files ([#14078](https://github.com/facebook/jest/pull/14078))
- `[jest-snapshot]` Support arrays as property matchers ([#14025](https://github.com/facebook/jest/pull/14025))
- `[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))
- `[jest-snapshot]` Support arrays as property matchers ([#14025](https://github.com/facebook/jest/pull/14025))
- `[jest-snapshot]` Add `toMatchNamedSnapshot` to bring snapshot support to concurrent mode ([#14045](https://github.com/facebook/jest/pull/14045))

### Fixes

Expand Down
16 changes: 16 additions & 0 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,16 @@ You can provide an optional `propertyMatchers` object argument, which has asymme

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.

### `.toMatchNamedSnapshot(snapshotName, propertyMatchers?)`

This ensures that a value matches the most recent snapshot. Check out [the Snapshot Testing guide](SnapshotTesting.md) for more information.

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.

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.

Jest sorts snapshots by name in the corresponding `.snap` file.

### `.toMatchInlineSnapshot(propertyMatchers?, inlineSnapshot)`

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

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

### `.toThrowErrorMatchingNamedSnapshot(snapshotName)`

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

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.

### `.toThrowErrorMatchingInlineSnapshot(inlineSnapshot)`

Use `.toThrowErrorMatchingInlineSnapshot` to test that a function throws an error matching the most recent snapshot when it is called.
Expand Down
6 changes: 6 additions & 0 deletions docs/SnapshotTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ More information on how snapshot testing works and why we built it can be found

:::

:::tip

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.

:::

### Updating Snapshots

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`support-reject 1`] = `
"// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[\`support-reject 1\`] = \`"octopus"\`;
"
`;
Loading