I am testing with Jest and react-testing-library a component that is calling an async function. When I run the test I get the error ReferenceError: waitForElement is not defined
Following this instructions I have tried:
without the
useBuiltins
option in.babelrc
, including@babel/polyfill
at the top of theapp.test.jsx
file, , and without@babel/polyfill
in the entry array inwebpack.config.js
. I get the errorReferenceError: waitForElement is not defined
from the test run but the application compiles succesfullywith
useBuiltIns: 'entry'
, including@babel/polyfill
at the top of theapp.test.jsx
file, and without@babel/polyfill
in the entry array inwebpack.config.js
. I getCannot find module 'core-js/modules/es6.array.every' from 'app.test.jsx'
and the application fails to compile.with
useBuiltIns: 'entry'
, NOT including@babel/polyfill
at the top of theapp.test.jsx
file, and WITH@babel/polyfill
in the entry array inwebpack.config.js
. I get the errorReferenceError: waitForElement is not defined
from the test run and the application fails to compile.
Here is the code from case 1:
imports in app.test.jsx
import '@babel/polyfill';
import React from 'react';
import { render, fireEvent, cleanup } from 'react-testing-library';
import AppContainer from '../components/AppContainer';
test in 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
}
]
]
}
I am expecting the waitForElement
function to be awaiting for the "return value" text to appear in a second textfield, and then the debug()
function to print the page html code. Instead I get the above mentioned errors.