EXAMPLE.test.ts
8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* Test Example File - Demonstrates various testing patterns
*
* Test Structure:
* 1. Import necessary test utilities and modules
* 2. Organize test suites with describe blocks
* 3. Write specific test cases with it blocks
* 4. Use expect for assertions
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
// ============================================================================
// Example 1: Simple Function Tests
// ============================================================================
// Function under test
function add(a: number, b: number): number {
return a + b;
}
describe('Example 1: Simple Function Tests', () => {
it('should add two positive numbers', () => {
const result = add(2, 3);
expect(result).toBe(5);
});
it('should add negative numbers', () => {
expect(add(-1, -2)).toBe(-3);
});
it('should handle zero', () => {
expect(add(0, 5)).toBe(5);
expect(add(5, 0)).toBe(5);
});
});
// ============================================================================
// Example 2: Object and Array Tests
// ============================================================================
function createUser(name: string, age: number) {
return { name, age, createdAt: new Date() };
}
describe('Example 2: Object Tests', () => {
it('should create user object with correct properties', () => {
const user = createUser('Alice', 25);
expect(user).toHaveProperty('name', 'Alice');
expect(user).toHaveProperty('age', 25);
expect(user).toHaveProperty('createdAt');
expect(user.createdAt).toBeInstanceOf(Date);
});
it('should create different user objects', () => {
const user1 = createUser('Bob', 30);
const user2 = createUser('Charlie', 35);
expect(user1).not.toEqual(user2);
});
});
// ============================================================================
// Example 3: Async Function Tests
// ============================================================================
async function fetchData(id: number): Promise<{ id: number; data: string }> {
// Simulate async operation
await new Promise(resolve => setTimeout(resolve, 10));
return { id, data: `Data for ${id}` };
}
describe('Example 3: Async Function Tests', () => {
it('should fetch data asynchronously', async () => {
const result = await fetchData(1);
expect(result).toEqual({
id: 1,
data: 'Data for 1',
});
});
it('should handle multiple async calls', async () => {
const results = await Promise.all([fetchData(1), fetchData(2), fetchData(3)]);
expect(results).toHaveLength(3);
expect(results[0]?.id).toBe(1);
expect(results[2]?.id).toBe(3);
});
});
// ============================================================================
// Example 4: Error Handling Tests
// ============================================================================
function divide(a: number, b: number): number {
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
}
describe('Example 4: Error Handling Tests', () => {
it('should divide numbers correctly', () => {
expect(divide(10, 2)).toBe(5);
});
it('should throw error when dividing by zero', () => {
expect(() => divide(10, 0)).toThrow('Division by zero');
});
it('should throw error of specific type', () => {
expect(() => divide(10, 0)).toThrow(Error);
});
});
// ============================================================================
// Example 5: Mock Function Tests
// ============================================================================
interface Logger {
log: (message: string) => void;
}
function processWithLogging(value: number, logger: Logger): number {
logger.log(`Processing value: ${value}`);
const result = value * 2;
logger.log(`Result: ${result}`);
return result;
}
describe('Example 5: Mock Function Tests', () => {
it('should call logger with correct messages', () => {
const mockLogger = {
log: vi.fn(),
};
const result = processWithLogging(5, mockLogger);
expect(result).toBe(10);
expect(mockLogger.log).toHaveBeenCalledTimes(2);
expect(mockLogger.log).toHaveBeenCalledWith('Processing value: 5');
expect(mockLogger.log).toHaveBeenCalledWith('Result: 10');
});
});
// ============================================================================
// Example 6: Lifecycle Hooks
// ============================================================================
class Counter {
private count = 0;
increment(): void {
this.count++;
}
getCount(): number {
return this.count;
}
reset(): void {
this.count = 0;
}
}
describe('Example 6: Lifecycle Hooks', () => {
let counter: Counter;
// Run before each test
beforeEach(() => {
counter = new Counter();
});
// Run after each test
afterEach(() => {
counter.reset();
});
it('should start with count 0', () => {
expect(counter.getCount()).toBe(0);
});
it('should increment count', () => {
counter.increment();
expect(counter.getCount()).toBe(1);
});
it('should increment multiple times', () => {
counter.increment();
counter.increment();
counter.increment();
expect(counter.getCount()).toBe(3);
});
});
// ============================================================================
// Example 7: Parameterized Tests
// ============================================================================
function validateAge(age: number): string {
if (age < 0) return 'invalid';
if (age < 18) return 'minor';
if (age < 65) return 'adult';
return 'senior';
}
describe('Example 7: Parameterized Tests', () => {
it.each([
[-1, 'invalid'],
[0, 'minor'],
[10, 'minor'],
[17, 'minor'],
[18, 'adult'],
[30, 'adult'],
[64, 'adult'],
[65, 'senior'],
[100, 'senior'],
])('should return %s for age %i', (age, expected) => {
expect(validateAge(age)).toBe(expected);
});
});
// ============================================================================
// Example 8: Array and String Matchers
// ============================================================================
describe('Example 8: Advanced Matchers', () => {
it('should test array contents', () => {
const fruits = ['apple', 'banana', 'orange'];
expect(fruits).toContain('banana');
expect(fruits).toHaveLength(3);
expect(fruits).toEqual(expect.arrayContaining(['apple', 'orange']));
});
it('should test string patterns', () => {
const email = 'test@example.com';
expect(email).toMatch(/@/);
expect(email).toMatch(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/);
});
it('should test object subset', () => {
const user = {
id: 1,
name: 'Alice',
email: 'alice@example.com',
role: 'admin',
};
expect(user).toMatchObject({
name: 'Alice',
role: 'admin',
});
});
});
// ============================================================================
// Test Naming Best Practices
// ============================================================================
/**
* Good test naming:
* ✅ should add two numbers correctly
* ✅ should throw error when input is invalid
* ✅ should return empty array when no items found
*
* Bad test naming:
* ❌ test1
* ❌ it works
* ❌ check function
*/
// ============================================================================
// Common Assertions Reference
// ============================================================================
/**
* Common assertions:
* - expect(value).toBe(expected) // Strict equality (===)
* - expect(value).toEqual(expected) // Deep equality
* - expect(value).toBeTruthy() // Truthy value
* - expect(value).toBeFalsy() // Falsy value
* - expect(value).toBeNull() // null
* - expect(value).toBeUndefined() // undefined
* - expect(value).toBeDefined() // Defined
* - expect(array).toContain(item) // Array contains
* - expect(array).toHaveLength(n) // Array length
* - expect(obj).toHaveProperty(key, value) // Object property
* - expect(fn).toThrow(error) // Throws error
* - expect(string).toMatch(pattern) // String match
* - expect(number).toBeGreaterThan(n) // Greater than
* - expect(number).toBeLessThan(n) // Less than
* - expect(value).toBeCloseTo(n, digits) // Approximate equality
*/