Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while using inside TestingModule (Empty Handlers) #308

Closed
tormozz48 opened this issue May 16, 2020 · 1 comment
Closed

Error while using inside TestingModule (Empty Handlers) #308

tormozz48 opened this issue May 16, 2020 · 1 comment

Comments

@tormozz48
Copy link

It is wrong behavior and initialization of CQRS inside test module which is used for my unit tests.

I have already found and written comment inside same problem


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

My current target is to verify the handler code by calling related command via command bus.
This is a simple example of my command and handler:

export class FooCommand {}

@CommandHandler(FooCommand)
export class FooHandler implements ICommandHandler<FooCommand> {
    constructor() {}

    async execute(command: FooCommand): Promise<void> {
         console.log('It works');
         return;
    }
}

Then I try to inspect it and create a unit test:

describe('foo handler', () => {
    let fooModule;    

    before(async () => {
        fooModule = await Test.createTestingModule({
            imports: [
                CqrsModule,
            ],
            controllers: [],
            providers: [
                FooHandler
            ]
        }).compile();    
   });

   it('it should works', async () => {
         const commandBus: CommandBus = fooModule.select(CqrsModule).get(CommandBus);
         await commandBus.execute(new FooCommand());
   });    
});

Now test fails with error: Error: The command handler for the "AddAttacksCommand" command was not found!

Expected behavior

I expect that the code of FooHandler execute method will be invoked and a new console record will be printed.

What is the motivation case for changing the behavior?

I also tried to use NestFactory.createApplicationContext instead of Test.createTestingModule and I received a passed test.

So there two possible issues:

  • Something in CQRS module can not be properly set up for testing modules. I did not found any possible places in sources of @nestjs/cqrs.
  • This is a bug in the testing module

Environment


Nest version: 
@nestjs/core: 7.0.1
@nestjs/testing: 7.0.11
@nestjs/common: 7.0.1
@nestjs/cqrs: 7.0.0

For Tooling issues:
- Node version: v12.14.1
- Platform:  Mac

@kamilmysliwiec
Copy link
Member

Add await fooModule.init() in the before hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants