testing-library / testing-library/react-testing-library

Error: Objects are not valid as a React child ...

Open
#1,387 7 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
19.7k
Forks
1.2k
Avg merge
3d 16h
Merged PRs (30d)
1

Description

Description:
Integrating vitest with a fresh nextJS application results in the following error:

Error: Objects are not valid as a React child (found: object with keys {$$typeof, type, key, props, _owner, _store}). If you meant to render a collection of children, use an array instead.
 ❯ throwOnInvalidObjectType ../../node_modules/react-dom/cjs/react-dom.development.js:13123:9
 ❯ reconcileChildFibers ../../node_modules/react-dom/cjs/react-dom.development.js:14064:7
 ❯ reconcileChildren ../../node_modules/react-dom/cjs/react-dom.development.js:19193:28
 ❯ updateHostRoot ../../node_modules/react-dom/cjs/react-dom.development.js:19912:5
 ❯ beginWork ../../node_modules/react-dom/cjs/react-dom.development.js:21654:14
 ❯ beginWork$1 ../../node_modules/react-dom/cjs/react-dom.development.js:27465:14
 ❯ performUnitOfWork ../../node_modules/react-dom/cjs/react-dom.development.js:26599:12
 ❯ workLoopSync ../../node_modules/react-dom/cjs/react-dom.development.js:26505:5
 ❯ renderRootSync ../../node_modules/react-dom/cjs/react-dom.development.js:26473:7
 ❯ performConcurrentWorkOnRoot ../../node_modules/react-dom/cjs/react-dom.development.js:25777:74

This uses React 19.

If I downgrade to React 18 it works:

Working Screenshot:

Image

Working package.json

{
  "name": "web",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev --turbopack",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "check-types": "tsc --noEmit",
    "test": "vitest run",
    "test:coverage": "vitest run --coverage",
    "test:watch": "vitest run --watch"
  },
  "dependencies": {
    "next": "15.2.3",
    "react": "18.3.1",
    "react-dom": "18.3.1"
  },
  "devDependencies": {
    ...
    "@types/react": "^18.3.1",
    "@types/react-dom": "^18.3.1",
    ...
    "vitest": "^3.0.9",
     "@testing-library/dom": "^10.4.0",
    "@testing-library/jest-dom": "^6.6.3",
    "@testing-library/react": "^16.2.0",
    "@testing-library/react-native": "^13.2.0",
    "@types/react-dom": "^19.0.4",
    "@vitejs/plugin-react": "^4.3.4",
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure with the React 19, Next.js, and Vitest dependency combination shown in package.json, then compare it with the working React 18 setup. Use the reported react-dom stack trace as the entry point; done means a fresh Next.js application runs its Vitest tests without the invalid React child error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js, react
Domain
frontend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.