首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >意料之外的保留词“等待”在开玩笑中

意料之外的保留词“等待”在开玩笑中
EN

Stack Overflow用户
提问于 2022-04-25 04:29:10
回答 1查看 441关注 0票数 1

我正在开发一个反应本机应用程序。但它会给出一个错误,因为在控制台中给出了下面的图像。这里我已经附加了js文件和测试文件。

错误显示在终端

测试套件无法运行

代码语言:javascript
复制
SyntaxError: C:\Geethma\skyU_frontend\skyu-frontend\src\features\dashboard\components\jest-tests\ExpandedInstancDetails.test.js: Unexpected reserved word 'await'. (10:8)

   8 |       }
   9 |     it("should go to after click",()=>{
> 10 |         await act(async () => {
     |         ^
  11 |             render(
  12 |               <Router history={mockHistory}>
  13 |                 <ExpandedInstancDetails />

  at instantiate (node_modules/@babel/parser/src/parse-error/credentials.js:61:22)
  at toParseError (node_modules/@babel/parser/src/parse-error.js:58:12)
  at Parser.raise (node_modules/@babel/parser/src/tokenizer/index.js:1763:19)
  at Parser.checkReservedWord (node_modules/@babel/parser/src/parser/expression.js:2718:12)
  at Parser.parseIdentifierName (node_modules/@babel/parser/src/parser/expression.js:2653:12)
  at Parser.parseIdentifier (node_modules/@babel/parser/src/parser/expression.js:2621:23)
  at Parser.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1256:27)
  at Parser.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:683:23)
  at Parser.parseUpdate (node_modules/@babel/parser/src/parser/expression.js:662:21)
  at Parser.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:631:23)

测试套件:1失败,1总测试:0总快照:0总时间: 3.712 s运行所有测试套件。

ExpandedInstancDetails.js文件

代码语言:javascript
复制
    import { useHistory } from "react-router-dom";
    
    const ExpandedInstancDetails = () => {
      let history = useHistory();
    
      function handleClick() {
        history.push("/home");
      }
    
      return (
        <button data-testid="button" type="button" onClick={handleClick}>
          Go home
        </button>
      );
    }
export default ExpandedInstancDetails;

ExpandedInstancDetails.test.js文件

代码语言:javascript
复制
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';    
import ExpandedInstancDetails from "../ExpandedInstancDetails";

describe('HomeButton', () => {
  
  const mockHistory = {
    push: jest.fn(),
  }

  it('should go to home after click', () => {
    await act(async () => {
      render(
        <Router history={mockHistory}>
          <ExpandedInstancDetails/>
        </Router>
      )

      userEvent.click(screen.getByTestId('button'))
    })

    
    expect(mockHistory.push).toBeCalledWith("/home")
  })
})
EN

回答 1

Stack Overflow用户

发布于 2022-04-25 04:36:29

await需要和async一起走。单元测试不声明async,这就是它抛出错误的原因。

对于修复,您应该在it之后添加it,而不是在act内部添加

代码语言:javascript
复制
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';    
import ExpandedInstancDetails from "../ExpandedInstancDetails";

describe('HomeButton', () => {
  
  const mockHistory = {
    push: jest.fn(),
  }
  
  //add `async` after `it`
  it('should go to home after click', async () => {
    await act(() => {
      render(
        <Router history={mockHistory}>
          <ExpandedInstancDetails/>
        </Router>
      )

      userEvent.click(screen.getByTestId('button'))
    })

    
    expect(mockHistory.push).toBeCalledWith("/home")
  })
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71994518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档