3

我正在尝试让笑话测试在我的项目中起作用。我是开玩笑测试的新手,这是我的第一次设置。

测试似乎通过但覆盖失败。

执行时出现以下错误yarn test

ERROR: C:\Users\elico\Documents\React workspace\pi-preperation\src\App.tsx: Unexpected token (27:20)

  25 |     switch (view) {
  26 |         case MainReducerSlice_1.ViewShown.Home:
> 27 |             return (<HomePage_1.default />);
     |                     ^
  28 |         case MainReducerSlice_1.ViewShown.Login:
  29 |         case MainReducerSlice_1.ViewShown.Register:
  30 |             return (<LoginPage_1.default />);
STACK: SyntaxError: C:\Users\elico\Documents\React workspace\pi-preperation\src\App.tsx: Unexpected token (27:20)

  25 |     switch (view) {
  26 |         case MainReducerSlice_1.ViewShown.Home:
> 27 |             return (<HomePage_1.default />);
     |                     ^
  28 |         case MainReducerSlice_1.ViewShown.Login:
  29 |         case MainReducerSlice_1.ViewShown.Register:
  30 |             return (<LoginPage_1.default />);
    at Parser.raise (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:6975:17)
    at Parser.unexpected (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:8368:16)
    at Parser.parseExprAtom (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:9627:20)
    at Parser.parseExprSubscripts (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:9210:23)
    at Parser.parseMaybeUnary (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:9190:21)
    at Parser.parseExprOps (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:9056:23)
    at Parser.parseMaybeConditional (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:9029:23)
    at Parser.parseMaybeAssign (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:8975:21)
    at Parser.parseParenAndDistinguishExpression (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:9765:28)      
    at Parser.parseExprAtom (C:\Users\elico\Documents\React workspace\pi-preperation\node_modules\@babel\parser\lib\index.js:9548:21)
Failed to collect coverage from C:\Users\elico\Documents\React workspace\pi-preperation\src\component\home-page\HomePage.tsx
ERROR: C:\Users\elico\Documents\React workspace\pi-preperation\src\component\home-page\HomePage.tsx: Unexpected token (32:12)

我究竟做错了什么?

4

1 回答 1

1

最近我有同样的问题。正如我所调查的那样,这是由我tsconfigjsx:"preserve"意思引起的,您的 TSX 将被编译为 JSX,但 Jest 不理解 JSX w/o 和预处理

compilerOptions:{
//...
    "jsx": "preserve"
}

正确的配置是

compilerOptions:{
//...
    "jsx": "react"
}
于 2020-02-27T20:41:22.877 回答