0

I've been trying to test my classes with Jest without success.

In the example, I have a method with a parameter, when I try it on playgroung it responds satisfactorily but in the tests it is giving me problems, I have seen some examples but they have not helped me so much. There is also no documentation or examples with the library.

@Resolver()
export class ProductoResolver {

  private productoServices: ProductoContract
  constructor(productoImpl: ProductoContract = new ProductoServices()){ this.productoServices  = productoImpl; }



  @Query(() => Producto, { description : 'Obtiene un producto especifico' })
  async getProductoById ( @Arg("productoId", { nullable: false }) productoId?: number ) {
    try {
      const data = await this.productoServices.obtenerProductoEspecifico(productoId);
      return data;
    } catch (err) {
      logger.error(err);
      throw new Error(err);
    }
  }
}

I have an n-layer application with the layers: application (resolving classes), domain and architecture.

I tried with these two ways:

    it('tipo 1', async ()=> {

        schema = await buildSchema({
            resolvers: [ProductoResolver],
            validate: true,
        });

        const query = `          
        query{
            getProductoById(productoId: 500){
              codigo
              descripcion
              monedaoperacion
            }
          }`;

        const result = await graphql(schema, query);
        console.log('result: ', result);
        expect(result).toBeDefined();

    });

    it('tipo 2 ', async () => {
        const functions = new ProductoResolver();
        const data = await functions.getProductoById(500);
        console.log('tipo 2: ',data);
        expect(data).toBeDefined();

    });

and but I have these errors.

error jest

Could you help me?

1 Answers1

1

You need to create a db connection before running your test.

beforeAll(async () => {
   await createConnection()
})
Michał Lytek
  • 10,577
  • 3
  • 16
  • 24
benawad
  • 685
  • 7
  • 17