Skip to content

Commit 0d48956

Browse files
committed
refactor: switch to export-based evaluation pattern
- Replace console.log Results output with direct exports - Rewrite tests to import modules directly instead of parsing stdout - Add export instructions with 🐨 and 💰 hints in problem files - Add helpful 🚨 error messages for common mistakes - Add 5s timeout to async tests - Reduce setTimeout delays in error-handling exercise for faster tests - Fix makeRequest to return value, not just log - Fix error-handling test to verify errors don't propagate (correct behavior)
1 parent 3e52a09 commit 0d48956

File tree

87 files changed

+766
-1089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+766
-1089
lines changed

exercises/01.promises/01.problem.creation/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,5 @@ type User = {
1414
// 🐨 Call fetchUser and log the result when it resolves
1515
// 💰 fetchUser().then((user) => console.log(user))
1616

17-
// 🐨 When you're done, uncomment this:
18-
// fetchUser().then((user) => {
19-
// console.log('Results:', JSON.stringify(user))
20-
// })
17+
// 🐨 Export your function so we can verify your work
18+
// 💰 export { fetchUser }

exercises/01.promises/01.problem.creation/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.promises_01.problem.creation",
33
"type": "module",
44
"scripts": {
5-
"start": "node index.ts",
5+
"start": "npx @kentcdodds/log-module@latest ./index.ts",
66
"test": "node --test index.test.ts"
77
}
88
}
Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
import assert from 'node:assert/strict'
2-
import { execSync } from 'node:child_process'
32
import { test } from 'node:test'
3+
import * as solution from './index.ts'
44

5-
const output = execSync('npm start --silent', { encoding: 'utf8' })
6-
const jsonLine = output.split('\n').find((line) => line.startsWith('Results:'))
7-
assert.ok(jsonLine, '🚨 Missing "Results:" output line')
8-
const user = JSON.parse(jsonLine.replace('Results:', '').trim())
9-
10-
await test('fetchUser returns a Promise that resolves to a User object', async () => {
11-
type User = {
12-
id: string
13-
name: string
14-
email: string
15-
}
16-
17-
assert.ok(
18-
'id' in user,
19-
'🚨 user should have an id property - check your Promise resolve value',
20-
)
21-
assert.ok(
22-
'name' in user,
23-
'🚨 user should have a name property - check your Promise resolve value',
24-
)
5+
await test('fetchUser is exported', () => {
256
assert.ok(
26-
'email' in user,
27-
'🚨 user should have an email property - check your Promise resolve value',
28-
)
29-
assert.strictEqual(
30-
user.id,
31-
'1',
32-
'🚨 user.id should be "1" - check your Promise resolve value',
33-
)
34-
assert.strictEqual(
35-
user.name,
36-
'Alice',
37-
'🚨 user.name should be "Alice" - check your Promise resolve value',
38-
)
39-
assert.strictEqual(
40-
user.email,
41-
42-
'🚨 user.email should be "[email protected]" - check your Promise resolve value',
7+
'fetchUser' in solution,
8+
'🚨 Make sure you export "fetchUser" - add: export { fetchUser }',
439
)
4410
})
11+
12+
await test(
13+
'fetchUser returns a Promise that resolves to a User object',
14+
{ timeout: 5000 },
15+
async () => {
16+
type User = {
17+
id: string
18+
name: string
19+
email: string
20+
}
21+
22+
const user = await solution.fetchUser()
23+
24+
assert.ok(
25+
'id' in user,
26+
'🚨 user should have an id property - check your Promise resolve value',
27+
)
28+
assert.ok(
29+
'name' in user,
30+
'🚨 user should have a name property - check your Promise resolve value',
31+
)
32+
assert.ok(
33+
'email' in user,
34+
'🚨 user should have an email property - check your Promise resolve value',
35+
)
36+
assert.strictEqual(
37+
user.id,
38+
'1',
39+
'🚨 user.id should be "1" - check your Promise resolve value',
40+
)
41+
assert.strictEqual(
42+
user.name,
43+
'Alice',
44+
'🚨 user.name should be "Alice" - check your Promise resolve value',
45+
)
46+
assert.strictEqual(
47+
user.email,
48+
49+
'🚨 user.email should be "[email protected]" - check your Promise resolve value',
50+
)
51+
},
52+
)

exercises/01.promises/01.solution.creation/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ function fetchUser(): Promise<User> {
1414
name: 'Alice',
1515
1616
})
17-
}, 1000)
17+
}, 10)
1818
})
1919
}
2020

