Skip to content

Commit e8733db

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 0e6780b commit e8733db

File tree

104 files changed

+795
-736
lines changed

Some content is hidden

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

104 files changed

+795
-736
lines changed

exercises/01.expressions-and-output/01.problem.hello-world/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.expressions-and-output_01.problem.hello-world",
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.expressions-and-output/01.solution.hello-world/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.expressions-and-output_01.solution.hello-world",
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.expressions-and-output/02.problem.escaping-strings/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,5 @@
1313
// 🐨 Log tab-separated data: Name: [tab] Age: [tab] City: (like column headers)
1414
// 💰 Use \t to create tabs between words
1515

16-
// 🐨 When you're done, uncomment this and update the values:
17-
// const results = {
18-
// apostrophe: "It's working!",
19-
// quotes: 'She said "Hi"',
20-
// newlines: 'Hello\nWorld',
21-
// tabs: 'Name:\tAge:\tCity:',
22-
// }
23-
// console.log('Results:', JSON.stringify(results))
16+
// 🐨 Export your variables so we can verify your work
17+
// 💰 export { apostrophe, quotes, newlines, tabs }

exercises/01.expressions-and-output/02.problem.escaping-strings/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "exercises_01.expressions-and-output_02.problem.escaping-strings",
33
"type": "module",
44
"scripts": {
5-
"start": "node index.ts",
5+
"start": "npx @kentcdodds/log-module ./index.ts",
66
"test": "node --test index.test.ts"
77
}
88
}
Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
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 { apostrophe, quotes, newlines, tabs } = JSON.parse(
9-
jsonLine.replace('Results:', '').trim(),
10-
)
5+
await test('apostrophe is exported', () => {
6+
assert.ok(
7+
'apostrophe' in solution,
8+
'🚨 Make sure you export "apostrophe" - add: export { apostrophe, ... }',
9+
)
10+
})
11+
12+
await test('should have string with apostrophe', () => {
13+
assert.strictEqual(
14+
solution.apostrophe,
15+
"It's working!",
16+
'🚨 apostrophe should be "It\'s working!" - make sure you escaped the apostrophe with \\\'',
17+
)
18+
})
1119

