Testing a new class and constructor involves using the configService.get method.
@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',
});
}
}
Below is the 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();
});
});
Upon running the test, an error is encountered:
https://i.sstatic.net/DUZ2W.png
If you have any solutions or suggestions, please share them for further discussion.