Skip to content

Commit b6dadcc

Browse files
committed
04/03: add test isolation exercise
1 parent 6fe2dde commit b6dadcc

Some content is hidden

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

43 files changed

+280
-49
lines changed

β€Ž.gitignoreβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ __screenshots__
1515
test-profiles
1616
coverage
1717
.vitest-reports
18+
*.DS_Store
1819

1920
# in a real app you'd want to not commit the .env
2021
# file as well, but since this is for a workshop

β€Žepicshop/generate-tests.jsβ€Ž

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import fs from 'node:fs'
2+
3+
const TEST_COUNT = 500
4+
5+
/**
6+
* @param {URL} exerciseDirectory
7+
* @param {number} count
8+
*/
9+
async function generateTests(exerciseDirectory, count) {
10+
const TESTS_DIR = new URL('./tests/', exerciseDirectory)
11+
12+
const existingTests = fs.globSync('./*.test.ts', {
13+
cwd: TESTS_DIR.pathname,
14+
})
15+
await Promise.all(
16+
existingTests.map((filename) => {
17+
return fs.promises.rm(new URL(filename, TESTS_DIR))
18+
}),
19+
)
20+
21+
await Promise.all(
22+
Array.from({ length: count }).map((_, index) => {
23+
const filename = `./test-${index.toString().padStart(count.toString().length, '0')}.test.ts`
24+
const contents = `\
25+
test('equals to ${index}', ({ expect }) => {
26+
expect(${index}).toBe(${index})
27+
})
28+
`
29+
30+
return fs.promises.writeFile(
31+
new URL(filename, TESTS_DIR),
32+
contents,
33+
'utf8',
34+
)
35+
}),
36+
)
37+
}
38+
39+
Promise.all([
40+
generateTests(
41+
new URL(
42+
'../exercises/04.performance/03.problem.test-isolation/',
43+
import.meta.url,
44+
),
45+
TEST_COUNT,
46+
),
47+
generateTests(
48+
new URL(
49+
'../exercises/04.performance/03.solution.test-isolation/',
50+
import.meta.url,
51+
),
52+
TEST_COUNT,
53+
),
54+
]).catch((error) => {
55+
console.error('❌ Failed to generate tests.\n\n', error)
56+
process.exit(1)
57+
})

β€Žepicshop/setup.jsβ€Ž

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawnSync } from 'child_process'
1+
import { spawnSync } from 'node:child_process'
22

33
const styles = {
44
// got these from playing around with what I found from:
@@ -34,18 +34,33 @@ if (major < 8 || (major === 8 && minor < 16)) {
3434
throw new Error('npm version is out of date')
3535
}
3636

37-
const command =
38-
'npx --yes "https://gist.github.com/kentcdodds/bb452ffe53a5caa3600197e1d8005733" -q'
39-
console.log(
40-
color('subtitle', ' Running the following command: ' + command),
41-
)
37+
// Generate test suites for exercises.
38+
{
39+
const result = spawnSync('node ./generate-tests.js', {
40+
cwd: new URL('.', import.meta.url),
41+
stdio: 'inherit',
42+
shell: true,
43+
})
4244

43-
const result = spawnSync(command, { stdio: 'inherit', shell: true })
45+
if (result.status !== 0) {
46+
process.exit(result.status)
47+
}
48+
}
49+
50+
{
51+
const command =
52+
'npx --yes "https://gist.github.com/kentcdodds/bb452ffe53a5caa3600197e1d8005733" -q'
53+
console.log(
54+
color('subtitle', ' Running the following command: ' + command),
55+
)
56+
57+
const result = spawnSync(command, { stdio: 'inherit', shell: true })
4458

45-
if (result.status === 0) {
46-
console.log(color('success', 'βœ… Workshop setup complete...'))
47-
} else {
48-
process.exit(result.status)
59+
if (result.status === 0) {
60+
console.log(color('success', 'βœ… Workshop setup complete!'))
61+
} else {
62+
process.exit(result.status)
63+
}
4964
}
5065

5166
/*
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore the test files for this exercise because
2+
# they are generated during the setup step.
3+
/tests/**/*.test.ts
Lines changed: 1 addition & 27 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "module",
3+
"name": "exercises_04.performance_03.problem.test-isolation",
4+
"scripts": {
5+
"test": "vitest"
6+
},
7+
"devDependencies": {
8+
"vitest": "^3.1.1"
9+
}
10+
}

β€Žexercises/04.performance/03.problem.test-isolation/tests/.gitkeepβ€Ž

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"lib": ["ES2023"]
5+
},
6+
"include": ["vitest.config.ts", "generate-tests.ts"]
7+
}

0 commit comments

Comments
Β (0)