【问题标题】:Jest, TypeScript: Missing semi colon error开玩笑,TypeScript:缺少分号错误
【发布时间】:2025-12-30 13:25:12
【问题描述】:

我正在使用 Jest 为我的 Angular 项目编写基本测试。但是,我收到一个奇怪的缺少分号的错误,我无法理解如何修复。

import { RegistrationService } from "../registration/services/registration.service";
describe("Email Validator function", () => {
let registrationService: RegistrationService;

test("Check Email Exists", () => {
    const input = 'donotreply@example.com';

    expect(registrationService.checkIfEmailAvailability(input)).toEqual(true);
  });
});

错误:

SyntaxError: src\app\__tests__\email-validator.spec.ts: Missing semicolon (4:25)

  2 |
  3 | describe("Email Validator function", () => {
> 4 |   let registrationService: RegistrationService;

【问题讨论】:

  • Jest 是否知道它处理的是 TypeScript,而不是 JavaScript?我的意思是,我看到了文件扩展名,但配置是否设置为处理它?
  • @T.J.Crowder 你能帮我理解如何确保它处理打字稿吗?文件扩展名为 .ts。
  • @jonrsharpe 编辑了我的问题,这有帮助吗?
  • 这样更好,但不清楚您如何配置 Jest 来运行它。它似乎将您的 TS 文件解释为 JS,假设 let name 后跟 ;(未定义的初始化变量)或 =,而不是 :

标签: angular typescript jestjs


【解决方案1】:

我按照这里的说明进行操作:https://basarat.gitbook.io/typescript/intro-1/jest

基本上我错过了那篇文章中提到的 ts-jest 和 jest.config.js。

【讨论】: