Performance: Unit testing controllers
Unit testing controllers affects development speed and feedback loop but does not impact runtime page load or rendering performance.
describe('UserController', () => { let controller: UserController; const mockUserService = { getUser: jest.fn().mockResolvedValue({ id: '1', name: 'Test' }) }; beforeEach(() => { controller = new UserController(mockUserService as any); }); it('should return user data', async () => { const result = await controller.getUser('1'); expect(result).toEqual({ id: '1', name: 'Test' }); }); });
describe('UserController', () => { let controller: UserController; beforeEach(async () => { const module = await Test.createTestingModule({ controllers: [UserController], imports: [DatabaseModule, HttpModule], providers: [UserService], }).compile(); controller = module.get<UserController>(UserController); }); it('should return user data', async () => { const result = await controller.getUser('1'); expect(result).toBeDefined(); }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full module loading in tests | 0 | 0 | 0 | [X] Bad |
| Mocking dependencies in tests | 0 | 0 | 0 | [OK] Good |
result be?class MyController {
getHello() {
return 'Hello World!';
}
}
// Test snippet
const controller = new MyController();
const result = controller.getHello();describe('MyController', () => {
let controller: MyController;
beforeEach(() => {
controller = new MyController();
});
it('should return greeting', () => {
expect(controller.getHello()).toBe('Hello World!');
});
});Pass the test and this pattern moves unknown