0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Reactのテスト(Jest)でWarning(ReactDOM.render is no longer supported in React 18)が発生する

Posted at

はじめに

Jestのテストを書いていて気になった個所があったので修正箇所をまとめています

問題

Reactのテストを実行すると赤文字で以下のエラーが出力されるようになりました

ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot

テストは問題なく通るのですが、warningでログに色々表示されてしまうので非表示にしようと思いました

解決方法

setupTests.tsを以下に変更したところwarningが消えました

setupTests.ts
import '@testing-library/jest-dom';

const originalError = console.error;
beforeAll(() => {
  console.error = (...args) => {
    if (/Warning: ReactDOM.render is no longer supported in React 18./.test(args[0])) {
      return;
    }
    originalError.call(console, ...args);
  };
});

afterAll(() => {
  console.error = originalError;
});

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?