You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jest+TypeScript+Next.js测试Firebase Auth遇Unexpected Token错误求助

解决Jest+TypeScript+Next.js测试Firebase Authentication的导入错误

这个错误的核心原因是:Jest默认不会转译node_modules里的文件,但Firebase现在使用的是ES模块语法(import/export),Jest无法直接解析,所以抛出了SyntaxError: Cannot use import statement outside a module。下面是一步步的修复方案:

1. 调整Jest配置,让它转译Firebase相关模块

修改你的jest.config.js,添加transformIgnorePatterns配置,告诉Jest不要跳过Firebase及其依赖的转译:

module.exports = {
  preset: 'ts-jest', // 新增:使用ts-jest预设简化配置,自动适配TS与Jest的转换
  roots: ["<rootDir>/src"],
  testMatch: [
    "**/__tests__/**/*.+(ts|tsx|js)",
    "**/?(*.)+(spec|test).+(ts|tsx|js)"
  ],
  transform: {
    "^.+\\.(ts|tsx)$": "ts-jest"
  },
  // 关键配置:让Jest转译firebase和@firebase开头的包,避免ES模块语法报错
  transformIgnorePatterns: ["node_modules/(?!firebase|@firebase)/"],
  // 确保Jest能识别所有需要的文件扩展名
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};

2. 改用Mock方式测试(单元测试最佳实践)

直接调用真实的Firebase Auth方法不是单元测试的正确思路——我们应该测试自己的业务逻辑,而不是Firebase的功能。你可以Mock Firebase的auth方法,完全隔离外部依赖:

修改你的测试文件firebase.unit.test.ts:

// 先Mock整个Firebase模块,模拟auth相关方法的行为
jest.mock("../../../common/firebase/firebase", () => ({
  auth: jest.fn(() => ({
    // Mock注册方法,返回模拟的成功结果
    createUserWithEmailAndPassword: jest.fn(() => 
      Promise.resolve({ user: { uid: "test-user-123" } })
    )
  }))
}));

import firebase from "../../../common/firebase/firebase";

test("signUp 应该正确调用Firebase注册方法并返回结果", async () => {
  // 类型断言,让TypeScript识别Mock函数的类型
  const mockCreateUser = firebase.auth().createUserWithEmailAndPassword as jest.Mock;
  
  // 执行测试逻辑
  const response = await firebase.auth().createUserWithEmailAndPassword("test@test.com", "password");
  
  // 验证Mock函数是否被正确调用
  expect(mockCreateUser).toHaveBeenCalledWith("test@test.com", "password");
  // 验证返回结果符合预期
  expect(response).toBeDefined();
  expect(response.user.uid).toBe("test-user-123");
});

3. 额外检查:确保TS配置与Jest兼容

你的tsconfig.json中module设置为esnext,这和Jest默认的CommonJS模式可以通过ts-jest预设自动兼容,所以加上preset: 'ts-jest'是关键,它会帮你处理TS到CommonJS的转换适配,避免额外的语法冲突。

完成以上步骤后,重新运行测试,应该就能解决导入错误,同时你的测试也会更符合单元测试的隔离原则,避免依赖外部服务的不稳定因素。

内容的提问来源于stack exchange,提问作者UK4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:52:29