InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

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

Open peasey opened this issue 6 years ago • 6 comments

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

  • Version used: ^5.0.1
  • Environment name and version (e.g. Chrome 39, node.js 5.4): node v8.10.0
  • Operating System and version (desktop or mobile): Windows 10
  • Link to your project: https://github.com/peasey/minimal-typescript-inversify-jest

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 avatar Nov 17 '18 06:11 peasey

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.

peasey avatar Nov 17 '18 16:11 peasey

I am having the same problem in this environment:

  • Mac OSX 10.13.6
  • node 10.15.0
  • typescript 3.2.4
  • jest 23.6.0 and 24.0.0

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

stephandrab avatar Feb 01 '19 08:02 stephandrab

Also appears in jest 24.1.0 typescript 3.3.3 node 10.15.0

I cannot use toDynamicValue

OlivierAlbertini avatar Feb 09 '19 20:02 OlivierAlbertini

Same problem.

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

rus0000 avatar May 13 '19 12:05 rus0000

Same problem.

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

You are a genius.

adammfrank avatar Feb 23 '21 07:02 adammfrank

Same problem.

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

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

Theshedman avatar Jun 13 '22 02:06 Theshedman