Putting my answer here as well.
Another less manual approach would be using [Suites](https://suites.dev) for automocking your dependencies (as [described in the nestjs docs](https://docs.nestjs.com/recipes/suites)). Given your service, it will create automatic mocks for all its dependencies (it handles dependenecies like `WINSTON_MODULE_PROVIDER` as well).
```typescript
import { TestBed, type Mocked } from '@suites/unit';
describe('InstanceController', () => {
let instanceController: InstanceController; // The unit we are testing
// The dependencies we are mocking
let executor: Mocked<Executor>;
let logger: Mocked<Logger>;
let stripeService: Mocked<StripeService>;
// ...etc
beforeAll(async () => {
// Create an isolated test env for the unit
const { unit, unitRef } = await TestBed.solitary(InstanceController).compile();
instanceController = unit;
// Retrieve the unit's dependency mocks - automatically generated
executor = unitRef.get(Executor);
logger = unitRef.get(WINSTON_MODULE_PROVIDER);
stripeService = unitRef.get(StripeService);
// ...etc
});
it('should create an instance', async () => {
await instanceController.createInstance();
expect(instanceService.createInstance).toHaveBeenCalledWith(instanceFixture);
// ...etc
});
```
Another less manual approach would be using [Suites](https://suites.dev) for automocking your dependencies (as [described in the nestjs docs](https://docs.nestjs.com/recipes/suites)). Given your service, it will create automatic mocks for all its dependencies (it handles dependenecies like `WINSTON_MODULE_PROVIDER` as well).
```typescript
import { TestBed, type Mocked } from '@suites/unit';
describe('InstanceController', () => {
let instanceController: InstanceController; // The unit we are testing
// The dependencies we are mocking
let executor: Mocked<Executor>;
let logger: Mocked<Logger>;
let stripeService: Mocked<StripeService>;
// ...etc
beforeAll(async () => {
// Create an isolated test env for the unit
const { unit, unitRef } = await TestBed.solitary(InstanceController).compile();
instanceController = unit;
// Retrieve the unit's dependency mocks - automatically generated
executor = unitRef.get(Executor);
logger = unitRef.get(WINSTON_MODULE_PROVIDER);
stripeService = unitRef.get(StripeService);
// ...etc
});
it('should create an instance', async () => {
await instanceController.createInstance();
expect(instanceService.createInstance).toHaveBeenCalledWith(instanceFixture);
// ...etc
});
```