Home > Software engineering >  nestjsx-automapper profiles not found in nestjs test modules
nestjsx-automapper profiles not found in nestjs test modules

Time:11-14

Im trying to make unit testing for some features in my NestJS API.

I make use of nestjsx-automapper. The exception happens only while testing any service that uses mappings. I use @nestjs/testing and jest. I have managed to replicate the error in this test:

describe('UserController', () => {
  let userServiceInterface: UserServiceInterface;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      imports: [AutomapperModule.withMapper('user')],
      providers: [
        {
          provide: 'UserServiceInterface',
          useClass: UserService,
        },
      ],
    }).compile();

    userServiceInterface = app.get<UserServiceInterface>(
      'UserServiceInterface',
    );
  });

  describe('find', () => {
    it(`should return user with name 'pipo'`, () => {
      const user = userServiceInterface.find('pipo');
      expect(user.username).toBe('pipo');
    });
  });
});

I've been looking for different ways to configure profiles, but importing them is the only way I've found.

Thanks in advance.

CodePudding user response:

I figured out how nestjsx-automapper load profiles. As you can't provide profiles in modules, you depend on that the application runs the profiles first, and it is handled by AutomapperModule from nestjsx-automapper.

I don't know why nest Modules load AutomapperModule and consequently the profiles, but Test Modules doesn't, I will have to work more with nest to understand it.

To tell Test Modules to load AutomapperModule (as they don't by default) on init, use:

moduleRef.get(AutomapperModule).onModuleInit();
  • Related