jest: [Bug]: Snapshot tests failing for React 19
Version
29.7.0
Steps to reproduce
Clone repo:
git clone https://github.com/remarkablemark/html-react-parser.git
cd html-react-parser
git checkout build/package
Install deps:
npm i
Run tests:
npm test
See https://github.com/remarkablemark/html-react-parser/pull/1629#issuecomment-2521866508
Expected behavior
Snapshot tests without issues
Actual behavior
Snapshot tests failing for React 19:
Error: expect(received).toMatchSnapshot()
Snapshot name: `domToReact converts single DOM node to React 1`
- Snapshot - 3
+ Received + 10
- <p>
- foo
- </p>
+ {
+ "$$typeof": Symbol(react.transitional.element),
+ "_owner": null,
+ "_store": {},
+ "key": null,
+ "props": {
+ "children": "foo",
+ },
+ "type": "p",
+ }
at Object.<anonymous> (__tests__/dom-to-react.test.tsx:24:26)
Additional context
See blog post
Environment
System:
OS: macOS 14.4.1
CPU: (8) arm64 Apple M1 Pro
Binaries:
Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node
Yarn: 1.22.22 - /opt/homebrew/bin/yarn
npm: 10.8.3 - ~/.nvm/versions/node/v22.9.0/bin/npm
pnpm: 9.14.4 - ~/Library/pnpm/pnpm
npmPackages:
jest: 29.7.0 => 29.7.0
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 17
- Comments: 15 (6 by maintainers)
Commits related to this issue
- deps(react19): Pin react-is to specific version Some of our legacy dependencies are holding back the version of react-is we have installed. It is used in prop-types and pretty-format which is used in... — committed to getsentry/sentry by scttcper 2 years ago
- test: pin pretty-format/react-is ref: https://github.com/jestjs/jest/issues/15402 — committed to gmsgowtham/react-native-marked by gmsgowtham 2 years ago
- fix(deps): upgrade react to v19 (#833) * fix(deps): upgrade react to v19 * chore: upgrade testing libs * test: pin pretty-format/react-is ref: https://github.com/jestjs/jest/issues/15402 * chore:... — committed to gmsgowtham/react-native-marked by gmsgowtham 2 years ago
- build(package): bump react and react-dom from 18.3.1 to 19.1.0 Fix Jest snapshots with `react-is` overrides: https://github.com/jestjs/jest/issues/15402#issuecomment-2575408173 — committed to remarkablemark/html-react-parser by remarkablemark 2 years ago
- build(package): update overrides to reference react version for react-is https://github.com/jestjs/jest/issues/15402#issuecomment-2575408173 — committed to remarkablemark/html-react-parser by remarkablemark 2 years ago
- build(package): upgrade `react-dom` and set overrides for `react-is` https://github.com/jestjs/jest/issues/15402#issuecomment-2575408173 — committed to lilboards/lilboards by remarkablemark 2 years ago
- build(deps): bump react from 18 to 19 (#2467) * build(deps): bump react and @types/react Bumps [react](https://github.com/facebook/react/tree/HEAD/packages/react) and [@types/react](https://github.c... — committed to lilboards/lilboards by dependabot[bot] 2 years ago
As a hotfix, you can add overrides to your package.json
The issue is related to react changing the
Symbol.for('react.element')toSymbol.for('react.transitional.element')- see https://github.com/facebook/react/commit/3b551c82844bcfde51f0febb8e42c1a0d777df2cThis is a bit tricky for Jest as updating the
react-islibrary to v19 will break react <19 and keeping the current version breaks the new react. I am having very similar issue in my library for shallow rendering react components and my current solution is addingreact-isas a peer dependency and having users specify thereact-isversion as a dependency in their projects, but it is not very practical especially in a library like Jest.Maybe updating
react-isto v19 (inpretty-format) and changing this condition withwill solve the issue? But I am not sure if there are more use-cases, that can break.
I had another related issue when the component to be serialized received a React Element as a prop. It tried to serialize it with
printComplexValue, ending inRangeError: Invalid string length.@Filipoliko’s comment did the trick.
For other
yarnusers, the syntax isThis issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
We just ran into a problem with massive snapshots in Jest 30, RN 79. 1 snapshot for a trivial file was
400 000lines long. We used a solution withoverrides: https://github.com/jestjs/jest/issues/15402#issuecomment-2575408173 But it’ll be nice to see this fixed.I tried, it failed. There are more use-cases, which need to be covered and not just the
react.elementsymbol. Most of them can be solved in a similar way, but there is one use-case, which cannot be compatible with both React 18 and 19. See #15729 for explanation and implementation attempt.Maybe Jest should just ditch support for older React at some point and offer this workaround as a solution for those with older React versions. But right now, React v18 still has more downloads compared to React v19 (at least in the npm registry), so maybe it is too early to do this transition now.