Skip to content

Commit 3a84e5c

Browse files
authored
Rework sprint 3 assignments (#694)
Previously they were: 1. Very repetitive, having the trainees solve the same problem more than once which was confusing and not a great use of time. 2. Large. Reviewing the whole PR for the sprint took a long time, so was very daunting for reviewers, and led to review backlogs. 3. Hard to track who was doing the stretch, because it was maybe in the same PR or just maybe missing from the PR. Instead: 1. Re-use their implementation for both sets of tests - this emphasises also that the point of the exercise is writing tests different ways. 2. Split off the practice-tdd and stretch into their own expected separate PRs. 3. Fix up a bunch of inconsistent formatting.
1 parent c3f337e commit 3a84e5c

26 files changed

+132
-127
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Implement solutions and rewrite tests with Jest
2+
3+
## 1 Implement solutions
4+
5+
In the `implement` directory you've got a number of functions you'll need to implement.
6+
For each function, you also have a number of different cases you'll need to check for your function.
7+
8+
Write your assertions and build up your program case by case. Don't rush to a solution. The point of these assignments is to learn how to write assertions and build up a program step by step.
9+
10+
Here is a recommended order:
11+
12+
1. `1-get-angle-type.js`
13+
1. `2-is-proper-fraction.js`
14+
1. `3-get-card-value.js`
15+
16+
## 2 Rewrite tests with Jest
17+
18+
`console.log` is most often used as a debugging tool. We use to inspect the state of our program during runtime.
19+
20+
We can use `console.assert` to write assertions: however, it is not very easy to use when writing large test suites. In the first section, Implement, we used a custom "helper function" to make our assertions more readable.
21+
22+
Jest is a whole library of helper functions we can use to make our assertions more readable and easier to write.
23+
24+
Your new task is to write the same tests as you wrote in the `implement` directory, but using Jest instead of `console.assert`.
25+
26+
You shouldn't have to change the contents of `implement` to write these tests.
27+
28+
There are files for your Jest tests in the `rewrite-tests-with-jest` directory. They will automatically use the functions you already implemented.
29+
30+
You can run all the tests in this repo by running `npm test` in your terminal. However, VSCode has a built-in test runner that you can use to run the tests, and this should make it much easier to focus on building up your test cases one at a time.
31+
32+
https://code.visualstudio.com/docs/editor/testing
33+
34+
1. Go to rewrite-tests-with-jest/1-get-angle-type.test.js
35+
1. Click the green play button to run the test. It's on the left of the test function in the gutter.
36+
1. Read the output in the TEST_RESULTS tab at the bottom of the screen.
37+
1. Explore all the tests in this repo by opening the TEST EXPLORER tab. The logo is a beaker.
38+
39+
![VSCode Test Runner](../../run-this-test.png)
40+
41+
![Test Results](../../test-results-output.png)
42+
43+
> [!TIP]
44+
> You can always run a single test file by running `npm test path/to/test-file.test.js`.

Sprint-3/1-key-implement/1-get-angle-type.js renamed to Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
// Then, write the next test! :) Go through this process until all the cases are implemented
99

1010
function getAngleType(angle) {
11-
if (angle === 90) return "Right angle";
12-
// read to the end, complete line 36, then pass your test here
11+
if (angle === 90) {
12+
return "Right angle";
13+
}
14+
// Run the tests, work out what Case 2 is testing, and implement the required code here.
15+
// Then keep going for the other cases, one at a time.
1316
}
1417

18+
// The line below allows us to load the getAngleType function into tests in other files.
19+
// This will be useful in the "rewrite tests with jest" step.
20+
module.exports = getAngleType;
21+
1522
// we're going to use this helper function to make our assertions easier to read
1623
// if the actual output matches the target output, the test will pass
1724
function assertEquals(actualOutput, targetOutput) {

Sprint-3/1-key-implement/2-is-proper-fraction.js renamed to Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99

1010
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) return true;
11+
if (numerator < denominator) {
12+
return true;
13+
}
1214
}
1315

16+
// The line below allows us to load the isProperFraction function into tests in other files.
17+
// This will be useful in the "rewrite tests with jest" step.
18+
module.exports = isProperFraction;
19+
1420
// here's our helper again
1521
function assertEquals(actualOutput, targetOutput) {
1622
console.assert(

Sprint-3/1-key-implement/3-get-card-value.js renamed to Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11-
if (rank === "A") return 11;
11+
if (rank === "A") {
12+
return 11;
13+
}
1214
}
1315

16+
// The line below allows us to load the getCardValue function into tests in other files.
17+
// This will be useful in the "rewrite tests with jest" step.
18+
module.exports = getCardValue;
19+
1420
// You need to write assertions for your function to check it works in different cases
1521
// we're going to use this helper function to make our assertions easier to read
1622
// if the actual output matches the target output, the test will pass

Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js renamed to Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const getAngleType = require("./1-get-angle-type");
1+
// This statement loads the getAngleType function you wrote in the implement directory.
2+
// We will use the same function, but write tests for it using Jest in this file.
3+
const getAngleType = require("../implement/1-get-angle-type");
24

35
test("should identify right angle (90°)", () => {
46
expect(getAngleType(90)).toEqual("Right angle");

Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js renamed to Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const isProperFraction = require("./2-is-proper-fraction");
1+
// This statement loads the isProperFraction function you wrote in the implement directory.
2+
// We will use the same function, but write tests for it using Jest in this file.
3+
const isProperFraction = require("../implement/2-is-proper-fraction");
24

35
test("should return true for a proper fraction", () => {
46
expect(isProperFraction(2, 3)).toEqual(true);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This statement loads the getCardValue function you wrote in the implement directory.
2+
// We will use the same function, but write tests for it using Jest in this file.
3+
const getCardValue = require("../implement/3-get-card-value");
4+
5+
test("should return 11 for Ace of Spades", () => {
6+
const aceofSpades = getCardValue("A♠");
7+
expect(aceofSpades).toEqual(11);
8+
});
9+
10+
// Case 2: Handle Number Cards (2-10):
11+
// Case 3: Handle Face Cards (J, Q, K):
12+
// Case 4: Handle Ace (A):
13+
// Case 5: Handle Invalid Cards:

Sprint-3/2-mandatory-rewrite/1-get-angle-type.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

Sprint-3/2-mandatory-rewrite/3-get-card-value.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)