Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Commit 99455f4

Browse files
Merge pull request #1 from Fatemeh-sadat-Torabi/test-tweaks
Test tweaks
2 parents 5ae7fb4 + e2d2a92 commit 99455f4

File tree

5 files changed

+168
-96
lines changed

5 files changed

+168
-96
lines changed

README.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
1-
Like learning a musical instrument, programming requires daily practise.
1+
# Coursework
22

3-
The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson.
3+
Like learning a musical instrument, programming requires daily practice.
4+
5+
The exercises are split into two folders: `mandatory` and `extra`. All homework in the `mandatory` section **must** be completed for homework by the following lesson.
46

57
The `extra` folder contains exercises that you can complete to challenge yourself, but are not required for the following lesson.
68

9+
If you think you need to do more practice with the basics, then you can find some more exercises listed in `resources.md`. These exercises are not mandatory and won't be assigned as part of your coursework for the week.
10+
11+
## Setting up your code editor
12+
13+
14+
There are some tools that will help you to write code. One of these, [Prettier](https://prettier.io/), formats your code, making it easier for you and others to read.
15+
16+
### 1. Install prettier
17+
18+
- In Visual Studio open the extensions panel (see https://code.visualstudio.com/docs/editor/extension-gallery#_browse-and-install-extensions)
19+
- Search for `Prettier - Code formatter`
20+
- Click install on the top result
21+
22+
### 2. Enable formatting on save
23+
24+
- In Visual Studio open the settings file (see https://code.visualstudio.com/docs/getstarted/settings#_creating-user-and-workspace-settings)
25+
- Search for `editor format`
26+
- Set `editor.formatOnSave` and `editor.formatOnPaste` to true
27+
28+
29+
## Running the code/tests
30+
31+
The files for the mandatory/extra exercises are intended to be run as jest tests.
32+
33+
- Once you have cloned the repository, run `npm install` once in the terminal to install jest (and any necessary dependencies).
34+
- To run the tests for all mandatory/extra exercises, run `npm test`
35+
- To run only the tests for the mandatory exercises, run `npm test -- --selectProjects mandatory`
36+
- To run only the tests for the extra exercises, run `npm test -- --selectProjects extra`
37+
- To run a single exercise/test (for example `mandatory/1-writer.js`), run `npm test -- --testPathPattern mandatory/1-writer.js` (Remember, you can use tab-completion to get files relative to the current directory, so m`Tab ↹`/1-`Tab ↹` will autocomplete get you the test file starting with 1-)
38+
39+
For more information about tests, look here:
40+
41+
https://syllabus.codeyourfuture.io/guides/intro-to-tests
42+
43+
Try out variant way of running tests:
44+
45+
- `npm test` -> run all mandatory and extra tests
46+
- `npm test -- --selectProjects mandatory` -> run only mandatory tests
47+
- `npm test -- --testPathPattern mandatory/1-syntax-errors.js` -> run single test
48+
749
## Solutions
850

951
The solutions for this coursework can be found here:
@@ -12,15 +54,9 @@ https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week2-Solution
1254

1355
This is a **private** repository. Please request access from your Teachers, Buddy or City Coordinator after the start of your next lesson.
1456

15-
## Testing your work
16-
17-
- Each of the *.js files in the `exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, `node exercises/B-boolean-literals/exercise.js` can be run from the root of the project.
18-
- To run the tests in the `mandatory` folder, run `npm run test` from the root of the project (after having run `npm install` once before).
19-
- To run the tests in the `extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before).
20-
2157
## Instructions for submission
2258

23-
For your homework, we'll be using [**test driven development**](https://medium.com/@adityaalifnugraha/test-driven-development-tdd-in-a-nutshell-b9e05dfe8adb) to check your answers. Test driven development (or TDD) is the practice of writing tests for your code first, and then write your code to pass those tests. This is a very useful way of writing good quality code and is used in a lot of industries. You don't have to worry about knowing how this works, but if you're curious, engage with a volunteer to find out more! :)
59+
For your homework, we'll be using tests to check your answers. The tests will assert that your functions behave in the correct way. A **failing test** ❌ indicates that a function _is not_ behaving correctly; a **passing test** ✅ indicates that your function is behaving correctly.
2460

2561
1. Complete the challenges in each file and save it once you're happy with your changes
2662
2. Run the script to check the results against the tests - all tests should read PASSED if you completed the challenges correctly. If a test reads FAILED, find the associated test to identify which function failed and fix it.

extra/1-factorial.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
*/
1010

1111
function factorial(input) {
12-
// TODO
12+
// TODO
1313
}
1414

1515
/* ======= TESTS - DO NOT MODIFY ===== */
1616

17-
test("3! should be 6", () => {
17+
describe("factorial", () => {
18+
test("3! should be 6", () => {
1819
expect(factorial(3)).toEqual(6);
19-
});
20+
});
2021

21-
test("5! should be 120", () => {
22+
test("5! should be 120", () => {
2223
expect(factorial(5)).toEqual(120);
23-
});
24+
});
2425

25-
test("10! should be 3628800", () => {
26+
test("10! should be 3628800", () => {
2627
expect(factorial(10)).toEqual(3628800);
28+
});
2729
});

mandatory/1-fix-functions.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
*/
1212

13-
function mood() {
13+
function getMood() {
1414
let isHappy = true;
1515

1616
if (isHappy) {
@@ -32,26 +32,30 @@ function greaterThan10(num) {
3232

3333
/* ======= TESTS - DO NOT MODIFY ===== */
3434

35-
test("mood function works for true", () => {
36-
expect(mood(true)).toEqual("I am happy");
37-
});
35+
describe("getMood", () => {
36+
test("getMood function works for true", () => {
37+
expect(getMood(true)).toEqual("I am happy");
38+
});
3839

39-
test("mood function works for false", () => {
40-
expect(mood(false)).toEqual("I am not happy");
40+
test("getMood function works for false", () => {
41+
expect(getMood(false)).toEqual("I am not happy");
42+
});
4143
});
4244

43-
test("greaterThanTen function works for value greater than 10", () => {
44-
expect(greaterThan10(11)).toEqual("num is greater than 10");
45-
});
45+
describe("greaterThanTen", () => {
46+
test("works for value greater than 10", () => {
47+
expect(greaterThan10(11)).toEqual("num is greater than 10");
48+
});
4649

47-
test("greaterThanTen function works for value much greater than 10", () => {
48-
expect(greaterThan10(96)).toEqual("num is greater than 10");
49-
});
50+
test("works for value much greater than 10", () => {
51+
expect(greaterThan10(96)).toEqual("num is greater than 10");
52+
});
5053

51-
test("greaterThanTen function works for value less than 10", () => {
52-
expect(greaterThan10(9)).toEqual("num is not big enough");
53-
});
54+
test("works for value less than 10", () => {
55+
expect(greaterThan10(9)).toEqual("num is not big enough");
56+
});
5457

55-
test("greaterThanTen function works for value equal to 10", () => {
56-
expect(greaterThan10(10)).toEqual("num is not big enough");
58+
test("works for value equal to 10", () => {
59+
expect(greaterThan10(10)).toEqual("num is not big enough");
60+
});
5761
});

mandatory/2-function-creation.js

Lines changed: 77 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -49,93 +49,108 @@ function countReverse(number) {}
4949

5050
/* ======= TESTS - DO NOT MODIFY ===== */
5151

52-
test("isAcceptableUser function returns true if over 18 and logged in", () => {
53-
expect(isAcceptableUser(21, true)).toEqual(true);
54-
});
52+
describe("isAcceptableUser", () => {
53+
test("returns true if over 18 and logged in", () => {
54+
expect(isAcceptableUser(21, true)).toEqual(true);
55+
});
5556

56-
test("isAcceptableUser function returns true if 18 and logged in", () => {
57-
expect(isAcceptableUser(18, true)).toEqual(true);
58-
});
57+
test("returns true if 18 and logged in", () => {
58+
expect(isAcceptableUser(18, true)).toEqual(true);
59+
});
5960

60-
test("isAcceptableUser function returns false if under 18 and logged in", () => {
61-
expect(isAcceptableUser(17, true)).toEqual(false);
62-
});
61+
test("returns false if under 18 and logged in", () => {
62+
expect(isAcceptableUser(17, true)).toEqual(false);
63+
});
6364

64-
test("isAcceptableUser function returns false if over 18 and not logged in", () => {
65-
expect(isAcceptableUser(21, false)).toEqual(false);
65+
test("returns false if over 18 and not logged in", () => {
66+
expect(isAcceptableUser(21, false)).toEqual(false);
67+
});
6668
});
6769

68-
test("applyDiscount function returns price with 5% discount", () => {
69-
expect(applyDiscount(120)).toEqual(114);
70-
});
70+
describe("applyDiscount", () => {
71+
test("returns price with 5% discount", () => {
72+
expect(applyDiscount(120)).toEqual(114);
73+
});
7174

72-
test("applyDiscount function returns price with 10% discount", () => {
73-
expect(applyDiscount(280)).toEqual(252);
75+
test("returns price with 10% discount", () => {
76+
expect(applyDiscount(280)).toEqual(252);
77+
});
7478
});
7579

76-
test("printOddNumbers function prints odd numbers between 1 and 2", () => {
77-
expectprintOddNumbersToLog([1], 2);
78-
});
80+
describe("printOddNumbers", () => {
81+
test("printOddNumbers function prints odd numbers between 1 and 2", () => {
82+
const consoleLogSpy = jest.spyOn(console, "log");
83+
printOddNumbers(2);
84+
85+
expect(consoleLogSpy).toBeCalledWith(1);
86+
consoleLogSpy.mockRestore();
87+
});
88+
89+
test("printOddNumbers function prints odd numbers between 1 and 10", () => {
90+
const consoleLogSpy = jest.spyOn(console, "log");
7991

80-
test("printOddNumbers function prints odd numbers between 1 and 10", () => {
81-
expectprintOddNumbersToLog([1, 3, 5, 7, 9], 10);
92+
printOddNumbers(10);
93+
expect(consoleLogSpy).toBeCalledWith(1);
94+
expect(consoleLogSpy).toBeCalledWith(3);
95+
expect(consoleLogSpy).toBeCalledWith(5);
96+
expect(consoleLogSpy).toBeCalledWith(7);
97+
expect(consoleLogSpy).toBeCalledWith(9);
98+
99+
consoleLogSpy.mockRestore();
100+
});
82101
});
83102

84-
function expectprintOddNumbersToLog(expectedValues, limit) {
85-
const consoleLogSpy = jest.spyOn(console, "log");
86-
printOddNumbers(limit);
87-
expect(consoleLogSpy).toBeCalledTimes(expectedValues.length);
88-
expectedValues.forEach((value, i) => {
89-
expect(consoleLogSpy).nthCalledWith(i + 1, value);
103+
describe("buyTwoGetTheCheapestFree", () => {
104+
test("buyTwoGetTheCheapestFree function returns first price when it is largest", () => {
105+
expect(buyTwoGetTheCheapestFree(700, 500)).toEqual(700);
90106
});
91-
consoleLogSpy.mockRestore();
92-
}
93107

94-
test("buyTwoGetTheCheapestFree function returns first price when it is largest", () => {
95-
expect(buyTwoGetTheCheapestFree(700, 500)).toEqual(700);
108+
test("buyTwoGetTheCheapestFree function returns second price when it is largest", () => {
109+
expect(buyTwoGetTheCheapestFree(500, 700)).toEqual(700);
110+
});
96111
});
97112

98-
test("buyTwoGetTheCheapestFree function returns second price when it is largest", () => {
99-
expect(buyTwoGetTheCheapestFree(500, 700)).toEqual(700);
100-
});
113+
describe("canRegister", () => {
114+
test("returns in case of a person of age 7", () => {
115+
expect(canRegister(7)).toEqual("You Are Too Young To Register");
116+
});
101117

102-
test("(canRegister function returns in case of a person of age 7", () => {
103-
expect(canRegister(7)).toEqual("You Are Too Young To Register");
104-
});
118+
test("returns in case of a person of age 12", () => {
119+
expect(canRegister(12)).toEqual("You Are Too Young To Register");
120+
});
105121

106-
test("canRegister function returns in case of a person of age 12", () => {
107-
expect(canRegister(12)).toEqual("You Are Too Young To Register");
108-
});
122+
test("returns in case of a person of age 13", () => {
123+
expect(canRegister(13)).toEqual("You Can Register");
124+
});
109125

110-
test("canRegister function returns in case of a person of age 13", () => {
111-
expect(canRegister(13)).toEqual("You Can Register");
112-
});
126+
test("returns in case of a person of age 89", () => {
127+
expect(canRegister(89)).toEqual("You Can Register");
128+
});
113129

114-
test("canRegister function returns in case of a person of age 89", () => {
115-
expect(canRegister(89)).toEqual("You Can Register");
116-
});
130+
test("returns in case of a person of age 90", () => {
131+
expect(canRegister(90)).toEqual("You Don't Need To Register");
132+
});
117133

118-
test("canRegister function returns in case of a person of age 90", () => {
119-
expect(canRegister(90)).toEqual("You Don't Need To Register");
134+
test("returns in case of a person of age 112", () => {
135+
expect(canRegister(112)).toEqual("You Don't Need To Register");
136+
});
120137
});
121138

122-
test("canRegister function returns in case of a person of age 112", () => {
123-
expect(canRegister(112)).toEqual("You Don't Need To Register");
124-
});
139+
describe("countReverse", () => {
140+
test("countReverse function logs values from 7 to 1", () => {
141+
const consoleLogSpy = jest.spyOn(console, "log");
125142

126-
test("countReverse function logs values from 7 to 1", () => {
127-
expectcountReverseToLog([7, 6, 5, 4, 3, 2, 1], 7);
128-
});
143+
expect(consoleLogSpy).toBeCalledWith(7);
144+
expect(consoleLogSpy).toBeCalledWith(6);
145+
expect(consoleLogSpy).toBeCalledWith(5);
146+
expect(consoleLogSpy).toBeCalledWith(4);
147+
expect(consoleLogSpy).toBeCalledWith(3);
148+
expect(consoleLogSpy).toBeCalledWith(2);
149+
expect(consoleLogSpy).toBeCalledWith(1);
129150

130-
function expectcountReverseToLog(expectedValues, start) {
131-
const consoleLogSpy = jest.spyOn(console, "log");
132-
countReverse(start);
133-
expect(consoleLogSpy).toBeCalledTimes(expectedValues.length);
134-
expectedValues.forEach((value, i) => {
135-
expect(consoleLogSpy).nthCalledWith(i + 1, value);
151+
consoleLogSpy.mockRestore();
136152
});
137-
consoleLogSpy.mockRestore();
138-
}
153+
});
139154

140155
/*
141156
CHECK OUT solutions.md FOR MORE INFO ON OUR SOLUTION

package.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@
44
"description": "Exercises for JS1 Week 2",
55
"license": "CC-BY-SA-4.0",
66
"scripts": {
7-
"test": "jest --testRegex='mandatory[/\\\\].*\\.js$' --testPathIgnorePatterns=playing-computer",
8-
"extra-tests": "jest --testRegex='extra[/\\\\].*\\.js$'"
7+
"test": "jest"
8+
},
9+
"jest": {
10+
"projects": [
11+
{
12+
"displayName": "mandatory",
13+
"testMatch": [
14+
"<rootDir>/mandatory/*.js"
15+
]
16+
},
17+
{
18+
"displayName": "extra",
19+
"testMatch": [
20+
"<rootDir>/extra/*.js"
21+
]
22+
}
23+
]
924
},
1025
"repository": {
1126
"type": "git",

0 commit comments

Comments
 (0)