Skip to content

Commit ce427fe

Browse files
committed
improve tests
1 parent a809993 commit ce427fe

File tree

12 files changed

+109
-145
lines changed

12 files changed

+109
-145
lines changed

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

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

16-
// 🐨 Export your variables so we can verify your work
17-
// 💰 export { apostrophe, quotes, newlines, tabs }
16+
// 🐨 No exports needed for this exercise
Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,49 @@
11
import assert from 'node:assert/strict'
2+
import { spawnSync } from 'node:child_process'
23
import { test } from 'node:test'
3-
import * as solution from './index.ts'
44

5-
await test('apostrophe is exported', () => {
6-
assert.ok(
7-
'apostrophe' in solution,
8-
'🚨 Make sure you export "apostrophe" - add: export { apostrophe, ... }',
9-
)
10-
})
5+
function getOutput() {
6+
const result = spawnSync('npm', ['start', '--silent'], { encoding: 'utf8' })
7+
8+
if (result.error) {
9+
throw result.error
10+
}
1111

12-
await test('should have string with apostrophe', () => {
1312
assert.strictEqual(
14-
solution.apostrophe,
15-
"It's working!",
16-
'🚨 apostrophe should be "It\'s working!" - make sure you escaped the apostrophe with \\\'',
13+
result.status,
14+
0,
15+
result.stderr || '🚨 Running the program failed',
1716
)
18-
})
1917

20-
await test('quotes is exported', () => {
21-
assert.ok(
22-
'quotes' in solution,
23-
'🚨 Make sure you export "quotes" - add: export { apostrophe, quotes, ... }',
24-
)
25-
})
18+
return result.stdout.replace(/\r\n/g, '\n')
19+
}
2620

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-
})
21+
const output = getOutput()
3422

