I'm auto mocking the provider that gets injected into the testing module (PaymentProvider), but when I run a test the test fails by saying a ConfigService cannot find an env variable. The config service is a dependency of the PaymentProviderAdapter which is in turn a dependency of PaymentProvider. So think something like this: new PaymentProvider(new PaymentProviderAdapter(new ConfigService))). I thought that calling jest.mock("@payment-providers/providers/some.provider") would auto-mock all the internals, but it seems like it's still calling the actual adapter, ergo the real config service.
side note: if i include jest.mock("@payment-providers/adapters/some.adapter") -- mocking the adapter module, the tests pass. But i do not see why this should be necessary.
import { Test } from '@nestjs/testing';
import { CustomersModule } from '@customers/customers.module';
import { LoggingModule } from '@logging/logging.module';
import { PaymentProvider } from '@payment-providers/providers/some.provider';
import { CheckoutService } from '../checkout.service';
import type { TestingModule } from '@nestjs/testing';
jest.mock("@payment-providers/providers/some.provider")
jest.mock("@customers/services/customers.service", () => ({
// eslint-disable-next-line @typescript-eslint/naming-convention
CustomersService: jest.fn().mockImplementation(() => ({
getCustomerByAccountId: jest.fn().mockResolvedValue({
id: "customer_id_MOCK",
})
}))
}))
describe('CheckoutService', () => {
let service: CheckoutService;
let paymentProvider: PaymentProvider;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [CustomersModule, LoggingModule],
providers: [
CheckoutService,
PaymentProvider, // <-- This seems to be the issue
],
}).compile();
service = module.get<CheckoutService>(CheckoutService);
paymentProvider = module.get(PaymentProvider)
});