简体   繁体   English

如何在Mocha中使用Inversify

[英]How to use Inversify with Mocha

I'm using Inversify for Dependency Injection in my TypeScript Node.js application. 我在TypeScript Node.js应用程序中使用Inversify进行依赖项注入。 This works great when actually running the app. 实际运行应用程序时,这非常有用。

However, the @injectable() annotation seems to have crippled my tests. 但是, @injectable()注释似乎使我的测试瘫痪了。 When running tests, I now get an error saying: 运行测试时,我现在收到一条错误消息:

TypeError: Reflect.hasOwnMetadata is not a function

I've seen this before, and was helped by this other SO answer: https://stackoverflow.com/a/43115660/4003671 我以前看过这个,并得到了其他SO答案的帮助: https : //stackoverflow.com/a/43115660/4003671

However, importing inversify.config does not solve the problem in my tests. 但是,导入inversify.config不能解决我的测试中的问题。 If I remove the @injectable() annotation from the class I'm testing, then the tests run. 如果我从正在测试的类中删除@injectable()批注,则将运行测试。

I don't actually need DI in my tests, so somehow disabling it would be an acceptable solution. 我实际上在测试中不需要DI,因此以某种方式禁用它是可以接受的解决方案。

Has anyone seen this before? 谁看过这个吗?

Think I've got it. 想我知道了。 Importing my entire inversify.config didn't work, but if I specifically import "reflect-metadata" at the top of my unit test, it's all dandy. 导入我的整个inversify.config无效,但是如果我在单元测试的顶部专门import "reflect-metadata" ,那就太花哨了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM