我正在开发一个反应本机应用程序。但它会给出一个错误,因为在控制台中给出了下面的图像。这里我已经附加了js文件和测试文件。
错误显示在终端中
测试套件无法运行
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文件
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文件
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")
})
})发布于 2022-04-25 04:36:29
await需要和async一起走。单元测试不声明async,这就是它抛出错误的原因。
对于修复,您应该在it之后添加it,而不是在act内部添加
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")
})
})https://stackoverflow.com/questions/71994518
复制相似问题