Reactjs React JS:使用Jest测试异步函数是抛出错误;引用错误:未定义regeneratorRuntime“;

Reactjs React JS:使用Jest测试异步函数是抛出错误;引用错误:未定义regeneratorRuntime“;,reactjs,unit-testing,jestjs,axios-mock-adapter,Reactjs,Unit Testing,Jestjs,Axios Mock Adapter,我正在使用Jest为React JS web应用程序编写单元测试/集成测试。我正在测试的组件正在异步进行API调用。我使用这个库来模拟API请求 import React from 'react'; import {cleanup, fireEvent, render, screen, waitForElement} from '@testing-library/react'; import ExampleList from "../../src/components/ExampleL

我正在使用Jest为React JS web应用程序编写单元测试/集成测试。我正在测试的组件正在异步进行API调用。我使用这个库来模拟API请求

import React from 'react';
import {cleanup, fireEvent, render, screen, waitForElement} from '@testing-library/react';
import ExampleList from "../../src/components/ExampleList";
import MockAdapter from "axios-mock-adapter";
import Axios from 'axios';

test("ExampleList Component", async () => {
    // first mock the API request.
    let mock = new MockAdapter(Axios);
    mock.onGet("http://dummy.restapiexample.com/api/v1/employees")
        .reply(200, {
            "status":"success",
            "data":[
                {
                    "id":"1",
                    "employee_name":"Wai Yan Hein",
                    "employee_salary":"720800",
                    "employee_age":"61"
                }
            ]
        })

    let  { getByText } = render(<ExampleList />)

    const listData = await waitForElement(getByText("Wai Yan Hein"))
    expect(listData).toBeTruthy()
})
这是我的组件

import React, { useState, useEffect } from 'react';
import Axios from 'axios';

const ExampleList = (props) => {
    const [ list, setList ] = useState([ ])

    const fetchList = async () => {
        let response = await Axios.get("http://dummy.restapiexample.com/api/v1/employees")
        setList(response.data.data)
    }

    const renderList = () => {
        return list.map((item, index) => {
            return <li key={index}>{item.employee_name}</li>
        })
    }

    useEffect(() => {
        fetchList()
    }, [ ])

    return (
        <div>
            <ul>
                {renderList()}
            </ul>
        </div>
    )
}

export default ExampleList;
import React,{useState,useffect}来自“React”;
从“Axios”导入Axios;
常量示例列表=(道具)=>{
const[list,setList]=useState([]
常量fetchList=async()=>{
let response=等待Axios.get(“http://dummy.restapiexample.com/api/v1/employees")
setList(response.data.data)
}
常量renderList=()=>{
返回列表.map((项目,索引)=>{
return
  • {item.employee\u name}
  • }) } useffect(()=>{ fetchList() }, [ ]) 返回(
      {renderList()}
    ) } 导出默认示例列表;
    这是我模拟API请求的测试

    import React from 'react';
    import {cleanup, fireEvent, render, screen, waitForElement} from '@testing-library/react';
    import ExampleList from "../../src/components/ExampleList";
    import MockAdapter from "axios-mock-adapter";
    import Axios from 'axios';
    
    test("ExampleList Component", async () => {
        // first mock the API request.
        let mock = new MockAdapter(Axios);
        mock.onGet("http://dummy.restapiexample.com/api/v1/employees")
            .reply(200, {
                "status":"success",
                "data":[
                    {
                        "id":"1",
                        "employee_name":"Wai Yan Hein",
                        "employee_salary":"720800",
                        "employee_age":"61"
                    }
                ]
            })
    
        let  { getByText } = render(<ExampleList />)
    
        const listData = await waitForElement(getByText("Wai Yan Hein"))
        expect(listData).toBeTruthy()
    })
    
    从“React”导入React;
    从'@testing library/react'导入{cleanup,firevent,render,screen,waitForElement};
    从“../../src/components/ExampleList”导入示例列表;
    从“axios模拟适配器”导入模拟适配器;
    从“Axios”导入Axios;
    测试(“示例列表组件”,异步()=>{
    //首先模拟API请求。
    让mock=新MockAdapter(Axios);
    mock.onGet(“http://dummy.restapiexample.com/api/v1/employees")
    .答复(200{
    “状态”:“成功”,
    “数据”:[
    {
    “id”:“1”,
    “员工姓名”:“伟仁恒”,
    “员工工资”:“720800”,
    “员工年龄”:“61”
    }
    ]
    })
    让{getByText}=render()
    const listData=wait waitForElement(getByText(“Wai-Yan-Hein”))
    expect(listData.toBeTruthy())
    })
    
    当我运行测试时,我得到以下错误

     FAIL  tests/unit/ExampleList.test.js
      ● Test suite failed to run
    
        ReferenceError: regeneratorRuntime is not defined
    
           5 | import Axios from 'axios';
           6 |
        >  7 | test("ExampleList Component", async () => {
             |     ^
           8 |     // first mock the API request.
           9 |     let mock = new MockAdapter(Axios);
          10 |     mock.onGet("http://dummy.restapiexample.com/api/v1/employees")
    
          at Object.<anonymous> (tests/unit/ExampleList.test.js:7:5)
    
    失败测试/unit/ExampleList.test.js
    ● 测试套件无法运行
    ReferenceError:未定义regeneratorRuntime
    5 |从“Axios”导入Axios;
    6 |
    >7 |测试(“示例列表组件”,异步()=>{
    |     ^
    8 |//首先模拟API请求。
    9 |让mock=新MockAdapter(Axios);
    10 |模拟onGet(“http://dummy.restapiexample.com/api/v1/employees")
    at对象。(tests/unit/ExampleList.test.js:7:5)
    

    我的代码出了什么问题,我如何修复它?

    显示你的
    jest.config.js
    你需要安装“babel polyfill”才能使用异步等待编程请通过-