【问题标题】:How to fix "Jest encountered an unexpected token" while integrating Jest with TypeScript?如何在将 Jest 与 TypeScript 集成时修复“Jest 遇到意外令牌”?
【发布时间】:2022-01-20 14:07:24
【问题描述】:

我正在尝试将 Jest 与我的 TypeScipt 项目集成,但收到 TypeError: Reflect.hasOwnMetadata is not a function 错误。

参考资料:

这是我迄今为止尝试过的:

  • 安装了以下软件包:

    • npm install --save-dev jest
    • npm install --save-dev @types/jest
    • npm install --save-dev ts-jest
  • 我的jest.config.ts 文件

    module.exports = {
      transform: {
        "^.+\\.tsx?$": "ts-jest",
      },
      testMatch: ["**/?(*.)+(spec|test).[jt]s?(x)"],
      testPathIgnorePatterns: ["/lib/", "/node_modules/"],
      moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
      collectCoverage: true,
      roots: [
        "<rootDir>/src", // informs jest of the root dir
      ],
      coverageThreshold: {
        global: {
          branches: 80,
          functions: 80,
          lines: 80,
          statements: 80,
        },
      },
    };
    
  • 我的package.json 文件

    "scripts": {
        "test": "jest --coverage --color"
      },
    
  • 我的示例测试文件

    describe("HealthService", () => {
        let healthService: HealthService;
    
        beforeEach(async () => {
            healthService = new HealthService();
        });
    
        it("Success", async () => {
            const mockHealthCheckModel: HealthModel = {
                dateTime: new Date(),
                description: "Health Check",
                status: "Connected",
            }
            const output = await healthService.healthCheck();
            expect(output).toEqual(mockHealthCheckModel);
    
        });
    });
    

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    花了一段时间后,我发现如果我在我的spec.ts 中添加import 'reflect-metadata'; 就可以解决问题。

    现在这意味着我必须在我的所有测试文件中添加这一行。我正在尝试查找是否有一种方法可以在一个位置进行配置,而不是在所有测试文件中进行改进。一旦我弄清楚了,我会更新这个答案。

    即:

    增强现实:

    import { HealthModel } from "../../models/health.model";
    import HealthService from "../health.service";
    
    describe("HealthService", () => {
        let healthService: HealthService;
    
        beforeEach(async () => {
            healthService = new HealthService();
        });
    
        it("Success", async () => {
            const mockHealthCheckModel: HealthModel = {
                dateTime: new Date(),
                description: "Health Check",
                status: "Connected",
            }
            const output = await healthService.healthCheck();
            expect(output).toEqual(mockHealthCheckModel);
        });
    });
    

    ER:

    import 'reflect-metadata';
    import { HealthModel } from "../../models/health.model";
    import HealthService from "../health.service";
    
    describe("HealthService", () => {
        let healthService: HealthService;
    
        beforeEach(async () => {
            healthService = new HealthService();
        });
    
        it("Success", async () => {
            const mockHealthCheckModel: HealthModel = {
                dateTime: new Date(),
                description: "Health Check",
                status: "Connected",
            }
            const output = await healthService.healthCheck();
            expect(output).toEqual(mockHealthCheckModel);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-19
      • 2019-12-29
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多