Skip to content

Commit b1b4a92

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
1 parent bdf5171 commit b1b4a92

File tree

87 files changed

+675
-710
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

+675
-710
lines changed

exercises/01.objects/01.problem.object-literals/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,5 @@
1414
// console.log(`User: ${user.name}, Age: ${user.age}`)
1515
// console.log(`Admin: ${admin.name}, Age: ${admin.age}`)
1616

17-
// 🐨 When you're done, uncomment this:
18-
// console.log(
19-
// 'Results:',
20-
// JSON.stringify({
21-
// user,
22-
// admin,
23-
// }),
24-
// )
17+
// 🐨 Export your variables so we can verify your work
18+
// 💰 export { user, admin }

exercises/01.objects/01.problem.object-literals/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.objects_01.problem.object-literals",
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.objects/01.solution.object-literals/index.test.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +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, admin } = JSON.parse(jsonLine.replace('Results:', '').trim())
5+
await test('user is exported', () => {
6+
assert.ok(
7+
'user' in solution,
8+
'🚨 Make sure you export "user" - add: export { user, ... }',
9+
)
10+
})
911

1012
await test('User object should have correct properties', () => {
1113
assert.strictEqual(
12-
user.name,
14+
solution.user.name,
1315
'Alice',
1416
'🚨 user.name should be "Alice" - make sure you set the name property correctly',
1517
)
1618
assert.strictEqual(
17-
user.age,
19+
solution.user.age,
1820
30,
1921
'🚨 user.age should be 30 - check that you set the age property to the correct number',
2022
)
2123
assert.strictEqual(
22-
user.email,
24+
solution.user.email,
2325
2426
'🚨 user.email should be "[email protected]" - verify the email property is set correctly',
2527
)
2628
})
2729

30+
await test('admin is exported', () => {
31+
assert.ok(
32+
'admin' in solution,
33+
'🚨 Make sure you export "admin" - add: export { user, admin }',
34+
)
35+
})
36+
2837
await test('Admin object should have correct properties', () => {
2938
assert.strictEqual(
30-
admin.name,
39+
solution.admin.name,
3140
'Bob',
3241
'🚨 admin.name should be "Bob" - make sure you set the name property correctly',
3342
)
3443
assert.strictEqual(
35-
admin.age,
44+
solution.admin.age,
3645
35,
3746
'🚨 admin.age should be 35 - check that you set the age property to the correct number',
3847
)
3948
assert.strictEqual(
40-
admin.email,
49+
solution.admin.email,
4150
4251
'🚨 admin.email should be "[email protected]" - verify the email property is set correctly',
4352
)

exercises/01.objects/01.solution.object-literals/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,4 @@ const admin: { name: string; age: number; email: string } = {
1616
console.log(`User: ${user.name}, Age: ${user.age}`)
1717
console.log(`Admin: ${admin.name}, Age: ${admin.age}`)
1818

19-
console.log(
20-
'Results:',
21-
JSON.stringify({
22-
user,
23-
admin,
24-
}),
25-
)
19+
export { user, admin }

exercises/01.objects/01.solution.object-literals/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.objects_01.solution.object-literals",
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.objects/02.problem.property-access/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,5 @@ const product = {
2323

2424
// console.log(formatProduct(product))
2525

26-
// 🐨 When you're done, uncomment this:
27-
// console.log(
28-
// 'Results:',
29-
// JSON.stringify({
30-
// product,
31-
// formatted: formatProduct(product),
32-
// }),
33-
// )
26+
// 🐨 Export your variables and functions so we can verify your work
27+
// 💰 export { product, formatProduct }

exercises/01.objects/02.problem.property-access/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.objects_02.problem.property-access",
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.objects/02.solution.property-access/index.test.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
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 { product, formatted } = JSON.parse(
9-
jsonLine.replace('Results:', '').trim(),
10-
)
5+
await test('product is exported', () => {
6+
assert.ok(
7+
'product' in solution,
8+
'🚨 Make sure you export "product" - add: export { product, ... }',
9+
)
10+
})
1111

1212
await test('Product object should have correct properties', () => {
1313
assert.strictEqual(
14-
product.name,
14+
solution.product.name,
1515
'TypeScript Handbook',
1616
'🚨 product.name should be "TypeScript Handbook" - use dot notation to access the name property',
1717
)
1818
assert.strictEqual(
19-
product['price'],
19+
solution.product['price'],
2020
29.99,
2121
'🚨 product["price"] should be 29.99 - use bracket notation to access the price property',
2222
)
2323
assert.strictEqual(
24-
product.inStock,
24+
solution.product.inStock,
2525
true,
2626
'🚨 product.inStock should be true - access the inStock property using dot notation',
2727
)
2828
assert.strictEqual(
29-
product.category,
29+
solution.product.category,
3030
'Books',
3131
'🚨 product.category should be "Books" - make sure you access the category property correctly',
3232
)
3333
})
3434

35+
await test('formatProduct is exported', () => {
36+
assert.ok(
37+
'formatProduct' in solution,
38+
'🚨 Make sure you export "formatProduct" - add: export { product, formatProduct }',
39+
)
40+
})
41+
3542
await test('formatProduct should format product correctly', () => {
43+
const formatted = solution.formatProduct(solution.product)
3644
assert.strictEqual(
3745
formatted,
3846
'TypeScript Handbook - $29.99',

exercises/01.objects/02.solution.property-access/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,4 @@ function formatProduct(p: { name: string; price: number }): string {
2222

2323
console.log(formatProduct(product))
2424

25-
console.log(
26-
'Results:',
27-
JSON.stringify({
28-
product,
29-
formatted: formatProduct(product),
30-
}),
31-
)
25+
export { product, formatProduct }

exercises/01.objects/02.solution.property-access/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.objects_02.solution.property-access",
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)