21-
void fetchUser().then((user) => {
22-
console.log('User fetched:', user)
23-
console.log('Results:', JSON.stringify(user))
24-
})
21+
export { fetchUser }

exercises/01.promises/01.solution.creation/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.promises_01.solution.creation",
33
"type": "module",
44
"scripts": {
5-
"start": "node index.ts",
5+
"start": "npx @kentcdodds/log-module@latest ./index.ts",
66
"test": "node --test index.test.ts"
77
}
88
}

exercises/01.promises/02.problem.chaining/index.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,5 @@ function fetchOrders(userId: string): Promise<Order[]> {
4848
// 🐨 Add error handling with .catch()
4949
// 💰 .catch((error) => console.error('Error:', error))
5050

51-
// 🐨 When you're done, uncomment this:
52-
// fetchUser()
53-
// .then((user) => fetchOrders(user.id).then((orders) => ({ user, orders })))
54-
// .then(({ user, orders }) => {
55-
// console.log(
56-
// 'Results:',
57-
// JSON.stringify({
58-
// user,
59-
// orders,
60-
// }),
61-
// )
62-
// })
63-
// .catch((error) => console.error('Error:', error))
51+
// 🐨 Export your functions so we can verify your work
52+
// 💰 export { fetchUser, fetchOrders }

exercises/01.promises/02.problem.chaining/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.promises_02.problem.chaining",
33
"type": "module",
44
"scripts": {
5-
"start": "node index.ts",
5+
"start": "npx @kentcdodds/log-module@latest ./index.ts",
66
"test": "node --test index.test.ts"
77
}
88
}

exercises/01.promises/02.solution.chaining/index.test.ts

Lines changed: 84 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,97 @@
11
import assert from 'node:assert/strict'
2-
import { execSync } from 'node:child_process'
32
import { test } from 'node:test'
3+
import * as solution from './index.ts'
44

