Nest: CQRS test, CommandHandlerNotFoundException

Created on 24 Feb 2020  路  3Comments  路  Source: nestjs/nest

Running the server and testing it with the postman works fine,
but the test case gives the following error:

error message

Failed: CommandHandlerNotFoundException {
      "message": "CommandHandler not found exception!",
    }

auth.service.spec.ts

import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
import { AuthModule } from '../auth.module';

describe('AuthService', () => {
  let service: AuthService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [AuthModule],
    }).compile();

    service = module.get<AuthService>(AuthService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  describe('login', () => {
    it('should return login data when email and password correct', async () => {
      const email = '[email protected]';
      const password = 'test1234';
      const result = await service.login({ email, password });
    });
  });
});

auth.module.ts

import { Module, HttpModule } from '@nestjs/common';

import { CqrsModule } from '@nestjs/cqrs';

import { AuthController } from './controllers/auth.controller';
import { AuthService } from './services/auth.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import httpConfigProvider from '../app/config/http-config.provider';
import { CommandHandlers } from './commands/handler';
import { AuthApiRepository } from './repository/auth-api.repository';

@Module({
  imports: [
    HttpModule.registerAsync({
      imports: [ConfigModule],
      useFactory: httpConfigProvider,
      inject: [ConfigService],
    }),
    CqrsModule,
  ],
  controllers: [AuthController],
  providers: [AuthService, ...CommandHandlers, AuthApiRepository],
})
export class AuthModule {}

needs triage question 馃檶

All 3 comments

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

Even though this has already been closed. Just had the same problem. The solution seems to be to start the application as described in the docs. CQRS events, commands, etc. are registered in the onApplicationBootstrap() lifecycle hook of the CqrsModule.
Maybe this should be pointed out in the documentation.

I also had this issue during an update of nestjs and the final solution was calling await app.init();, even in my regular bootstrap function

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BrunnerLivio picture BrunnerLivio  路  40Comments

MonsieurMan picture MonsieurMan  路  28Comments

ZenSoftware picture ZenSoftware  路  35Comments

hypeofpipe picture hypeofpipe  路  27Comments

raydirty picture raydirty  路  37Comments