1

The code I am trying to test the driver / repository for my nodeJS project:

import { Injectable, OnModuleInit } from '@nestjs/common';
import { mapping, types } from 'cassandra-driver';
import { Products } from './poducts.model';
import { CassandraService } from '../database/cassandra/cassandra.service';
import Uuid = types.Uuid;

@Injectable()
export class ProductsRepository implements OnModuleInit {
  constructor(private cassandraService: CassandraService) {}

  productsMapper: mapping.ModelMapper<Products>;

  onModuleInit() {
    const mappingOptions: mapping.MappingOptions = {
      models: {
        Products: {
          tables: ['products'],
          mappings: new mapping.UnderscoreCqlToCamelCaseMappings(),
        },
      },
    };

    this.productsMapper = this.cassandraService
      .createMapper(mappingOptions)
      .forModel('Products');
  }

  async getProducts() {
    return (await this.productsMapper.findAll()).toArray(); // <-----Breaks here with findAll()
  }
}

I am trying to write something like this:

  describe('product repository get all', () => {
    it('calls the repository get all', async () => {
      const await productsRepository.getProducts();
      expect().DoSomething()
    });
  });

This is the error I am getting:

Cannot read property 'findAll' of undefined

How would I accomplish a meaning-full test with Jest to get proper code coverage?

When I try to use jest to spy on the this.products.Mapper.findAll() it seems to break every time.

Ry10p
  • 312
  • 1
  • 2
  • 11

0 Answers0