dimanche 29 novembre 2020

Jest TypeError: configService.get is not a function

I try to test a new class and the constructor, I have a configService.get.

@Injectable()
export class StripeService {
    private stripe: Stripe;

    constructor(private configService: ConfigService) {
        const secretKey = configService.get<string>('STRIPE_SECRET') || '';
        this.stripe = new Stripe(secretKey, {
            apiVersion: '2020-08-27',
        });
    }
}

And this is my test class :

import { Test, TestingModule } from '@nestjs/testing';
import { StripeService } from './stripe.service';
import { ConfigService } from '@nestjs/config';

const mockStripe = () => ({})

describe('StripeService', () => {
  let service: StripeService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [StripeService,
      { provide: ConfigService, useFactory: mockStripe}
    ],
    }).compile();

    service = module.get<StripeService>(StripeService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});

When i try to run my test I have this error :

enter image description here

If someone have a idea of a solution, i would like to have an explanation.

Thanks,

Nicolas

Aucun commentaire:

Enregistrer un commentaire