Skip to content

Commit 977493f

Browse files
committed
docs(recipe): add example for mock configuration
1 parent 6dd1bf7 commit 977493f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

content/recipes/suites.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,43 @@ describe('User Service Unit Spec', () => {
150150
`TestBed.solitary()` analyzes the constructor and creates typed mocks for all dependencies.
151151
The `Mocked<T>` type provides IntelliSense support for mock configuration.
152152

153+
#### Pre-compile mock configuration
154+
155+
Configure mock behavior before compilation using `.mock().impl()`:
156+
157+
```typescript
158+
@@filename(user.service.spec)
159+
import { TestBed } from '@suites/unit';
160+
import { UserService } from './user.service';
161+
import { UserRepository } from './user.repository';
162+
163+
describe('User Service Unit Spec - pre-configured', () => {
164+
let unit: UserService;
165+
let repository: Mocked<UserRepository>;
166+
167+
beforeAll(async () => {
168+
const { unit: underTest, unitRef } = await TestBed.solitary(UserService)
169+
.mock(UserRepository)
170+
.impl(stubFn => ({
171+
findById: stubFn().mockResolvedValue({ id: '1', email: '[email protected]', name: 'Test' })
172+
}))
173+
.compile();
174+
175+
repository = unitRef.get(UserRepository);
176+
unit = underTest;
177+
})
178+
179+
it('should find user with pre-configured mock', async () => {
180+
const result = await unit.findById('1');
181+
182+
expect(repository.findById).toHaveBeenCalled();
183+
expect(result.email).toBe('[email protected]');
184+
});
185+
});
186+
```
187+
188+
The `stubFn` parameter corresponds to the installed doubles adapter (`jest.fn()` for Jest, `vi.fn()` for Vitest, `sinon.stub()` for Sinon).
189+
153190
#### Testing with real dependencies
154191

155192
Use `TestBed.sociable()` with `.expose()` to use real implementations for specific dependencies:

0 commit comments

Comments
 (0)