【发布时间】:2022-01-20 14:07:24
【问题描述】:
我正在尝试将 Jest 与我的 TypeScipt 项目集成,但收到 TypeError: Reflect.hasOwnMetadata is not a function 错误。
参考资料:
- https://jestjs.io/docs/getting-started#using-babel
- https://javascript.plainenglish.io/beginners-guide-to-testing-jest-with-node-typescript-1f46a1b87dad
这是我迄今为止尝试过的:
-
安装了以下软件包:
npm install --save-dev jestnpm install --save-dev @types/jestnpm 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