#CQRS V11 breaking tests because of type reflection

18 messages · Page 1 of 1 (latest)

weak bobcat
#

Hello, we're in the process of updating to nest V11, however,

I have tests that fail because of a TypeError when manually registering command handlers via the .register() method of CommandBus.

It now takes an InstanceWrapper, instead of previously a CommandHandlerType[]

What would be the proper way to fix that type issue in the tests ?

Thank you

wheat dustBOT
#

Please run the command npx -y @nestjs/cli info and paste the output in a code block. This will help us determine if there is a version issue in your packages and which version of nest we are triaging.

weak bobcat
#

[System Information]
OS Version : macOS24.3.0
NodeJS Version : v20.16.0
YARN Version : 4.5.0

[Nest CLI]
Nest CLI Version : 11.0.2

[Nest Platform Information
platform-express version : 11.0.7
elasticsearch version : 11.0.0
event-emitter version : 3.0.0
schematics version : 11.0.0
passport version : 11.0.5
schedule version : 5.0.1
terminus version : 11.0.0
swagger version : 11.0.3
typeorm version : 11.0.0
testing version : 11.0.7
common version : 11.0.7
config version : 4.0.0
bull version : 11.0.2
core version : 11.0.7
cqrs version : 11.0.1

weak bobcat
#

Any news ?

misty wind
#

so this change can cause type errors in tests where command handlers are manually registered

#

to address this issue, you can utilize the ModuleRef to resolve your command handlers and then register them with the CommandBus

#

i mean, first, you have to ensure that your testing module includes the necessary command handlers and that you have access to ModuleRef

#
import { Test, TestingModule } from '@nestjs/testing';
import { ModuleRef } from '@nestjs/core';
import { CommandBus } from '@nestjs/cqrs';
import { YourCommandHandler } from 'path-to-your-command-handler';

describe('Your Test Suite', () => {
  let moduleRef: ModuleRef;
  let commandBus: CommandBus;

  beforeAll(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [YourCommandHandler, CommandBus],
    }).compile();

    moduleRef = module.get<ModuleRef>(ModuleRef);
    commandBus = module.get<CommandBus>(CommandBus);
  });

  // Your tests here
});
#

also, after resolving the handlers, register them with the CommandBus using the register method

#
beforeAll(async () => {
  // ... previous setup code

  const handlerInstance = await moduleRef.resolve(YourCommandHandler);
  commandBus.register([
    {
      instance: handlerInstance,
      metatype: YourCommandHandler,
      isDependencyTreeStatic: () => true,
    },
  ]);
});
#

you know, in this setup, isDependencyTreeStatic is a method that should return true if the handler's dependency tree is static

#

this approach ensures that the CommandBus is aware of your command handlers during testing

weak bobcat
#

Hello @misty wind you've been of a tremendous help it's now fixed on our side, thank you and have a nice day 🙂

wheat dustBOT
#

This post has been marked as resolved. ✅
Please read through the conversation and resolution, if you are having the same issue.
If you were the original author of the post and the issue is still fresh (within a few days) and you are still have having trouble, continue to reply here. If you are not the original author of the post or the post has aged, start a new thread linking this one as relevant to your problem, providing as much additional information as possible.

misty wind
#

Plz feel free ping me if you need more help

spare anchor
#

Hi guys anyone have problems with cqrs ( command part) on nest11 ?
On my side it seems that if u provide the module it dosen'twork.
The strange part it's that query are working but command not