jam*_*ani 9 reactjs jestjs babeljs react-testing-library
我正在使用 Jest 和 react-testing-library 测试调用异步函数的组件。当我运行测试时出现错误ReferenceError: waitForElement is not defined
按照这个说明我尝试过:
withoutuseBuiltins
在 中的选项.babelrc
,包括@babel/polyfill
在文件顶部的app.test.jsx
,以及@babel/polyfill
在 中的条目数组中没有的选项webpack.config.js
。ReferenceError: waitForElement is not defined
我从测试运行中收到错误,但应用程序编译成功
with useBuiltIns: 'entry'
,包括@babel/polyfill
在文件顶部app.test.jsx
,而 without 则@babel/polyfill
位于 中的条目数组中webpack.config.js
。我得到了Cannot find module 'core-js/modules/es6.array.every' from 'app.test.jsx'
,应用程序无法编译。
with useBuiltIns: 'entry'
,不包括@babel/polyfill
在文件顶部app.test.jsx
,并且WITH@babel/polyfill
位于 中的条目数组中webpack.config.js
。ReferenceError: waitForElement is not defined
我从测试运行中 收到错误,并且应用程序无法编译。
这是案例 1 中的代码:
在 app.test.jsx 中导入
import '@babel/polyfill';
import React from 'react';
import { render, fireEvent, cleanup } from 'react-testing-library';
import AppContainer from '../components/AppContainer';
Run Code Online (Sandbox Code Playgroud)
在 app.test.jsx 中测试
test('State change', async () => {
const { debug, getByLabelText, getByTestId, getByText } = render(<AppContainer />);
fireEvent.change(getByLabelText('testfield'), { target: { value: 'Hello' } });
fireEvent.click(getByTestId('button'));
await waitForElement(() => getByText('return value'));
debug();
});
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
const HtmlWebPackPlugin = require('html-webpack-plugin');
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
},
],
},
],
},
resolve: {
extensions: ['*', '.js', '.jsx'],
},
plugins: [
new HtmlWebPackPlugin({
template: './src/index.html',
filename: './index.html',
}),
],
};
Run Code Online (Sandbox Code Playgroud)
.babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
}
Run Code Online (Sandbox Code Playgroud)
我期望该waitForElement
函数等待“返回值”文本出现在第二个文本字段中,然后debug()
等待该函数打印页面 html 代码。相反,我收到了上述错误。
您必须waitForElement
从以下位置导入react-testing-library
:
import { render, waitForElement } from 'react-testing-library'
Run Code Online (Sandbox Code Playgroud)
无需安装,dom-testing-library
因为 RTL 已经依赖于它。
归档时间: |
|
查看次数: |
14180 次 |
最近记录: |