Skip to content

Commit eb84880

Browse files
add copilot exercise
1 parent 02ad22c commit eb84880

14 files changed

+16779
-4
lines changed

docs/package-lock.json

Lines changed: 16709 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/develop/README.md

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,76 @@ Github CLI is not supported yet with our Worldline account, so you need to use t
5757

5858
## 🧪 Exercise
5959

60-
::: warning TODO
61-
#### Install Github Copilot on VSCode and generate tests for the following project repository
62-
#### Use Copilot to refactor the following project repository
63-
:::
60+
### Install Github Copilot on VSCode
61+
62+
1. Install the [Github Copilot extension](https://marketplace.visualstudio.com/items?itemName=github.copilot) on your VSCode
63+
2. Sign in with your Github account, if not already done
64+
3. Create a new file and start typing a function or a class, to see the suggestions provided by Copilot
65+
66+
### Generating unit tests
67+
68+
We"re going to use Github Copilot to generate unit tests for a simple JavaScript project repository.
69+
70+
To run the project and unit tests, you will need [NodeJs](https://nodejs.org/en/download/) installed on your machine.
71+
72+
1. Clone the following repository: [github.com/worldline/learning-ai-workspace-js](https://github.com/worldline/learning-ai-workspace-js)
73+
2. Open the project in your VSCode
74+
3. Open Github Copilot Chat by clicking on the Copilot icon in the bottom right corner of your VSCode
75+
4. Ask Copilot to generate unit tests for the `index.js` file . You can also try the `/setupTests` command
76+
5. Copilot may make several suggestions: choosing a testing framework, adding a `test` command to package.json, install new dependencies. Accept all its suggestions.
77+
6. Try to run the generated tests. In case of trouble, use Copilot Chat to ask for help.
78+
79+
80+
<details>
81+
<summary>Solution</summary>
82+
83+
<p>Here we decided to go with supertest framework</p>
84+
<img src="./images/setupTestsCommand.png" height="400" />
85+
<img src="./images/setupTests3.png" height="400" />
86+
<img src="./images/setupTests.png" height="400" />
87+
<img src="./images/setupTests2.png" height="400" />
88+
89+
<p>Here is an example of how Copilot can help you fix a failing test:</p>
90+
<img src="./images/fixTest.png" height="300" />
91+
<img src="./images/fixTest2.png" height="300" />
92+
93+
</details>
94+
95+
### Refactoring
96+
97+
Now we are going to use Copilot to refactor a piece of code in the same project.
98+
99+
1. Open the `index.js` file in the project
100+
2. Ask Copilot to add a feature in the GET /movies endpoint that allows filtering movies by director, based on a `director` query parameter.
101+
3. Copilot will generate the code for you. Try to understand the changes it made and run the project to test the new feature.
102+
4. Ask Copilot to complete the unit test in `index.test.js` to test getting movies filtered by director. It should generate more unit tests that check against one of the directors in the example data.
103+
5. Now we're going to refactor the code to extract the filtering logic into a separate function. Select the parts of the code with the `.find()` and `.filter()` function calls and ask Copilot to extract them into a new function. Let Copilot suggest a name for these functions
104+
6. Under the previous generated function, type `function filterMoviesByYear(`. Wait for Copilot to suggest you the rest of the function signature and function body. Accept the suggestion using the `Tab` key.
105+
7. Ask Copilot again to allow filtering movies by a `year` query parameter. Copilot should use the `filterMoviesByYear` function you just created to implement this feature.
106+
8. Open `index.test.js`. In the `GET /movies` test block, add a new assertion block by typing `it('should return movies filtered by year',`. Wait for Copilot to suggest you the rest of the tests. Review code to make sure it uses the ?year query parameter and checks correctly a date from the example data.
107+
9. Run the tests to make sure everything is working as expected. Use Copilot to ask for help if needed.
108+
109+
<details>
110+
<summary>Solution</summary>
111+
112+
<p>Adding the new feature</p>
113+
<img src="./images/addFeature.png" />
114+
115+
<p>Complete the test</p>
116+
<img src="./images/completeTest.png" />
117+
118+
<p>Refactor the code to extract logic</p>
119+
<img src="./images/refactor.png" />
120+
121+
<p>Get code suggestion from a function name</p>
122+
<img src="./images/codeSuggestion.png" />
123+
124+
<p>Refactor with Copilot Chat the API logic</p>
125+
<img src="./images/refactor2.png" />
126+
127+
<p>Get code suggestion from test description</p>
128+
<img src="./images/testSuggestion.png" />
129+
</details>
64130
65131
## Gihub Spark
66132
45.3 KB
Loading
7.44 KB
Loading
66.8 KB
Loading

docs/src/develop/images/fixTest.png

42.5 KB
Loading

docs/src/develop/images/fixTest2.png

38.8 KB
Loading

docs/src/develop/images/refactor.png

47.5 KB
Loading

docs/src/develop/images/refactor2.png

44.5 KB
Loading
51.6 KB
Loading
22.7 KB
Loading
22.7 KB
Loading
20 KB
Loading
32.3 KB
Loading

0 commit comments

Comments
 (0)