我正在使用Typescript并试图用Jest测试我的代码,我对一个类进行了类型转换,这样我就可以模拟它。
不幸的是,当我运行测试套件时,我得到了以下错误:
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)
谢谢.
2条答案
按热度按时间cld4siwp1#
显然,我没有配置Babel来使用Typescript,因为Jest必须使用它。请确保您遵循“使用Babel”和“使用Typescript”下的说明here
xzlaal3s2#
Jest通过Babel支持TypeScript。按照以下步骤进行配置。
第1步:通过在控制台中粘贴此命令来添加依赖项。
步骤2:在项目的根目录下创建一个名为
babel.config.js
的新文件。步骤3:将此文本粘贴到
babel.config.js
文件中。