【发布时间】:2020-03-08 20:10:09
【问题描述】:
我正在使用 Jest 和 testing-library/react 为我的 React 应用程序编写测试
我的测试通过了,但我无法摆脱这个错误:
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.
我也看到此警告的多个实例:
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 */
这是我的测试套件顶部的导入:
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';
这是我的 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 已经在 v16.9 上。我错过了什么?
令人沮丧的是,此错误似乎间歇性地出现。当我尝试不同的东西时,它有时会在运行几次测试套件后完全消失,然后返回。
【问题讨论】:
-
你试过了吗?
rm -rf ./node_modules && npm install -
@LongNguyen 是的,我会定期尝试但没有成功。
-
确保您的
react和react-dom软件包版本相同。 -
@Clarity 我将
react-dom提高到16.11(以匹配react),这使得“等待”错误消失了。 ???似乎我的问题有两个不同的问题,所以我将把第二艺术分成一个新问题。如果您为刚刚解决的问题提交答案,我会接受。 -
@Clarity 我将第二部分拆分为这个新问题:stackoverflow.com/questions/58847236/…随时准备接受您的回答...
标签: reactjs jestjs react-testing-library