我正在尝试将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 ={^.+\.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);};});
发布于 2021-12-20 16:26:28
在花了一段时间之后,我发现如果我在我的spec.ts
中添加了spec.ts
,那么它就解决了这个问题。
这意味着我必须在我的所有测试文件中添加这一行。我试图找出是否有一种方法可以在一个位置配置它,而不是在所有测试文件上改进它。一旦我搞清楚了,我会更新这个答案的。
即:
AR:
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);
});
});
https://stackoverflow.com/questions/70396713
复制相似问题