首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在将Jest与TypeScript集成时修复"Jest遇到意外令牌“?

如何在将Jest与TypeScript集成时修复"Jest遇到意外令牌“?
EN

Stack Overflow用户
提问于 2021-12-17 17:20:01
回答 1查看 573关注 0票数 0

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

参考文献:

这就是我迄今尝试过的:

  • 安装了以下软件包:

代码语言:javascript
运行
复制
- `npm install --save-dev jest`
- `npm install --save-dev @types/jest`
- `npm install --save-dev ts-jest`

  • 我的jest.config.ts文件

module.exports ={^.+\.tsx?$:"ts-jest",},testMatch:[“**/?(*)+(规范测试).jts?(X)”],testPathIgnorePatterns:"/lib/",“/node_node/”,moduleFileExtensions:"ts","tsx","js","jsx",“jsx”,"json","node",collectCoverage: true,root:"/src",//通知jest根dir,coverageThreshold:{全局:{分支: 80,函数: 80,行: 80,语句: 80,};

  • 我的package.json文件

“脚本”:{“测试”:“开玩笑-覆盖-颜色”},

  • 我的示例测试文件

描述(“healthService”,() => { let healthService: HealthService;beforeEach(异步() => {HealthService=新HealthService ();});它(“成功”,异步() => { const mockHealthCheckModel: HealthModel ={ dateTime: new Date() ),描述:“健康检查”,状态:“连接”,} const output = await healthService.healthCheck();预期(输出).toEqual(MockHealthCheckModel);};});

EN

Stack Overflow用户

发布于 2021-12-20 16:26:28

在花了一段时间之后,我发现如果我在我的spec.ts中添加了spec.ts,那么它就解决了这个问题。

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

即:

AR:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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);
    });
});
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70396713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档