5-
const output = execSync('npm start --silent', { encoding: 'utf8' })
6-
const jsonLine = output.split('\n').find((line) => line.startsWith('Results:'))
7-
assert.ok(jsonLine, '🚨 Missing "Results:" output line')
8-
const { user, orders } = JSON.parse(jsonLine.replace('Results:', '').trim())
9-
10-
await test('fetchUser resolves to a User object', async () => {
11-
assert.ok(
12-
'id' in user,
13-
'🚨 user should have an id property - make sure fetchUser resolves correctly',
14-
)
5+
await test('fetchUser and fetchOrders are exported', () => {
156
assert.ok(
16-
'name' in user,
17-
'🚨 user should have a name property - make sure fetchUser resolves correctly',
7+
'fetchUser' in solution,
8+
'🚨 Make sure you export "fetchUser" - add: export { fetchUser, fetchOrders }',
189
)
1910
assert.ok(
20-
'email' in user,
21-
'🚨 user should have an email property - make sure fetchUser resolves correctly',
22-
)
23-
assert.strictEqual(
24-
user.id,
25-
'1',
26-
'🚨 user.id should be "1" - check your Promise resolve value',
27-
)
28-
assert.strictEqual(
29-
user.name,
30-
'Alice',
31-
'🚨 user.name should be "Alice" - check your Promise resolve value',
32-
)
33-
assert.strictEqual(
34-
user.email,
35-
36-
'🚨 user.email should be "[email protected]" - check your Promise resolve value',
11+
'fetchOrders' in solution,
12+
'🚨 Make sure you export "fetchOrders" - add: export { fetchUser, fetchOrders }',
3713
)
3814
})
3915

40-
await test('fetchOrders resolves to an array of Order objects', async () => {
41-
assert.strictEqual(
42-
Array.isArray(orders),
43-
true,
44-
'🚨 orders should be an array - make sure fetchOrders returns an array',
45-
)
46-
assert.ok(
47-
orders.length > 0,
48-
'🚨 orders should have at least one item - check your Promise resolve value',
49-
)
50-
assert.ok(
51-
'id' in orders[0],
52-
'🚨 orders[0] should have an id property - check your Promise resolve value',
53-
)
54-
assert.ok(
55-
'userId' in orders[0],
56-
'🚨 orders[0] should have a userId property - check your Promise resolve value',
57-
)
58-
assert.ok(
59-
'items' in orders[0],
60-
'🚨 orders[0] should have an items property - check your Promise resolve value',
61-
)
62-
assert.ok(
63-
'total' in orders[0],
64-
'🚨 orders[0] should have a total property - check your Promise resolve value',
65-
)
66-
assert.strictEqual(
67-
orders[0].userId,
68-
'1',
69-
'🚨 orders[0].userId should be "1" - make sure you use the userId parameter',
70-
)
71-
})
16+
await test(
17+
'fetchUser resolves to a User object',
18+
{ timeout: 5000 },
19+
async () => {
20+
const user = await solution.fetchUser()
21+
22+
assert.ok(
23+
'id' in user,
24+
'🚨 user should have an id property - make sure fetchUser resolves correctly',
25+
)
26+
assert.ok(
27+
'name' in user,
28+
'🚨 user should have a name property - make sure fetchUser resolves correctly',
29+
)
30+
assert.ok(
31+
'email' in user,
32+
'🚨 user should have an email property - make sure fetchUser resolves correctly',
33+
)
34+
assert.strictEqual(
35+
user.id,
36+
'1',
37+
'🚨 user.id should be "1" - check your Promise resolve value',
38+
)
39+
assert.strictEqual(
40+
user.name,
41+
'Alice',
42+
'🚨 user.name should be "Alice" - check your Promise resolve value',
43+
)
44+
assert.strictEqual(
45+
user.email,
46+
47+
'🚨 user.email should be "[email protected]" - check your Promise resolve value',
48+
)
49+
},
50+
)
51+
52+
await test(
53+
'fetchOrders resolves to an array of Order objects',
54+
{ timeout: 5000 },
55+
async () => {
56+
const orders = await solution.fetchOrders('1')
57+
58+
assert.strictEqual(
59+
Array.isArray(orders),
60+
true,
61+
'🚨 orders should be an array - make sure fetchOrders returns an array',
62+
)
63+
assert.ok(
64+
orders.length > 0,
65+
'🚨 orders should have at least one item - check your Promise resolve value',
66+
)
67+
assert.ok(
68+
'id' in orders[0],
69+
'🚨 orders[0] should have an id property - check your Promise resolve value',
70+
)
71+
assert.ok(
72+
'userId' in orders[0],
73+
'🚨 orders[0] should have a userId property - check your Promise resolve value',
74+
)
75+
assert.ok(
76+
'items' in orders[0],
77+
'🚨 orders[0] should have an items property - check your Promise resolve value',
78+
)
79+
assert.ok(
80+
'total' in orders[0],
81+
'🚨 orders[0] should have a total property - check your Promise resolve value',
82+
)
83+
assert.strictEqual(
84+
orders[0].userId,
85+
'1',
86+
'🚨 orders[0].userId should be "1" - make sure you use the userId parameter',
87+
)
88+
},
89+
)
90+
91+
await test('promise chaining works correctly', { timeout: 5000 }, async () => {
92+
const user = await solution.fetchUser()
93+
const orders = await solution.fetchOrders(user.id)
7294

73-
await test('promise chaining works correctly', async () => {
7495
assert.strictEqual(
7596
user.id,
7697
'1',

exercises/01.promises/02.solution.chaining/index.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function fetchUser(): Promise<User> {
2121
name: 'Alice',
2222
2323
})
24-
}, 1000)
24+
}, 10)
2525
})
2626
}
2727

@@ -36,25 +36,8 @@ function fetchOrders(userId: string): Promise<Array<Order>> {
3636
total: 1299.99,
3737
},
3838
])
39-
}, 500)
39+
}, 10)
4040
})
4141
}
4242

43-
fetchUser()
44-
.then((user) => {
45-
console.log('User:', user)
46-
return fetchOrders(user.id)
47-
})
48-
.then((orders) => {
49-
console.log('Orders:', orders)
50-
console.log(
51-
'Results:',
52-
JSON.stringify({
53-
user: { id: '1', name: 'Alice', email: '[email protected]' },
54-
orders,
55-
}),
56-
)
57-
})
58-
.catch((error) => {
59-
console.error('Error:', error)
60-
})
43+
export { fetchUser, fetchOrders }

exercises/01.promises/02.solution.chaining/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.promises_02.solution.chaining",
33
"type": "module",
44
"scripts": {
5-
"start": "node index.ts",
5+
"start": "npx @kentcdodds/log-module@latest ./index.ts",
66
"test": "node --test index.test.ts"
77
}
88
}

0 commit comments

Comments
 (0)