簡體   English   中英

當 mocking 使用 Typescript 在 Jest 中使用 class 時引發“缺少分號”錯誤

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

我正在使用 Typescript 並嘗試使用 Jest 測試我的代碼,並且我在 class 上進行了類型轉換,因此我可以模擬它。

不幸的是,當我運行測試套件時,出現以下錯誤:

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

這是我的代碼:

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
 })

錯誤是指我聲明“mkControllers”的行。

這是更深入的錯誤日志:

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)

謝謝。

顯然我沒有將 Babel 配置為與 Typescript 一起使用,因為 Jest 必須使用它。 確保按照“使用 Babel”和“使用 Typescript”下的說明進行操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM