简体   繁体   中英

How to use Inversify with Mocha

I'm using Inversify for Dependency Injection in my TypeScript Node.js application. This works great when actually running the app.

However, the @injectable() annotation seems to have crippled my tests. 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

However, importing inversify.config does not solve the problem in my tests. If I remove the @injectable() annotation from the class I'm testing, then the tests run.

I don't actually need DI in my tests, so somehow disabling it would be an acceptable solution.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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