Skip to content

Commit 4669f69

Browse files
committed
01/01: add solution
1 parent 5607bfb commit 4669f69

File tree

38 files changed

+106
-31
lines changed

38 files changed

+106
-31
lines changed

β€Ž.vscode/extensions.jsonβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"recommendations": [
33
"dbaeumer.vscode-eslint",
44
"esbenp.prettier-vscode",
5-
"bradlc.vscode-tailwindcss",
65
"neotan.vscode-auto-restart-typescript-eslint-servers"
76
]
87
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["vitest.explorer"]
3+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Visual Studio Code extension
2+
3+
Think about your testing loop right now. Most likely, it looks something like this:
4+
5+
1. You write some code;
6+
1. You write tests for that code;
7+
1. You run those tests and address failures, if any.
8+
1. Rinse and repeat.
9+
10+
The shorter is the time between each step in this loop, the faster you can get from having no tests to having tests written and passing. And since debugging often involves tests, this is important for fixing bugs more efficiently, too!
11+
12+
One of the ways to improve your efficiency and optimize this testing loop is by _getting faster feedback_ from your tests. As a baseline, you should see the test results right in your IDE, without even having to run any commands. Ideally, you should also have the tools to run a subset of tests, rerun them as you change your code, and debug failed test cases.
13+
14+
All of those things (and more) are provided by the official Vitest extension! Let's go through this exercise together and have it up and running in Visual Studio Code!
15+
16+
> At the time of writing this, Vitest provides an official extension only for Visual Studio Code. Keep an eye on the [IDE integrations](https://vitest.dev/guide/#ide-integrations) for any future support for other IDEs.
17+
18+
## Installing extension
19+
20+
🐨 Install the [Vitest extension](https://marketplace.visualstudio.com/items?itemName=vitest.explorer) from Visual Studio Code Marketplace.
21+
22+
And ... that's it! πŸŽ‰ Next, we will explore how to use the extension to iterate on tests faster and help you debug failed tests more efficiently.
23+
24+
## Running tests inline
25+
26+
Now, you can go to any test case in your project and click on the green "run" icon next to the test's name to run it inline. Vitest will report the test results right next to the test.
27+
28+
> πŸ¦‰ You can run all tests or a subset of tests from the "Testing" tab in your explorer as well.
29+
30+
## Continuous run
31+
32+
Start a continuous run of your tests by going to the "Testing" tab and clicking on the "Start Continuous Run" button (with the eye icon) in the controls panel of the extension. You will be prompted to choose test files to run in continuous (i.e. watch) mode.
33+
34+
Once the continuous run is active, you will see the test results _immediately_ as you work on your code or write your tests. This makes for a lightning-fast feedback loop when testing.
35+
36+
## Debugging failed tests
37+
38+
You can start a pre-configured debugging session for any test by clicking on the "Debug Test" button next to the failing test (or, alternatively, debug all tests via the "Debug Tests" button on the top panel). Use debuggers, breakpoints, and Debug Console to hunt down those nasty bugs and fix them.

β€Žexercises/01.setup/01.solution.vscode-extension/package.jsonβ€Ž renamed to β€Žexercises/01.setup/01.problem.vscode-extension/package.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"type": "module",
3-
"name": "exercises_01.setup_01.solution.vscode-extension",
3+
"name": "exercises_01.setup_01.problem.vscode-extension",
44
"scripts": {
55
"dev": "vite",
66
"test": "vitest",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { sortBy } from './sort-by'
2+
3+
test('sorts a list of fruits by their name', () => {
4+
expect(
5+
sortBy(
6+
[{ name: 'Banana' }, { name: 'Apple' }, { name: 'Cucumber' }],
7+
'name',
8+
),
9+
).toEqual([{ name: 'Apple' }, { name: 'Banana' }, { name: 'Cucumber' }])
10+
})
11+
12+
test('sorts a list of players by their score', () => {
13+
expect(
14+
sortBy(
15+
[
16+
{ name: 'John', score: 10 },
17+
{ name: 'Allen', score: 5 },
18+
{ name: 'Sarah', score: 21 },
19+
],
20+
'score',
21+
),
22+
).toEqual([
23+
{ name: 'Sarah', score: 21 },
24+
{ name: 'John', score: 10 },
25+
{ name: 'Allen', score: 5 },
26+
])
27+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function sortBy<T extends Record<string, unknown>>(
2+
list: Array<T>,
3+
sortKey: keyof T,
4+
): Array<T> {
5+
const clone = structuredClone(list)
6+
7+
clone.sort((left, right) => {
8+
return left[sortKey] < right[sortKey] ? -1 : 1
9+
})
10+
11+
return clone
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"include": ["src/**/*"],
4+
"exclude": ["src/**/*.test.ts*"]
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"compilerOptions": {
44
"lib": ["ES2023"]
55
},
6-
"include": ["vite.config.ts"]
6+
"include": ["vitest.config.ts"]
77
}

0 commit comments

Comments
Β (0)