inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.33k stars 719 forks source link

TypeScript, InversifyJS and Jest: TypeError: Cannot read property 'constructor' of null. #997

Open peasey opened 6 years ago

peasey commented 6 years ago

When using InversifyJS with Jest, creating a container with mock dependencies results in the error:

TypeError: Cannot read property 'constructor' of null.

Expected Behavior

A container should be able to handle mock depenencies and resolve them without errors.

Current Behavior

A container cannot handle mock depenencies and resolve them without errors.

Possible Solution

Seems related to this issue: https://github.com/inversify/InversifyJS/issues/984

Steps to Reproduce (for bugs)

I've created a minimal example that reproduces the issue: https://github.com/peasey/minimal-typescript-inversify-jest

Context

I cant use InversifyJS and Jest together, so unless this can be fixed I'll need to find another DI container or test framework, neither of which are ideal.

Your Environment

Stack trace

TypeError: Cannot read property 'constructor' of null

  at getClassPropsAsTargets (node_modules/inversify/lib/planning/reflection_utils.js:82:75)
      at Array.forEach (<anonymous>)
peasey commented 6 years ago

I've been able to get this working by binding with toDynamicValue instead. I've updated the minimal example on a new branch: https://github.com/peasey/minimal-typescript-inversify-jest/tree/using-dynamic-value

Leaving the issue open for now.

stephandrab commented 5 years ago

I am having the same problem in this environment:

^ Frankly I cannot use toDynamicValue... :-/ Do you have an idea when this may be fixed?

OlivierAlbertini commented 5 years ago

Also appears in jest 24.1.0 typescript 3.3.3 node 10.15.0

I cannot use toDynamicValue

rus0000 commented 5 years ago

Same problem.

Helped this quick hack in test file Object.getPrototypeOf(EventEmitter.prototype).constructor = Object;

adammfrank commented 3 years ago

Same problem.

  • Mint 19
  • Node 8.9.4
  • inversify@5.0.1
  • jest@24.8.0
  • typescript@3.4.5

Helped this quick hack in test file Object.getPrototypeOf(EventEmitter.prototype).constructor = Object;

You are a genius.

Theshedman commented 2 years ago

Same problem.

  • Mint 19
  • Node 8.9.4
  • inversify@5.0.1
  • jest@24.8.0
  • typescript@3.4.5

Helped this quick hack in test file Object.getPrototypeOf(EventEmitter.prototype).constructor = Object;

This really saved me from hours of debugging. Thanks a lot.