用装饰器注入类时测试失败

时间:2018-10-03 10:00:26

标签: node.js typescript jestjs inversifyjs

这可能是我的环境问题,但是我在测试使用装饰器的注入类时遇到问题。

希望别人能看到我所缺少的东西。

注意:当我运行代码时,它运行良好。仅在运行笑话测试时才会发生该错误。我有一个完整的测试套件,可以正常工作。当我删除装饰器注释并且不再具有扩展类EventEmitter时,失败的测试通过。

我还使用ts-node运行代码。我使用ts-node ./node_modules/.bin/jest src/event-emitter-inversify/__tests__/event-emitter-inversify.test.ts

我不确定为什么测试失败并且运行成功。

我在测试中始终看到错误TypeError: Cannot read property 'constructor' of null

这是设置。

event-emitter-inversify.ts

import 'reflect-metadata';
import { EventEmitter } from 'events';
import { ContainerModule, decorate, injectable, interfaces } from 'inversify';

decorate(injectable(), EventEmitter);

@injectable()
export class EventEmitterChild extends EventEmitter {
  constructor() {
    super();
  }

  greet() {
    console.log('hello world.');
  }
}

export const EventEmitterModule = new ContainerModule(
  (bind: interfaces.Bind) => {
    bind<EventEmitterChild>('EventEmitterChild').to(EventEmitterChild).inSingletonScope();
  }
);

event-emitter-inversify-dependent.ts

import { inject, injectable } from 'inversify';
import { EventEmitterChild } from './event-emitter-inversify';

@injectable()
export class EventEmitterDependent {
  constructor(@inject('EventEmitterChild') private readonly _eventEmitterChild: EventEmitterChild) {
  }

  run() {
    this._eventEmitterChild.greet();
  }
}

run.ts(运行正常!)

import { Container } from 'inversify';

import { EventEmitterChild, EventEmitterModule } from './event-emiiter-inversify';
import { EventEmitterDependent } from './event-emitter-inversify-dependent';

const referenceContainer = new Container();

referenceContainer.load(EventEmitterModule);

const eventEmitterChild = referenceContainer.get<EventEmitterChild>('EventEmitterChild');
eventEmitterChild.greet();

const eventEmitterDependent = referenceContainer.get<EventEmitterDependent>('EventEmitterDependent');

eventEmitterDependent.run();

event-emitter-inversify.test.ts(失败)

import { Container } from 'inversify';

import { EventEmitterChild, EventEmitterModule } from '../event-emitter-inversify';

describe('EventEmitter test', () => {
  const container: Container = new Container();

  beforeAll(async () => {
    container.load(EventEmitterModule);
  });

  test('get EventEmitterChild from container', () => {
    const eventEmitterChild = container.get<EventEmitterChild>('EventEmitterChild');

    expect(eventEmitterChild).toBeDefined();
  });
});

我通过以下方式运行测试:

ts-node ./node_modules/.bin/jest src/event-emitter-inversify/__tests__/event-emitter-inversify.test.ts

它失败并显示:

TypeError: Cannot read property 'constructor' of null

堆栈跟踪

  at getClassPropsAsTargets (node_modules/inversify/lib/planning/reflection_utils.js:82:75)
  at getClassPropsAsTargets (node_modules/inversify/lib/planning/reflection_utils.js:84:27)
  at getClassPropsAsTargets (node_modules/inversify/lib/planning/reflection_utils.js:84:27)
  at getTargets (node_modules/inversify/lib/planning/reflection_utils.js:28:27)
  at Object.getDependencies (node_modules/inversify/lib/planning/reflection_utils.js:12:19)
  at node_modules/inversify/lib/planning/planner.js:106:51
      at Array.forEach (<anonymous>)
  at _createSubRequests (node_modules/inversify/lib/planning/planner.js:94:20)
  at Object.plan (node_modules/inversify/lib/planning/planner.js:136:9)
  at node_modules/inversify/lib/container/container.js:318:37

关于我丢失或做错什么的任何想法吗?

0 个答案:

没有答案