lundi 6 mai 2019

Trying to test my nest api with jest, returns undefined

I am attempting to create a Jest test with the NestJs framework.

The test should check the controller and service however 1) The controller is undefined and 2) The service is undefined.

Here is the code in my home-cards.controller.spec.ts:

describe('HomeCardsController', () => {
    let homeCardsController: HomeCardsController;
    let homeCardsService: HomeCardsService;

    beforeEach(async () => {
        const module = await Test.createTestingModule({
            imports: [TypeOrmModule.forFeature([HomeCard])],
            controllers: [HomeCardsController],
            providers: [HomeCardsService]
        }).compile();

        homeCardsController = module.get<HomeCardsController>(HomeCardsController);
        homeCardsService = module.get<HomeCardsService>(HomeCardsService);
    });

    describe('findall', () => {
        it('should return an array of home cards', async () => {
            expect( await homeCardsController.findAll(1000)).toBeInstanceOf(Array);
        });
    });
});

errors:

New error as controller asking for user:

TypeError: Cannot read property 'clientId' of undefined

      17 |   async findAll(@Req() request): Promise<HomeCard[]> {
      18 |       const user: AuthUser = request.user;
    > 19 |       return await this.homeCardsService.findAll(user.clientId);
         |                                                       ^
      20 |   }
      21 | 
      22 |   @Get(":id")

Aucun commentaire:

Enregistrer un commentaire