create-huenei-template 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/templates.js +3 -3
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/dist/templates/frontend/angular-base/package.json +0 -1
- package/dist/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/package.json +1 -1
- package/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/frontend/angular-base/package.json +0 -1
- package/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/templates/frontend/angular-base/package-lock.json +0 -13321
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: angular-testing
|
|
3
|
+
description: Write unit and integration tests for Angular v21+ applications using Vitest or Jasmine with TestBed, component harnesses, and modern testing patterns. Use for testing components with signals, OnPush change detection, services with inject(), and HTTP interactions. Triggers on test creation, testing signal-based components, mocking dependencies, or setting up test infrastructure.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Angular Testing
|
|
7
|
+
|
|
8
|
+
Test Angular v21+ applications with Vitest (recommended) or Jasmine, focusing on signal-based components and modern patterns.
|
|
9
|
+
|
|
10
|
+
## Vitest Setup (Angular v20+)
|
|
11
|
+
|
|
12
|
+
Angular v20+ has native Vitest support through the `@angular/build` package.
|
|
13
|
+
|
|
14
|
+
### Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install -D vitest jsdom
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Configuration
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
// angular.json - update test architect
|
|
24
|
+
{
|
|
25
|
+
"projects": {
|
|
26
|
+
"your-app": {
|
|
27
|
+
"architect": {
|
|
28
|
+
"test": {
|
|
29
|
+
"builder": "@angular/build:unit-test",
|
|
30
|
+
"options": {
|
|
31
|
+
"tsConfig": "tsconfig.spec.json",
|
|
32
|
+
"buildTarget": "your-app:build"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
// tsconfig.spec.json
|
|
43
|
+
{
|
|
44
|
+
"extends": "./tsconfig.json",
|
|
45
|
+
"compilerOptions": {
|
|
46
|
+
"types": ["vitest/globals"]
|
|
47
|
+
},
|
|
48
|
+
"include": ["src/**/*.spec.ts"]
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Running Tests
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Run tests
|
|
56
|
+
ng test
|
|
57
|
+
|
|
58
|
+
# Watch mode
|
|
59
|
+
ng test --watch
|
|
60
|
+
|
|
61
|
+
# Coverage
|
|
62
|
+
ng test --code-coverage
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Vitest Test Example
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
69
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
70
|
+
import { Counter } from './counter.component';
|
|
71
|
+
|
|
72
|
+
describe('Counter', () => {
|
|
73
|
+
let component: Counter;
|
|
74
|
+
let fixture: ComponentFixture<Counter>;
|
|
75
|
+
|
|
76
|
+
beforeEach(async () => {
|
|
77
|
+
await TestBed.configureTestingModule({
|
|
78
|
+
imports: [Counter],
|
|
79
|
+
}).compileComponents();
|
|
80
|
+
|
|
81
|
+
fixture = TestBed.createComponent(Counter);
|
|
82
|
+
component = fixture.componentInstance;
|
|
83
|
+
fixture.detectChanges();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should create', () => {
|
|
87
|
+
expect(component).toBeTruthy();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should increment count', () => {
|
|
91
|
+
expect(component.count()).toBe(0);
|
|
92
|
+
|
|
93
|
+
component.increment();
|
|
94
|
+
|
|
95
|
+
expect(component.count()).toBe(1);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Vitest Mocking
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
104
|
+
|
|
105
|
+
describe('UserCmpt', () => {
|
|
106
|
+
const mockUserService = {
|
|
107
|
+
getUser: vi.fn(),
|
|
108
|
+
updateUser: vi.fn(),
|
|
109
|
+
user: signal<User | null>(null),
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
beforeEach(async () => {
|
|
113
|
+
vi.clearAllMocks();
|
|
114
|
+
mockUserService.getUser.mockReturnValue(of({ id: '1', name: 'Test' }));
|
|
115
|
+
|
|
116
|
+
await TestBed.configureTestingModule({
|
|
117
|
+
imports: [UserCmpt],
|
|
118
|
+
providers: [
|
|
119
|
+
{ provide: User, useValue: mockUserService },
|
|
120
|
+
],
|
|
121
|
+
}).compileComponents();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should call getUser on init', () => {
|
|
125
|
+
const fixture = TestBed.createComponent(UserCmpt);
|
|
126
|
+
fixture.detectChanges();
|
|
127
|
+
|
|
128
|
+
expect(mockUserService.getUser).toHaveBeenCalledWith('1');
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Vitest with HTTP Testing
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
137
|
+
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
|
|
138
|
+
import { provideHttpClient } from '@angular/common/http';
|
|
139
|
+
|
|
140
|
+
describe('User', () => {
|
|
141
|
+
let service: User;
|
|
142
|
+
let httpMock: HttpTestingController;
|
|
143
|
+
|
|
144
|
+
beforeEach(() => {
|
|
145
|
+
TestBed.configureTestingModule({
|
|
146
|
+
providers: [
|
|
147
|
+
provideHttpClient(),
|
|
148
|
+
provideHttpClientTesting(),
|
|
149
|
+
],
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
service = TestBed.inject(User);
|
|
153
|
+
httpMock = TestBed.inject(HttpTestingController);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
afterEach(() => {
|
|
157
|
+
httpMock.verify();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should fetch user', () => {
|
|
161
|
+
const mockUser = { id: '1', name: 'Test User' };
|
|
162
|
+
|
|
163
|
+
service.getUser('1').subscribe(user => {
|
|
164
|
+
expect(user).toEqual(mockUser);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const req = httpMock.expectOne('/api/users/1');
|
|
168
|
+
expect(req.request.method).toBe('GET');
|
|
169
|
+
req.flush(mockUser);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## Basic Component Test
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
180
|
+
import { Counter } from './counter.component';
|
|
181
|
+
|
|
182
|
+
describe('Counter', () => {
|
|
183
|
+
let component: Counter;
|
|
184
|
+
let fixture: ComponentFixture<Counter>;
|
|
185
|
+
|
|
186
|
+
beforeEach(async () => {
|
|
187
|
+
await TestBed.configureTestingModule({
|
|
188
|
+
imports: [Counter], // Standalone component
|
|
189
|
+
}).compileComponents();
|
|
190
|
+
|
|
191
|
+
fixture = TestBed.createComponent(Counter);
|
|
192
|
+
component = fixture.componentInstance;
|
|
193
|
+
fixture.detectChanges();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('should create', () => {
|
|
197
|
+
expect(component).toBeTruthy();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should increment count', () => {
|
|
201
|
+
expect(component.count()).toBe(0);
|
|
202
|
+
|
|
203
|
+
component.increment();
|
|
204
|
+
|
|
205
|
+
expect(component.count()).toBe(1);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should display count in template', () => {
|
|
209
|
+
component.count.set(5);
|
|
210
|
+
fixture.detectChanges();
|
|
211
|
+
|
|
212
|
+
const element = fixture.nativeElement.querySelector('.count');
|
|
213
|
+
expect(element.textContent).toContain('5');
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Testing Signals
|
|
219
|
+
|
|
220
|
+
### Direct Signal Testing
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { signal, computed } from '@angular/core';
|
|
224
|
+
|
|
225
|
+
describe('Signal logic', () => {
|
|
226
|
+
it('should update computed when signal changes', () => {
|
|
227
|
+
const count = signal(0);
|
|
228
|
+
const doubled = computed(() => count() * 2);
|
|
229
|
+
|
|
230
|
+
expect(doubled()).toBe(0);
|
|
231
|
+
|
|
232
|
+
count.set(5);
|
|
233
|
+
expect(doubled()).toBe(10);
|
|
234
|
+
|
|
235
|
+
count.update(c => c + 1);
|
|
236
|
+
expect(doubled()).toBe(12);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Testing Component Signals
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
@Component({
|
|
245
|
+
selector: 'app-todo-list',
|
|
246
|
+
template: `
|
|
247
|
+
<ul>
|
|
248
|
+
@for (todo of filteredTodos(); track todo.id) {
|
|
249
|
+
<li>{{ todo.text }}</li>
|
|
250
|
+
}
|
|
251
|
+
</ul>
|
|
252
|
+
<p>{{ remaining() }} remaining</p>
|
|
253
|
+
`,
|
|
254
|
+
})
|
|
255
|
+
export class TodoList {
|
|
256
|
+
todos = signal<Todo[]>([]);
|
|
257
|
+
filter = signal<'all' | 'active' | 'done'>('all');
|
|
258
|
+
|
|
259
|
+
filteredTodos = computed(() => {
|
|
260
|
+
const todos = this.todos();
|
|
261
|
+
switch (this.filter()) {
|
|
262
|
+
case 'active': return todos.filter(t => !t.done);
|
|
263
|
+
case 'done': return todos.filter(t => t.done);
|
|
264
|
+
default: return todos;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
remaining = computed(() => this.todos().filter(t => !t.done).length);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
describe('TodoList', () => {
|
|
272
|
+
let component: TodoList;
|
|
273
|
+
let fixture: ComponentFixture<TodoList>;
|
|
274
|
+
|
|
275
|
+
beforeEach(async () => {
|
|
276
|
+
await TestBed.configureTestingModule({
|
|
277
|
+
imports: [TodoList],
|
|
278
|
+
}).compileComponents();
|
|
279
|
+
|
|
280
|
+
fixture = TestBed.createComponent(TodoList);
|
|
281
|
+
component = fixture.componentInstance;
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('should filter active todos', () => {
|
|
285
|
+
component.todos.set([
|
|
286
|
+
{ id: '1', text: 'Task 1', done: false },
|
|
287
|
+
{ id: '2', text: 'Task 2', done: true },
|
|
288
|
+
{ id: '3', text: 'Task 3', done: false },
|
|
289
|
+
]);
|
|
290
|
+
|
|
291
|
+
component.filter.set('active');
|
|
292
|
+
|
|
293
|
+
expect(component.filteredTodos().length).toBe(2);
|
|
294
|
+
expect(component.remaining()).toBe(2);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('should render filtered todos', () => {
|
|
298
|
+
component.todos.set([
|
|
299
|
+
{ id: '1', text: 'Active Task', done: false },
|
|
300
|
+
{ id: '2', text: 'Done Task', done: true },
|
|
301
|
+
]);
|
|
302
|
+
component.filter.set('active');
|
|
303
|
+
fixture.detectChanges();
|
|
304
|
+
|
|
305
|
+
const items = fixture.nativeElement.querySelectorAll('li');
|
|
306
|
+
expect(items.length).toBe(1);
|
|
307
|
+
expect(items[0].textContent).toContain('Active Task');
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## Testing OnPush Components
|
|
313
|
+
|
|
314
|
+
OnPush components require explicit change detection:
|
|
315
|
+
|
|
316
|
+
```typescript
|
|
317
|
+
@Component({
|
|
318
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
319
|
+
template: `<span>{{ data().name }}</span>`,
|
|
320
|
+
})
|
|
321
|
+
export class OnPush {
|
|
322
|
+
data = input.required<{ name: string }>();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
describe('OnPush', () => {
|
|
326
|
+
it('should update when input signal changes', () => {
|
|
327
|
+
const fixture = TestBed.createComponent(OnPush);
|
|
328
|
+
|
|
329
|
+
// Set input using setInput (for signal inputs)
|
|
330
|
+
fixture.componentRef.setInput('data', { name: 'Initial' });
|
|
331
|
+
fixture.detectChanges();
|
|
332
|
+
|
|
333
|
+
expect(fixture.nativeElement.textContent).toContain('Initial');
|
|
334
|
+
|
|
335
|
+
// Update input
|
|
336
|
+
fixture.componentRef.setInput('data', { name: 'Updated' });
|
|
337
|
+
fixture.detectChanges();
|
|
338
|
+
|
|
339
|
+
expect(fixture.nativeElement.textContent).toContain('Updated');
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
## Testing Services
|
|
345
|
+
|
|
346
|
+
### Basic Service Test
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
import { TestBed } from '@angular/core/testing';
|
|
350
|
+
|
|
351
|
+
@Injectable({ providedIn: 'root' })
|
|
352
|
+
export class CounterSvc {
|
|
353
|
+
private _count = signal(0);
|
|
354
|
+
readonly count = this._count.asReadonly();
|
|
355
|
+
|
|
356
|
+
increment() {
|
|
357
|
+
this._count.update(c => c + 1);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
reset() {
|
|
361
|
+
this._count.set(0);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
describe('CounterSvc', () => {
|
|
366
|
+
let service: CounterSvc;
|
|
367
|
+
|
|
368
|
+
beforeEach(() => {
|
|
369
|
+
TestBed.configureTestingModule({});
|
|
370
|
+
service = TestBed.inject(CounterSvc);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it('should increment count', () => {
|
|
374
|
+
expect(service.count()).toBe(0);
|
|
375
|
+
|
|
376
|
+
service.increment();
|
|
377
|
+
expect(service.count()).toBe(1);
|
|
378
|
+
|
|
379
|
+
service.increment();
|
|
380
|
+
expect(service.count()).toBe(2);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it('should reset count', () => {
|
|
384
|
+
service.increment();
|
|
385
|
+
service.increment();
|
|
386
|
+
|
|
387
|
+
service.reset();
|
|
388
|
+
|
|
389
|
+
expect(service.count()).toBe(0);
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### Service with Dependencies
|
|
395
|
+
|
|
396
|
+
```typescript
|
|
397
|
+
@Injectable({ providedIn: 'root' })
|
|
398
|
+
export class User {
|
|
399
|
+
private http = inject(HttpClient);
|
|
400
|
+
|
|
401
|
+
getUser(id: string) {
|
|
402
|
+
return this.http.get<User>(`/api/users/${id}`);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
describe('User', () => {
|
|
407
|
+
let service: User;
|
|
408
|
+
let httpMock: HttpTestingController;
|
|
409
|
+
|
|
410
|
+
beforeEach(() => {
|
|
411
|
+
TestBed.configureTestingModule({
|
|
412
|
+
providers: [
|
|
413
|
+
provideHttpClient(),
|
|
414
|
+
provideHttpClientTesting(),
|
|
415
|
+
],
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
service = TestBed.inject(User);
|
|
419
|
+
httpMock = TestBed.inject(HttpTestingController);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
afterEach(() => {
|
|
423
|
+
httpMock.verify(); // Verify no outstanding requests
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('should fetch user by id', () => {
|
|
427
|
+
const mockUser: User = { id: '1', name: 'Test User' };
|
|
428
|
+
|
|
429
|
+
service.getUser('1').subscribe(user => {
|
|
430
|
+
expect(user).toEqual(mockUser);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
const req = httpMock.expectOne('/api/users/1');
|
|
434
|
+
expect(req.request.method).toBe('GET');
|
|
435
|
+
req.flush(mockUser);
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
## Mocking Dependencies
|
|
441
|
+
|
|
442
|
+
### Using Jasmine Spies
|
|
443
|
+
|
|
444
|
+
```typescript
|
|
445
|
+
describe('ComponentWithDependency', () => {
|
|
446
|
+
let userServiceSpy: jasmine.SpyObj<User>;
|
|
447
|
+
|
|
448
|
+
beforeEach(async () => {
|
|
449
|
+
userServiceSpy = jasmine.createSpyObj('User', ['getUser', 'updateUser']);
|
|
450
|
+
userServiceSpy.getUser.and.returnValue(of({ id: '1', name: 'Test' }));
|
|
451
|
+
|
|
452
|
+
await TestBed.configureTestingModule({
|
|
453
|
+
imports: [UserProfile],
|
|
454
|
+
providers: [
|
|
455
|
+
{ provide: User, useValue: userServiceSpy },
|
|
456
|
+
],
|
|
457
|
+
}).compileComponents();
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it('should call getUser on init', () => {
|
|
461
|
+
const fixture = TestBed.createComponent(UserProfile);
|
|
462
|
+
fixture.detectChanges();
|
|
463
|
+
|
|
464
|
+
expect(userServiceSpy.getUser).toHaveBeenCalledWith('1');
|
|
465
|
+
});
|
|
466
|
+
});
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
### Mock Signal-Based Service
|
|
470
|
+
|
|
471
|
+
```typescript
|
|
472
|
+
// Create mock with signal
|
|
473
|
+
const mockAuth = {
|
|
474
|
+
user: signal<User | null>(null),
|
|
475
|
+
isAuthenticated: computed(() => mockAuth.user() !== null),
|
|
476
|
+
login: jasmine.createSpy('login'),
|
|
477
|
+
logout: jasmine.createSpy('logout'),
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
beforeEach(async () => {
|
|
481
|
+
await TestBed.configureTestingModule({
|
|
482
|
+
imports: [Protected],
|
|
483
|
+
providers: [
|
|
484
|
+
{ provide: Auth, useValue: mockAuth },
|
|
485
|
+
],
|
|
486
|
+
}).compileComponents();
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it('should show content when authenticated', () => {
|
|
490
|
+
mockAuth.user.set({ id: '1', name: 'Test User' });
|
|
491
|
+
|
|
492
|
+
const fixture = TestBed.createComponent(Protected);
|
|
493
|
+
fixture.detectChanges();
|
|
494
|
+
|
|
495
|
+
expect(fixture.nativeElement.querySelector('.protected-content')).toBeTruthy();
|
|
496
|
+
});
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
## Testing Inputs and Outputs
|
|
500
|
+
|
|
501
|
+
```typescript
|
|
502
|
+
@Component({
|
|
503
|
+
selector: 'app-item',
|
|
504
|
+
template: `
|
|
505
|
+
<div (click)="select()">{{ item().name }}</div>
|
|
506
|
+
`,
|
|
507
|
+
})
|
|
508
|
+
export class Item {
|
|
509
|
+
item = input.required<Item>();
|
|
510
|
+
selected = output<Item>();
|
|
511
|
+
|
|
512
|
+
select() {
|
|
513
|
+
this.selected.emit(this.item());
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
describe('Item', () => {
|
|
518
|
+
it('should emit selected event on click', () => {
|
|
519
|
+
const fixture = TestBed.createComponent(Item);
|
|
520
|
+
const item: Item = { id: '1', name: 'Test Item' };
|
|
521
|
+
|
|
522
|
+
fixture.componentRef.setInput('item', item);
|
|
523
|
+
fixture.detectChanges();
|
|
524
|
+
|
|
525
|
+
// Subscribe to output
|
|
526
|
+
let emittedItem: Item | undefined;
|
|
527
|
+
fixture.componentInstance.selected.subscribe(i => emittedItem = i);
|
|
528
|
+
|
|
529
|
+
// Trigger click
|
|
530
|
+
fixture.nativeElement.querySelector('div').click();
|
|
531
|
+
|
|
532
|
+
expect(emittedItem).toEqual(item);
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
## Testing Async Operations
|
|
538
|
+
|
|
539
|
+
### Using fakeAsync
|
|
540
|
+
|
|
541
|
+
```typescript
|
|
542
|
+
import { fakeAsync, tick, flush } from '@angular/core/testing';
|
|
543
|
+
|
|
544
|
+
it('should debounce search', fakeAsync(() => {
|
|
545
|
+
const fixture = TestBed.createComponent(Search);
|
|
546
|
+
fixture.detectChanges();
|
|
547
|
+
|
|
548
|
+
// Type in search
|
|
549
|
+
fixture.componentInstance.query.set('test');
|
|
550
|
+
|
|
551
|
+
// Advance time for debounce
|
|
552
|
+
tick(300);
|
|
553
|
+
fixture.detectChanges();
|
|
554
|
+
|
|
555
|
+
expect(fixture.componentInstance.results().length).toBeGreaterThan(0);
|
|
556
|
+
|
|
557
|
+
// Flush any remaining timers
|
|
558
|
+
flush();
|
|
559
|
+
}));
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
### Using waitForAsync
|
|
563
|
+
|
|
564
|
+
```typescript
|
|
565
|
+
import { waitForAsync } from '@angular/core/testing';
|
|
566
|
+
|
|
567
|
+
it('should load data', waitForAsync(() => {
|
|
568
|
+
const fixture = TestBed.createComponent(Data);
|
|
569
|
+
fixture.detectChanges();
|
|
570
|
+
|
|
571
|
+
fixture.whenStable().then(() => {
|
|
572
|
+
fixture.detectChanges();
|
|
573
|
+
expect(fixture.componentInstance.data()).toBeDefined();
|
|
574
|
+
});
|
|
575
|
+
}));
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
## Testing HTTP Resources
|
|
579
|
+
|
|
580
|
+
```typescript
|
|
581
|
+
@Component({
|
|
582
|
+
template: `
|
|
583
|
+
@if (userResource.isLoading()) {
|
|
584
|
+
<p>Loading...</p>
|
|
585
|
+
} @else if (userResource.hasValue()) {
|
|
586
|
+
<p>{{ userResource.value().name }}</p>
|
|
587
|
+
}
|
|
588
|
+
`,
|
|
589
|
+
})
|
|
590
|
+
export class UserCmpt {
|
|
591
|
+
userId = signal('1');
|
|
592
|
+
userResource = httpResource<UserData>(() => `/api/users/${this.userId()}`);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
describe('UserCmpt', () => {
|
|
596
|
+
let httpMock: HttpTestingController;
|
|
597
|
+
|
|
598
|
+
beforeEach(async () => {
|
|
599
|
+
await TestBed.configureTestingModule({
|
|
600
|
+
imports: [UserCmpt],
|
|
601
|
+
providers: [
|
|
602
|
+
provideHttpClient(),
|
|
603
|
+
provideHttpClientTesting(),
|
|
604
|
+
],
|
|
605
|
+
}).compileComponents();
|
|
606
|
+
|
|
607
|
+
httpMock = TestBed.inject(HttpTestingController);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it('should display user name after loading', () => {
|
|
611
|
+
const fixture = TestBed.createComponent(UserCmpt);
|
|
612
|
+
fixture.detectChanges();
|
|
613
|
+
|
|
614
|
+
// Initially loading
|
|
615
|
+
expect(fixture.nativeElement.textContent).toContain('Loading');
|
|
616
|
+
|
|
617
|
+
// Respond to request
|
|
618
|
+
const req = httpMock.expectOne('/api/users/1');
|
|
619
|
+
req.flush({ id: '1', name: 'John Doe' });
|
|
620
|
+
fixture.detectChanges();
|
|
621
|
+
|
|
622
|
+
expect(fixture.nativeElement.textContent).toContain('John Doe');
|
|
623
|
+
});
|
|
624
|
+
});
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
## Vitest vs Jasmine Comparison
|
|
628
|
+
|
|
629
|
+
| Feature | Vitest | Jasmine/Karma |
|
|
630
|
+
|---------|--------|---------------|
|
|
631
|
+
| Speed | Faster (native ESM) | Slower |
|
|
632
|
+
| Watch mode | Instant feedback | Slower rebuilds |
|
|
633
|
+
| Mocking | `vi.fn()`, `vi.mock()` | `jasmine.createSpy()` |
|
|
634
|
+
| Assertions | `expect()` (Chai-style) | `expect()` (Jasmine) |
|
|
635
|
+
| UI | Built-in UI mode | Karma browser |
|
|
636
|
+
| Config | `angular.json` | `karma.conf.js` |
|
|
637
|
+
|
|
638
|
+
### Migration from Jasmine to Vitest
|
|
639
|
+
|
|
640
|
+
```typescript
|
|
641
|
+
// Jasmine
|
|
642
|
+
const spy = jasmine.createSpy('callback');
|
|
643
|
+
spy.and.returnValue('value');
|
|
644
|
+
expect(spy).toHaveBeenCalledWith('arg');
|
|
645
|
+
|
|
646
|
+
// Vitest
|
|
647
|
+
const spy = vi.fn();
|
|
648
|
+
spy.mockReturnValue('value');
|
|
649
|
+
expect(spy).toHaveBeenCalledWith('arg');
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
```typescript
|
|
653
|
+
// Jasmine
|
|
654
|
+
spyOn(service, 'method').and.returnValue(of(data));
|
|
655
|
+
|
|
656
|
+
// Vitest
|
|
657
|
+
vi.spyOn(service, 'method').mockReturnValue(of(data));
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
For advanced testing patterns, see [references/testing-patterns.md](references/testing-patterns.md).
|