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)

Image

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

Most upvoted comments

As a hotfix, you can add overrides to your package.json

{
  "overrides": {
    "pretty-format": {
      "react-is": "19.0.0"
    }
  }
}

The issue is related to react changing the Symbol.for('react.element') to Symbol.for('react.transitional.element') - see https://github.com/facebook/react/commit/3b551c82844bcfde51f0febb8e42c1a0d777df2c

This is a bit tricky for Jest as updating the react-is library 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 adding react-is as a peer dependency and having users specify the react-is version as a dependency in their projects, but it is not very practical especially in a library like Jest.

Maybe updating react-is to v19 (in pretty-format) and changing this condition with

typeof val === 'object' &&
val != null &&
(
  ReactIs.isElement(val) ||
  val.$$typeof === Symbol.for('react.element') // Compatibility with react <19
)

will 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 in RangeError: Invalid string length.

@Filipoliko’s comment did the trick.

For other yarn users, the syntax is

{
    "resolutions": {
        "**/pretty-format/react-is": "19.0.0"
    }
}

This 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 000 lines long. We used a solution with overrides: 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.element symbol. 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.