12-
await test('should print string with apostrophe', () => {
20+
await test('quotes is exported', () => {
1321
assert.ok(
14-
apostrophe === "It's working!",
15-
'🚨 Output should include "It\'s working!" - make sure you escaped the apostrophe',
22+
'quotes' in solution,
23+
'🚨 Make sure you export "quotes" - add: export { apostrophe, quotes, ... }',
1624
)
1725
})
1826

19-
await test('should print string with quotes', () => {
27+
await test('should have string with quotes', () => {
28+
assert.strictEqual(
29+
solution.quotes,
30+
'She said "Hi"',
31+
'🚨 quotes should be: She said "Hi" - make sure you escaped the double quotes with \\"',
32+
)
33+
})
34+
35+
await test('newlines is exported', () => {
2036
assert.ok(
21-
quotes === 'She said "Hi"',
22-
'🚨 Output should include: She said "Hi" - make sure you escaped the double quotes',
37+
'newlines' in solution,
38+
'🚨 Make sure you export "newlines" - add: export { ..., newlines, ... }',
39+
)
40+
})
41+
42+
await test('should have Hello and World on separate lines', () => {
43+
assert.strictEqual(
44+
solution.newlines,
45+
'Hello\nWorld',
46+
'🚨 newlines should be "Hello\\nWorld" - use \\n in a single string',
2347
)
2448
})
2549

26-
await test('should print Hello and World on separate lines', () => {
50+
await test('tabs is exported', () => {
2751
assert.ok(
28-
newlines === 'Hello\nWorld',
29-
'🚨 Output should include "Hello" and "World" on separate lines - use \\n in a single string',
52+
'tabs' in solution,
53+
'🚨 Make sure you export "tabs" - add: export { ..., tabs }',
3054
)
3155
})
3256

33-
await test('should print tab-separated values', () => {
57+
await test('should have tab-separated values', () => {
3458
assert.ok(
35-
tabs.includes('\t'),
36-
'🚨 Output should include tab characters - make sure you used \\t for tabs',
59+
solution.tabs.includes('\t'),
60+
'🚨 tabs should include tab characters - make sure you used \\t for tabs',
3761
)
3862
})

exercises/01.expressions-and-output/02.solution.escaping-strings/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ console.log(newlines)
1717
const tabs = 'Name:\tAge:\tCity:'
1818
console.log(tabs)
1919

20-
console.log('Results:', JSON.stringify({ apostrophe, quotes, newlines, tabs }))
20+
export { apostrophe, quotes, newlines, tabs }

exercises/01.expressions-and-output/02.solution.escaping-strings/package.json

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

exercises/01.expressions-and-output/03.problem.strings/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,5 @@
88
// 🐨 Log a sentence by concatenating multiple strings
99
// 💰 Example: "I" + " " + "am" + " " + "learning" + " " + "to" + " " + "code"
1010

11-
// 🐨 When you're done, uncomment this and update the values:
12-
// const results = {
13-
// greeting: 'Hello TypeScript',
14-
// fullName: 'Your Name',
15-
// sentence: 'I am learning to code',
16-
// }
17-
// console.log('Results:', JSON.stringify(results))
11+
// 🐨 Export your variables so we can verify your work
12+
// 💰 export { greeting, fullName, sentence }

exercises/01.expressions-and-output/03.problem.strings/package.json

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

exercises/01.expressions-and-output/03.solution.strings/index.test.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
import assert from 'node:assert/strict'
2-
import { execSync } from 'node:child_process'
32
import { readFileSync } from 'node:fs'
43
import { test } from 'node:test'
4+
import * as solution from './index.ts'
55

6-
const output = execSync('npm start --silent', { encoding: 'utf8' })
7-
const jsonLine = output.split('\n').find((line) => line.startsWith('Results:'))
8-
assert.ok(jsonLine, '🚨 Missing "Results:" output line')
9-
const { greeting } = JSON.parse(jsonLine.replace('Results:', '').trim())
6+
await test('greeting is exported', () => {
7+
assert.ok(
8+
'greeting' in solution,
9+
'🚨 Make sure you export "greeting" - add: export { greeting, ... }',
10+
)
11+
})
12+
13+
await test('should have Hello TypeScript', () => {
14+
assert.strictEqual(
15+
solution.greeting,
16+
'Hello TypeScript',
17+
'🚨 greeting should be "Hello TypeScript" - concatenate "Hello" + " " + "TypeScript"',
18+
)
19+
})
20+
21+
await test('fullName is exported', () => {
22+
assert.ok(
23+
'fullName' in solution,
24+
'🚨 Make sure you export "fullName" - add: export { greeting, fullName, ... }',
25+
)
26+
})
1027

11-
await test('should print Hello TypeScript', () => {
28+
await test('fullName should have a space', () => {
1229
assert.ok(
13-
greeting === 'Hello TypeScript',
14-
'🚨 Output should include "Hello TypeScript" - concatenate "Hello" + " " + "TypeScript"',
30+
solution.fullName.includes(' '),
31+
'🚨 fullName should contain a space between first and last name',
1532
)
1633
})
1734

18-
await test('should print at least three lines', () => {
19-
const lines = output.trim().split('\n').filter(Boolean)
35+
await test('sentence is exported', () => {
2036
assert.ok(
21-
lines.length >= 3,
22-
'🚨 Output should have at least 3 lines - make sure you logged all three string expressions',
37+
'sentence' in solution,
38+
'🚨 Make sure you export "sentence" - add: export { greeting, fullName, sentence }',
2339
)
2440
})
2541

0 commit comments

Comments
 (0)