【问题标题】:"ReferenceError: waitForElement is not defined" when testing react.js测试 react.js 时出现“ReferenceError:waitForElement 未定义”
【发布时间】:2019-09-02 02:55:58
【问题描述】:

我正在使用 Jest 和 react-testing-library 测试一个调用异步函数的组件。当我运行测试时,我收到错误 ReferenceError: waitForElement is not defined

关注this instructions我试过了:

  1. .babelrc 中没有useBuiltins选项,包括在app.test.jsx 文件顶部的@babel/polyfill,并且在webpack.config.js 的条目数组中没有@babel/polyfill。我从测试运行中收到错误ReferenceError: waitForElement is not defined,但应用程序编译成功

  2. 带有useBuiltIns: 'entry',包括@babel/polyfillapp.test.jsx文件的顶部,并且在webpack.config.js的入口数组中没有@babel/polyfill。我得到Cannot find module 'core-js/modules/es6.array.every' from 'app.test.jsx' 并且应用程序无法编译。

  3. 带有useBuiltIns: 'entry',不包括@babel/polyfill 位于app.test.jsx 文件的顶部,以及带有@babel/polyfillwebpack.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';

在 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();
});

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',
    }),
  ],
};

.babelrc

{
    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "plugins": [
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ]
  ]
}

我期待 waitForElement 函数等待“返回值”文本出现在第二个文本字段中,然后 debug() 函数打印页面 html 代码。相反,我得到了上述错误。

【问题讨论】:

    标签: reactjs jestjs babeljs react-testing-library


    【解决方案1】:

    你必须从react-testing-library导入waitForElement

    import { render, waitForElement } from 'react-testing-library'
    

    不需要安装dom-testing-library,因为 RTL 已经依赖它了。

    【讨论】:

      【解决方案2】:

      好的,我解决了这个问题。我错过了 dom-testing-library 依赖项。

      这是可行的解决方案。

      • 安装 dom-testing 库:npm install --save-dev dom-testing-library

      • app.test.jsximport waithForElement@babel/polyfill

      import '@babel/polyfill';
      import { waitForElement } from 'dom-testing-library';
      
      • 其余代码与上述案例 1 相同。

      【讨论】:

        猜你喜欢
        • 2014-05-22
        • 2017-10-09
        • 1970-01-01
        • 1970-01-01
        • 2020-07-19
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多