简体   繁体   English

当 mocking 使用 Typescript 在 Jest 中使用 class 时引发“缺少分号”错误

[英]“Missing semicolon” error raised when mocking a class in Jest using Typescript

I'm using Typescript and trying to test my code with Jest, and I made a type casting on a class so I can mock it.我正在使用 Typescript 并尝试使用 Jest 测试我的代码,并且我在 class 上进行了类型转换,因此我可以模拟它。

Unfortunately, when I ran the test suite I got the following error:不幸的是,当我运行测试套件时,出现以下错误:

 SyntaxError: C:\Projects\sim_editor\server\src\tests\routes.test.ts: Missing semicolon (7:37)

Here is my code:这是我的代码:

describe('Unit test api routes', () => {
    jest.mock('../controllers')
    const mkControllers = Controllers as jest.MockedClass<typeof Controllers>

    beforeEach(() => {
        mkControllers.mockClear()
    })

    .
    .
    .
    The rest of my test suite
 })

The error was referring the line where I declared "mkControllers".错误是指我声明“mkControllers”的行。

Here is a deeper log of the error:这是更深入的错误日志:

at Parser._raise (node_modules/@babel/parser/src/parser/error.js:97:45)
  at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:92:17)
  at Parser.raise (node_modules/@babel/parser/src/parser/error.js:41:17)
  at Parser.semicolon (node_modules/@babel/parser/src/parser/util.js:131:10)
  at Parser.parseVarStatement (node_modules/@babel/parser/src/parser/statement.js:707:10)
  at Parser.parseStatementContent (node_modules/@babel/parser/src/parser/statement.js:223:21)
  at Parser.parseStatement (node_modules/@babel/parser/src/parser/statement.js:163:17)
  at Parser.parseBlockOrModuleBlockBody (node_modules/@babel/parser/src/parser/statement.js:880:25)
  at Parser.parseBlockBody (node_modules/@babel/parser/src/parser/statement.js:856:10)
  at Parser.parseBlock (node_modules/@babel/parser/src/parser/statement.js:826:10)

Thanks.谢谢。

Apparently I didn't configure Babel to work with Typescript, as it is essential for Jest to use it.显然我没有将 Babel 配置为与 Typescript 一起使用,因为 Jest 必须使用它。 Make sure you follow the instructions here , under "Using Babel" and "Using Typescript"确保按照“使用 Babel”和“使用 Typescript”下的说明进行操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM