Skip to content

Commit ff26830

Browse files
Add recitation 5: testing
1 parent 1d8fc69 commit ff26830

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

docs/_data/schedule.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@
371371
link: ''
372372
name: ''
373373
recitation:
374-
handout: ''
374+
handout: 'recitations/reci5-testing'
375375
name: 'Unit testing'
376376
quiz: ''
377377
slides: ''

docs/recitations/reci5-testing.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Recitation #3
2+
### 17-356/17-766
3+
4+
## Unit Testing
5+
- Testing individual components or units of code in isolation.
6+
- A unit can be a function, method, or class.
7+
- Why Unit Testing?
8+
- Easy to define scope and logic.
9+
- Catches bugs early in the development cycle.
10+
- Facilitates refactoring and maintenance.
11+
12+
## Jest
13+
- JavaScript testing framework developed by Facebook
14+
- Great for testing both backend and frontend
15+
- Built-in assertion libraries and mocking functionalities
16+
- Easy to use!
17+
18+
## Why Test Everything? Even Simple Logic
19+
- Future-Proofing
20+
- Refactoring Safety
21+
- Edge Cases
22+
- Different data type: JSON, null, None, undefined, empty strings, or unexpected objects.
23+
- Unpredictable behaviors: clicking button 1000 times, rapid API calls
24+
- Boundary Conditions: integer overflow
25+
- Concurrency Issues
26+
27+
## Async Functions
28+
- Always use await and tools like waitFor to test async functions.
29+
- Example: handle API calls or delayed updates
30+
31+
## Frontend Unit Test
32+
- Interact with component: (e.g., Click, Submit forms)
33+
- Styling and Layout
34+
- Write test for styling and properties if it is critical to functionality (e.g., a disabled button color).
35+
- Avoid testing purely visual aspects; use visual regression tools instead.
36+
- Test Error code as well
37+
38+
## TDD: Worth the Hype or Overkill
39+
- Pros
40+
- Ensures testable design.
41+
- Prevents engineers from deleting failing tests when bugs arise.
42+
- Cons
43+
- Can slow down development, especially in fast-paced startups.
44+
- May feel restrictive for exploratory or prototype-driven projects.
45+
- Solution
46+
- Use TDD for critical components.
47+
- Avoid focusing too much on testing coding style.
48+
- Use AI tools to generate code based on test cases?
49+
50+
## How Do We Know We Test Everything?
51+
- Black Box Testing
52+
- Focuses on all the possible input and output without knowing internal logic.
53+
- Tests functionality from the user’s perspective.
54+
- White Box Testing
55+
- Tests all internal structures and code.
56+
- Ensures all paths, branches, and conditions are covered
57+
58+
## How to Test Randomness
59+
- Use a fixed seed to make random behavior deterministic.
60+
- Test Range or Distribution.
61+
- Verify that outputs fall within expected ranges.
62+
- Mock Randomness function.
63+
- Run several times, and make sure there’s no pattern between them
64+
65+
## Mocking: Isolating Your Tests
66+
- Simulating the behavior of real objects or services.
67+
- Common Use Cases:
68+
- API calls.
69+
- Database queries.
70+
- Components that the output is hard to predict (e.g., time-dependent functions, randomness, AI algorithms)
71+
- Why Mock?
72+
- Speed
73+
- Reliability & Easy to debug
74+
- Simulate specific scenarios (e.g., errors, timeouts)
75+
- Avoid Extra Costs
76+
- Examples
77+
```javascript
78+
jest.mock("axios");
79+
80+
beforeEach(() => {
81+
axios.mockClear(); // Clear previous mocks before each test
82+
});
83+
84+
test("clicking (8 + 9 =) results in (17)", async () => {
85+
// Mock Axios response
86+
axios.post.mockResolvedValueOnce({
87+
data: { result: "17" }, // Mock response data
88+
});
89+
});
90+
```
91+
92+
93+

0 commit comments

Comments
 (0)