Skip to content

Commit cc0329d

Browse files
feat(posts): add "Fix Jest snapshot tests for React 19"
Post: 2025-04-09-fix-jest-snapshot-tests-for-react-19.md
1 parent a4af84f commit cc0329d

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
layout: post
3+
title: Fix Jest snapshot tests for React 19
4+
date: 2025-04-09 19:05:55
5+
excerpt: How to fix Jest snapshot tests when upgrading React from 18 to 19.
6+
categories: jest snapshot test react
7+
---
8+
9+
## Problem
10+
11+
When upgrading to React 19, Jest snapshot tests can fail with something like the following:
12+
13+
```diff
14+
Error: expect(received).toMatchSnapshot()
15+
16+
Snapshot name: `domToReact converts single DOM node to React 1`
17+
18+
- Snapshot - 3
19+
+ Received + 10
20+
21+
- <p>
22+
- foo
23+
- </p>
24+
+ {
25+
+ "$$typeof": Symbol(react.transitional.element),
26+
+ "_owner": null,
27+
+ "_store": {},
28+
+ "key": null,
29+
+ "props": {
30+
+ "children": "foo",
31+
+ },
32+
+ "type": "p",
33+
+ }
34+
35+
at Object.<anonymous> (__tests__/dom-to-react.test.tsx:24:26)
36+
```
37+
38+
This happens because React changed `Symbol.for('react.element')` to `Symbol.for('react.transitional.element')`.
39+
40+
## Solution
41+
42+
Following this [comment](https://github.com/jestjs/jest/issues/15402#issuecomment-2575408173), you can override `react-is` in `package.json`:
43+
44+
```json
45+
{
46+
"overrides": {
47+
"pretty-format": {
48+
"react-is": "19.1.0"
49+
}
50+
}
51+
}
52+
```
53+
54+
See [example](https://github.com/remarkablemark/html-react-parser/pull/1746).
55+
56+
If the `react` version is specified in dependencies, you can reference the value with `$react`:
57+
58+
```json
59+
{
60+
"dependencies": {
61+
"react": "19.1.0"
62+
},
63+
"overrides": {
64+
"pretty-format": {
65+
"react-is": "$react"
66+
}
67+
}
68+
}
69+
```
70+
71+
See [example](https://github.com/lilboards/lilboards/pull/2467).

0 commit comments

Comments
 (0)