You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/4/en/part4b.md
+16-18Lines changed: 16 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,7 @@ We can create our separate test database in MongoDB Atlas. This is not an optima
77
77
78
78
It would be better to run our tests using a database that is installed and running on the developer's local machine. The optimal solution would be to have every test execution use a separate database. This is "relatively simple" to achieve by [running Mongo in-memory](https://docs.mongodb.com/manual/core/inmemory/) or by using [Docker](https://www.docker.com) containers. We will not complicate things and will instead continue to use the MongoDB Atlas database.
79
79
80
-
Let's make some changes to the module that defines the application's configuration:
80
+
Let's make some changes to the module that defines the application's configuration in _utils/config.js_:
81
81
82
82
```js
83
83
require('dotenv').config()
@@ -176,12 +176,11 @@ afterAll(async () => {
176
176
177
177
When running your tests you may run across the following console warning:
178
178
179
-

179
+

180
180
181
181
The problem is quite likely caused by the Mongoose version 6.x, the problem does not appear when version 5.x or 7.x is used. [Mongoose documentation](https://mongoosejs.com/docs/jest.html) does not recommend testing Mongoose applications with Jest.
182
182
183
-
[One way](https://stackoverflow.com/questions/50687592/jest-and-mongoose-jest-has-detected-opened-handles) to get rid of this is to
184
-
add to the directory <i>tests</i> a file <i>teardown.js</i> with the following content
183
+
[One way](https://stackoverflow.com/questions/50687592/jest-and-mongoose-jest-has-detected-opened-handles) to get rid of this is to add to the directory <i>tests</i> a file <i>teardown.js</i> with the following content
185
184
186
185
```js
187
186
module.exports= () => {
@@ -209,7 +208,7 @@ test('notes are returned as json', async () => {
209
208
.get('/api/notes')
210
209
.expect(200)
211
210
.expect('Content-Type',/application\/json/)
212
-
}, 100000)
211
+
}, 100000)// highlight-line
213
212
```
214
213
215
214
This third parameter sets the timeout to 100000 ms. A long timeout ensures that our test won't fail due to the time it takes to run. (A long timeout may not be what you want for tests based on performance or speed, but this is fine for our example tests).
@@ -302,7 +301,7 @@ module.exports = {
302
301
303
302
### Initializing the database before tests
304
303
305
-
Testing appears to be easy and our tests are currently passing. However, our tests are bad as they are dependent on the state of the database, that now happens to have two notes. To make our tests more robust, we have to reset the database and generate the needed test data in a controlled manner before we run the tests.
304
+
Testing appears to be easy and our tests are currently passing. However, our tests are bad as they are dependent on the state of the database, that now happens to have two notes. To make them more robust, we have to reset the database and generate the needed test data in a controlled manner before we run the tests.
306
305
307
306
Our tests are already using the [afterAll](https://jestjs.io/docs/api#afterallfn-timeout) function of Jest to close the connection to the database after the tests are finished executing. Jest offers many other [functions](https://jestjs.io/docs/setup-teardown) that can be used for executing operations once before any test is run or every time before a test is run.
308
307
@@ -1002,13 +1001,13 @@ Full stack development is <i> extremely hard</i>, that is why I will use all the
1002
1001
1003
1002
- I will have my browser developer console open all the time
1004
1003
- I will use the network tab of the browser dev tools to ensure that frontend and backend are communicating as I expect
1005
-
- I will constantly keep an eye on the state of the server to make sure that the data sent there by the frontend is saved there as I expect
1004
+
- I will constantly keep an eye on the state of the server to make sure that the data sent there by the frontend is saved as I expect
1006
1005
- I will keep an eye on the database: does the backend save data there in the right format
1007
1006
- I will progress in small steps
1008
1007
- <i>I will write lots of _console.log_ statements to make sure I understand how the code and the tests behave and to help pinpoint problems</i>
1009
1008
- If my code does not work, I will not write more code. Instead, I start deleting the code until it works or just return to a state when everything was still working
1010
1009
- <i>If a test does not pass, I make sure that the tested functionality for sure works in the application</i>
1011
-
- When I ask for help in the course Discord or Telegram channel or elsewhere I formulate my questions properly, see [here](https://fullstackopen.com/en/part0/general_info#how-to-get-help-in-discord-telegram) how to ask for help
1010
+
- When I ask for help in the course Discord or Telegram channel or elsewhere I formulate my questions properly, see [here](/en/part0/general_info#how-to-get-help-in-discord-telegram) how to ask for help
1012
1011
1013
1012
</div>
1014
1013
@@ -1020,7 +1019,7 @@ Full stack development is <i> extremely hard</i>, that is why I will use all the
1020
1019
1021
1020
**Warning:** If you find yourself using async/await and <i>then</i> methods in the same code, it is almost guaranteed that you are doing something wrong. Use one or the other and don't mix the two.
1022
1021
1023
-
#### 4.8: Blog list tests, step1
1022
+
#### 4.8: Blog List Tests, step 1
1024
1023
1025
1024
Use the supertest package for writing a test that makes an HTTP GET request to the <i>/api/blogs</i> URL. Verify that the blog list application returns the correct amount of blog posts in the JSON format.
1026
1025
@@ -1032,8 +1031,7 @@ Notice that you will have to make similar changes to the code that were made [in
1032
1031
1033
1032

1034
1033
1035
-
[One way](https://stackoverflow.com/questions/50687592/jest-and-mongoose-jest-has-detected-opened-handles) to get rid of this is to
1036
-
add to the <i>tests</i> directory a file <i>teardown.js</i> with the following content
1034
+
[One way](https://stackoverflow.com/questions/50687592/jest-and-mongoose-jest-has-detected-opened-handles) to get rid of this is to add to the <i>tests</i> directory a file <i>teardown.js</i> with the following content
1037
1035
1038
1036
```js
1039
1037
module.exports= () => {
@@ -1053,21 +1051,21 @@ and by extending the Jest definitions in the <i>package.json</i> as follows
1053
1051
}
1054
1052
```
1055
1053
1056
-
**NB:** when you are writing your tests **<i>it is better to not execute all of your tests</i>**, only execute the ones you are working on. Read more about this [here](/en/part4/testing_the_backend#running-tests-one-by-one).
1054
+
**NB:** when you are writing your tests **<i>it is better to not execute them all</i>**, only execute the ones you are working on. Read more about this [here](/en/part4/testing_the_backend#running-tests-one-by-one).
1057
1055
1058
-
#### 4.9: Blog list tests, step2
1056
+
#### 4.9: Blog List Tests, step 2
1059
1057
1060
1058
Write a test that verifies that the unique identifier property of the blog posts is named <i>id</i>, by default the database names the property <i>_id</i>. Verifying the existence of a property is easily done with Jest's [toBeDefined](https://jestjs.io/docs/en/expect#tobedefined) matcher.
1061
1059
1062
1060
Make the required changes to the code so that it passes the test. The [toJSON](/en/part3/saving_data_to_mongo_db#connecting-the-backend-to-a-database) method discussed in part 3 is an appropriate place for defining the <i>id</i> parameter.
1063
1061
1064
-
#### 4.10: Blog list tests, step3
1062
+
#### 4.10: Blog List Tests, step 3
1065
1063
1066
1064
Write a test that verifies that making an HTTP POST request to the <i>/api/blogs</i> URL successfully creates a new blog post. At the very least, verify that the total number of blogs in the system is increased by one. You can also verify that the content of the blog post is saved correctly to the database.
1067
1065
1068
1066
Once the test is finished, refactor the operation to use async/await instead of promises.
1069
1067
1070
-
#### 4.11*: Blog list tests, step4
1068
+
#### 4.11*: Blog List Tests, step4
1071
1069
1072
1070
Write a test that verifies that if the <i>likes</i> property is missing from the request, it will default to the value 0. Do not test the other properties of the created blogs yet.
1073
1071
@@ -1223,7 +1221,7 @@ afterAll(async () => {
1223
1221
})
1224
1222
```
1225
1223
1226
-
The test output is grouped according to the <i>describe</i> blocks:
1224
+
The test output in the console is grouped according to the <i>describe</i> blocks:
0 commit comments