35-
await test('newlines is exported', () => {
23+
await test('should print apostrophe string', () => {
3624
assert.ok(
37-
'newlines' in solution,
38-
'🚨 Make sure you export "newlines" - add: export { ..., newlines, ... }',
25+
output.includes("It's working!"),
26+
'🚨 Output should include "It\'s working!" - make sure you escaped the apostrophe with \\\'',
3927
)
4028
})
4129

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',
30+
await test('should print string with quotes', () => {
31+
assert.ok(
32+
output.includes('She said "Hi"'),
33+
'🚨 Output should include: She said "Hi" - make sure you escaped the double quotes with \\"',
4734
)
4835
})
4936

50-
await test('tabs is exported', () => {
37+
await test('should print Hello and World on separate lines', () => {
5138
assert.ok(
52-
'tabs' in solution,
53-
'🚨 Make sure you export "tabs" - add: export { ..., tabs }',
39+
output.includes('Hello\nWorld'),
40+
'🚨 Output should include "Hello\\nWorld" - use \\n in a single string',
5441
)
5542
})
5643

57-
await test('should have tab-separated values', () => {
44+
await test('should print tab-separated values', () => {
5845
assert.ok(
59-
solution.tabs.includes('\t'),
60-
'🚨 tabs should include tab characters - make sure you used \\t for tabs',
46+
output.includes('Name:\tAge:\tCity:'),
47+
'🚨 Output should include tab characters - make sure you used \\t for tabs',
6148
)
6249
})

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ console.log(newlines)
1616
// prettier-ignore
1717
const tabs = 'Name:\tAge:\tCity:'
1818
console.log(tabs)
19-
20-
export { apostrophe, quotes, newlines, tabs }

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

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

11-
// 🐨 Export your variables so we can verify your work
12-
// 💰 export { greeting, fullName, sentence }
11+
// 🐨 No exports needed for this exercise

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
import assert from 'node:assert/strict'
2+
import { spawnSync } from 'node:child_process'
23
import { readFileSync } from 'node:fs'
34
import { test } from 'node:test'
4-
import * as solution from './index.ts'
55

6-
await test('greeting is exported', () => {
7-
assert.ok(
8-
'greeting' in solution,
9-
'🚨 Make sure you export "greeting" - add: export { greeting, ... }',
10-
)
11-
})
6+
function getOutput() {
7+
const result = spawnSync('npm', ['start', '--silent'], { encoding: 'utf8' })
8+
9+
if (result.error) {
10+
throw result.error
11+
}
1212

13-
await test('should have Hello TypeScript', () => {
1413
assert.strictEqual(
15-
solution.greeting,
16-
'Hello TypeScript',
17-
'🚨 greeting should be "Hello TypeScript" - concatenate "Hello" + " " + "TypeScript"',
14+
result.status,
15+
0,
16+
result.stderr || '🚨 Running the program failed',
1817
)
19-
})
2018

21-
await test('fullName is exported', () => {
19+
return result.stdout.replace(/\r\n/g, '\n')
20+
}
21+
22+
const output = getOutput()
23+
24+
await test('should print Hello TypeScript', () => {
2225
assert.ok(
23-
'fullName' in solution,
24-
'🚨 Make sure you export "fullName" - add: export { greeting, fullName, ... }',
26+
output.includes('Hello TypeScript'),
27+
'🚨 Output should include "Hello TypeScript" - concatenate "Hello" + " " + "TypeScript"',
2528
)
2629
})
2730

28-
await test('fullName should have a space', () => {
31+
await test('should print a full name with a space', () => {
2932
assert.ok(
30-
solution.fullName.includes(' '),
31-
'🚨 fullName should contain a space between first and last name',
33+
output.includes('Kody Koala'),
34+
'🚨 Output should include a full name with a space between first and last name',
3235
)
3336
})
3437

35-
await test('sentence is exported', () => {
38+
await test('should print the concatenated sentence', () => {
3639
assert.ok(
37-
'sentence' in solution,
38-
'🚨 Make sure you export "sentence" - add: export { greeting, fullName, sentence }',
40+
output.includes('I am learning to code'),
41+
'🚨 Output should include "I am learning to code" - concatenate multiple strings',
3942
)
4043
})
4144

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@ const sentence = 'I' + ' ' + 'am' + ' ' + 'learning' + ' ' + 'to' + ' ' + 'code'
88
console.log(greeting)
99
console.log(fullName)
1010
console.log(sentence)
11-
12-
export { greeting, fullName, sentence }

exercises/01.expressions-and-output/04.problem.numbers/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@
1010
// 🐨 Use console.log to show the result of (10 + 5) multiplied by 2
1111
// 💰 Use parentheses to control order of operations
1212

13-
// 🐨 Export your variables so we can verify your work
14-
// 💰 export { addResult, multiplyResult, divideResult, groupedResult }
13+
// 🐨 No exports needed for this exercise
Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,53 @@
11
import assert from 'node:assert/strict'
2+
import { spawnSync } from 'node:child_process'
23
import { test } from 'node:test'
3-
import * as solution from './index.ts'
44

5-
await test('addResult is exported', () => {
6-
assert.ok(
7-
'addResult' in solution,
8-
'🚨 Make sure you export "addResult" - add: export { addResult, ... }',
9-
)
10-
})
5+
function getOutput() {
6+
const result = spawnSync('npm', ['start', '--silent'], { encoding: 'utf8' })
7+
8+
if (result.error) {
9+
throw result.error
10+
}
1111

12-
await test('addResult should be 42', () => {
1312
assert.strictEqual(
14-
solution.addResult,
15-
42,
16-
'🚨 addResult should be 42 (the result of 25 + 17)',
13+
result.status,
14+
0,
15+
result.stderr || '🚨 Running the program failed',
1716
)
18-
})
1917

20-
await test('multiplyResult is exported', () => {
21-
assert.ok(
22-
'multiplyResult' in solution,
23-
'🚨 Make sure you export "multiplyResult" - add: export { ..., multiplyResult, ... }',
24-
)
25-
})
18+
return result.stdout.replace(/\r\n/g, '\n')
19+
}
2620

27-
await test('multiplyResult should be 48', () => {
28-
assert.strictEqual(
29-
solution.multiplyResult,
30-
48,
31-
'🚨 multiplyResult should be 48 (the result of 8 * 6)',
32-
)
33-
})
21+
const output = getOutput()
22+
const lines = output
23+
.split('\n')
24+
.map((line) => line.trim())
25+
.filter(Boolean)
3426

35-
await test('divideResult is exported', () => {
27+
await test('should print 42', () => {
3628
assert.ok(
37-
'divideResult' in solution,
38-
'🚨 Make sure you export "divideResult" - add: export { ..., divideResult, ... }',
29+
lines.includes('42'),
30+
'🚨 Output should include 42 (the result of 25 + 17)',
3931
)
4032
})
4133

42-
await test('divideResult should be 25', () => {
43-
assert.strictEqual(
44-
solution.divideResult,
45-
25,
46-
'🚨 divideResult should be 25 (the result of 100 / 4)',
34+
await test('should print 48', () => {
35+
assert.ok(
36+
lines.includes('48'),
37+
'🚨 Output should include 48 (the result of 8 * 6)',
4738
)
4839
})
4940

50-
await test('groupedResult is exported', () => {
41+
await test('should print 25', () => {
5142
assert.ok(
52-
'groupedResult' in solution,
53-
'🚨 Make sure you export "groupedResult" - add: export { ..., groupedResult }',
43+
lines.includes('25'),
44+
'🚨 Output should include 25 (the result of 100 / 4)',
5445
)
5546
})
5647

57-
await test('groupedResult should be 30', () => {
58-
assert.strictEqual(
59-
solution.groupedResult,
60-
30,
61-
'🚨 groupedResult should be 30 (the result of (10 + 5) * 2)',
48+
await test('should print 30', () => {
49+
assert.ok(
50+
lines.includes('30'),
51+
'🚨 Output should include 30 (the result of (10 + 5) * 2)',
6252
)
6353
})

exercises/01.expressions-and-output/04.solution.numbers/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@ console.log(addResult)
1010
console.log(multiplyResult)
1111
console.log(divideResult)
1212
console.log(groupedResult)
13-
14-
export { addResult, multiplyResult, divideResult, groupedResult }

exercises/01.expressions-and-output/05.problem.template-literals/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
// 🐨 Log a math problem like "10 times 5 equals 50"
1010
// 💰 Use ${10 * 5} for the result
1111

12-
// 🐨 Export your variables so we can verify your work
13-
// 💰 export { answer, greeting, math }
12+
// 🐨 No exports needed for this exercise

0 commit comments

Comments
 (0)