React + Jest + testing-library: “Please upgrade to at least react-dom@16.9.0 to remove this warning.” …but react-dom is 16.9.0 already

旧城冷巷雨未停 提交于 2019-12-24 23:25:03

问题


I'm writing tests for my React app using Jest and testing-library/react

My tests pass but I can't get rid of this error:

It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least react-dom@16.9.0 to remove this warning.

I am also seeing multiple instances of this warning:

Warning: An update to ConnectFunction inside a test was not wrapped in act(...).

When testing, code that causes React state updates should be wrapped into act(...):

act(() => {
  /* fire events that update state */
});
/* assert on the output */

Here are the imports at the top of my test suite:

import React from "react";
import { BrowserRouter } from "react-router-dom";
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { createMemoryHistory } from 'history';

//testing-library/react
import { render, fireEvent, waitForElement, act } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import {
  wait
} from '@testing-library/dom';
import MyComponent from '../MyComponent';

Here's my package.json

  "dependencies": {
    "axios": "^0.18.1",
    "react": "^16.11.0",
    "react-dom": "^16.9.0",
    "react-scripts": "2.1.5",
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^4.2.3",
    "@testing-library/react": "^9.3.2",
  }

react-dom is already on v16.9. What am I missing?

Frustratingly, this error seems to appear intermittently. As I'm trying different things it will sometimes vanish entirely for a few runs of the test suite and then return.


回答1:


After several days of struggling with this mystery, I finally restarted my computer and the errors/warnings vanished. I don't understand why that mattered, as I had restarted the test script many times, as well as completely re-installed node_modules many times. My best guess is that it had something to do with transitioning from react-test-renderer module to the testing-library wrapper... but that is pure speculation.

  • macOS v10.14.6
  • node v11.10.1
  • npm v6.7.0

Hope this saves someone else some time.



来源:https://stackoverflow.com/questions/58830157/react-jest-testing-library-please-upgrade-to-at-least-react-dom16-9-0-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!