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/3/es/part3a.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -982,7 +982,7 @@ app.use(requestLogger)
982
982
983
983
Recuerda, las funciones middleware se llaman en el orden en el que son encontradas por el motor de JavaScript. Ten en cuenta que _json-parser_ se encuentra definido antes que _requestLogger_, porque de lo contrario, ¡<i>request.body</i> no se inicializará cuando se ejecute el logger!
984
984
985
-
Las funciones de middleware deben utilizarse antes que las rutas cuando queremos que sean ejecutadas por los controladores de eventos de ruta. A veces, queremos usar funciones de middleware después que las rutas. Hacemos esto cuando las funciones de middleware solo son llamadas si ninguna ruta maneja la solicitud HTTP.
985
+
Las funciones de middleware deben utilizarse antes que las rutas cuando queremos que sean ejecutadas por los controladores de eventos de ruta. A veces, queremos usar funciones de middleware después que las rutas. Hacemos esto cuando las funciones de middleware solo son llamadas si ningún controlador de ruta se encarga de la solicitud HTTP.
986
986
987
987
Agreguemos el siguiente middleware después de nuestras rutas, que se usa para capturar solicitudes realizadas a rutas inexistentes. Para estas solicitudes, el middleware devolverá un mensaje de error en formato JSON.
Copy file name to clipboardExpand all lines: src/content/4/en/part4.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,6 @@ lang: en
9
9
In this part, we will continue our work on the backend. Our first major theme will be writing unit and integration tests for the backend. After we have covered testing, we will take a look at implementing user authentication and authorization.
10
10
11
11
<i>Part updated 13th Feb 2024</i>
12
-
- <i>Jest replaced with Node embedded testrunner</i>
12
+
- <i>Jest replaced with Node embedded test runner</i>
Copy file name to clipboardExpand all lines: src/content/4/en/part4a.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ Let's continue our work on the backend of the notes application we started in [p
11
11
12
12
### Project structure
13
13
14
-
**Note** this course material was written with version v20.11.0 of Node.js. Please make sure that your version of Node is at least as new as the version used in the material (you can check the version by running node -v in the command line).
14
+
**Note**: this course material was written with version v20.11.0 of Node.js. Please make sure that your version of Node is at least as new as the version used in the material (you can check the version by running _node -v_ in the command line).
15
15
16
16
Before we move into the topic of testing, we will modify the structure of our project to adhere to Node.js best practices.
17
17
@@ -407,7 +407,7 @@ The nature of VS Code bleeding into how you write your code is probably not idea
407
407
408
408
### Exercises 4.1.-4.2.
409
409
410
-
**Note** this course material was written with version v20.11.0 of Node.js. Please make sure that your version of Node is at least as new as the version used in the material (you can check the version by running node -v in the command line).
410
+
**Note**: this course material was written with version v20.11.0 of Node.js. Please make sure that your version of Node is at least as new as the version used in the material (you can check the version by running _node -v_ in the command line).
411
411
412
412
In the exercises for this part, we will be building a <i>blog list application</i>, that allows users to save information about interesting blogs they have stumbled across on the internet. For each listed blog we will save the author, title, URL, and amount of upvotes from users of the application.
413
413
@@ -569,7 +569,7 @@ In the next row, the test file imports the function to be tested and assigns it
Individual test cases are defined with the _test_ function. The first parameter of the function is the test description as a string. The second parameter is a <i>function</i>, that defines the functionality for the test case. The functionality for the second test case looks like this:
572
+
Individual test cases are defined with the _test_ function. The first argument of the function is the test description as a string. The second argument is a <i>function</i>, that defines the functionality for the test case. The functionality for the second test case looks like this:
573
573
574
574
```js
575
575
() => {
@@ -601,7 +601,7 @@ Running this test results in the following error message:
601
601
602
602

603
603
604
-
Let output from the npm test with _average_ function, into a new file <i>tests/average.test.js</i>.
604
+
Let's put the tests for the _average_ function, into a new file called <i>tests/average.test.js</i>.
Let's create a collection of helper functions that are best suited for working with the described sections of the blog list. Create the functions into a file called <i>utils/list_helper.js</i>. Write your tests into an appropriately named test file under the <i>tests</i> directory.
676
+
Let's create a collection of helper functions that are best suited for working with the describe sections of the blog list. Create the functions into a file called <i>utils/list_helper.js</i>. Write your tests into an appropriately named test file under the <i>tests</i> directory.
Copy file name to clipboardExpand all lines: src/content/4/en/part4b.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -231,7 +231,7 @@ test('the first note is about HTTP methods', async () => {
231
231
constresponse=awaitapi.get('/api/notes')
232
232
233
233
constcontents=response.body.map(e=>e.content)
234
-
// is the parameter truthy
234
+
// is the argument truthy
235
235
assert(contents.includes('HTML is easy'))
236
236
})
237
237
```
@@ -275,7 +275,7 @@ module.exports = {
275
275
276
276
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.
277
277
278
-
Our tests are already using the[after](https://nodejs.org/api/test.html#afterfn-options) function of to close the connection to the database after the tests are finished executing. The library Node:test offers many other functions that can be used for executing operations once before any test is run or every time before a test is run.
278
+
Our tests are already using the[after](https://nodejs.org/api/test.html#afterfn-options) function of to close the connection to the database after the tests are finished executing. The library node:test offers many other functions that can be used for executing operations once before any test is run or every time before a test is run.
279
279
280
280
Let's initialize the database <i>before every test</i> with the [beforeEach](https://nodejs.org/api/test.html#beforeeachfn-options) function:
281
281
@@ -337,7 +337,7 @@ test('the first note is about HTTP methods', async () => {
337
337
338
338
The _npm test_ command executes all of the tests for the application. When we are writing tests, it is usually wise to only execute one or two tests.
339
339
340
-
There are a few different ways of accomplishing this, one of which is the [only](https://nodejs.org/api/test.html#testonlyname-options-fn) method. With the method we can define in the code what tests should be executed:
340
+
There are a few different ways of accomplishing this, one of which is the [only](https://nodejs.org/api/test.html#testonlyname-options-fn) method. With this method we can define in the code what tests should be executed:
341
341
342
342
```js
343
343
test.only('notes are returned as json', async () => {
@@ -354,31 +354,31 @@ test.only('there are two notes', async () => {
354
354
})
355
355
```
356
356
357
-
When tests are run with option _--test-only_, that is, with the command
357
+
When tests are run with option _--test-only_, that is, with the command:
358
358
359
359
```
360
360
npm test -- --test-only
361
361
```
362
362
363
363
only the _only_ marked tests are executed.
364
364
365
-
The danger of the _only_ is that one forgets to remove those from the code.
365
+
The danger of _only_ is that one forgets to remove those from the code.
366
366
367
-
Another option is to specify the tests that need to be run as parameters of the <i>npm test</i> command.
367
+
Another option is to specify the tests that need to be run as arguments of the <i>npm test</i> command.
368
368
369
369
The following command only runs the tests found in the <i>tests/note_api.test.js</i> file:
370
370
371
371
```js
372
372
npm test -- tests/note_api.test.js
373
373
```
374
374
375
-
The [--tests-by-name-pattern](https://nodejs.org/api/test.html#filtering-tests-by-name)option can be used for running tests with a specific name:
375
+
The [--tests-by-name-pattern](https://nodejs.org/api/test.html#filtering-tests-by-name) option can be used for running tests with a specific name:
376
376
377
377
```js
378
378
npm test ----test-name-pattern="the first note is about HTTP methods"
379
379
```
380
380
381
-
The provided parameter can refer to the name of the test or the describe block. The parameter can also contain just a part of the name. The following command will run all of the tests that contain <i>notes</i> in their name:
381
+
The provided argument can refer to the name of the test or the describe block. It can also contain just a part of the name. The following command will run all of the tests that contain <i>notes</i> in their name:
382
382
383
383
```js
384
384
npm run test ----test-name-pattern="notes"
@@ -960,7 +960,7 @@ beforeEach(async () => {
960
960
961
961
The solution is quite advanced despite its compact appearance. The _noteObjects_ variable is assigned to an array of Mongoose objects that are created with the _Note_ constructor for each of the notes in the _helper.initialNotes_ array. The next line of code creates a new array that <i>consists of promises</i>, that are created by calling the _save_ method of each item in the _noteObjects_ array. In other words, it is an array of promises for saving each of the items to the database.
962
962
963
-
The [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) method can be used for transforming an array of promises into a single promise, that will be <i>fulfilled</i> once every promise in the array passed to it as a parameter is resolved. The last line of code <em>await Promise.all(promiseArray)</em> waits until every promise for saving a note is finished, meaning that the database has been initialized.
963
+
The [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) method can be used for transforming an array of promises into a single promise, that will be <i>fulfilled</i> once every promise in the array passed to it as an argument is resolved. The last line of code <em>await Promise.all(promiseArray)</em> waits until every promise for saving a note is finished, meaning that the database has been initialized.
964
964
965
965
> The returned values of each promise in the array can still be accessed when using the Promise.all method. If we wait for the promises to be resolved with the _await_ syntax <em>const results = await Promise.all(promiseArray)</em>, the operation will return an array that contains the resolved values for each promise in the _promiseArray_, and they appear in the same order as the promises in the array.
966
966
@@ -1183,7 +1183,7 @@ after(async () => {
1183
1183
1184
1184
The test output in the console is grouped according to the <i>describe</i> blocks:
The [populate](http://mongoosejs.com/docs/populate.html) method is chained after the <i>find</i> method making the initial query. The parameter given to the populate method defines that the <i>ids</i> referencing <i>note</i> objects in the <i>notes</i> field of the <i>user</i> document will be replaced by the referenced <i>note</i> documents.
494
+
The [populate](http://mongoosejs.com/docs/populate.html) method is chained after the <i>find</i> method making the initial query. The argument given to the populate method defines that the <i>ids</i> referencing <i>note</i> objects in the <i>notes</i> field of the <i>user</i> document will be replaced by the referenced <i>note</i> documents.
495
495
496
496
The result is almost exactly what we wanted:
497
497
498
498

499
499
500
-
We can use the populate parameter for choosing the fields we want to include from the documents. In addition to the field <i>id</i> we are now only interested in <i>content</i> and <i>important</i>.
500
+
We can use the populate method for choosing the fields we want to include from the documents. In addition to the field <i>id</i> we are now only interested in <i>content</i> and <i>important</i>.
501
501
502
502
The selection of fields is done with the Mongo [syntax](https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/#return-the-specified-fields-and-the-_id-field-only):
Copy file name to clipboardExpand all lines: src/content/4/en/part4d.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -393,7 +393,7 @@ The operation must respond with a suitable status code and some kind of an error
393
393
394
394
Also, **implement tests** that ensure invalid users are not created and that an invalid add user operation returns a suitable status code and error message.
395
395
396
-
**NB** if you decide to define tests on multiple files, you should note that by default each test file is executed in its own process (see _Test execution model_ in the [documentation](https://nodejs.org/api/test.html)). The consequence of this is that different test files are executed at the same time. Since the tests share the same database, simultaneous execution may cause problems. Problems are avoided by executing the tests with the option _--test-concurrency=1_, i.e. defining them to be executed sequentially.
396
+
**NB** if you decide to define tests on multiple files, you should note that by default each test file is executed in its own process (see _Test execution model_ in the [documentation](https://nodejs.org/api/test.html#test-runner-execution-model)). The consequence of this is that different test files are executed at the same time. Since the tests share the same database, simultaneous execution may cause problems, which can be avoided by executing the tests with the option _--test-concurrency=1_, i.e. defining them to be executed sequentially.
As can be seen, this happens by chaining multiple middlewares as the parameter of function <i>use</i>. It would also be possible to register a middleware only for a specific operation:
523
+
As can be seen, this happens by chaining multiple middlewares as the arguments of the function <i>use</i>. It would also be possible to register a middleware only for a specific operation:
Copy file name to clipboardExpand all lines: src/content/4/es/part4.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ lang: es
8
8
9
9
En esta parte, continuaremos nuestro trabajo en el backend. Nuestro primer tema principal será escribir pruebas de unidad e integración para el backend. Una vez que hayamos cubierto las pruebas, analizaremos la implementación de la autenticación y autorización de usuario.
10
10
11
-
<i>Parte actualizada el 22 de enero de 2023</i>
12
-
- <i>No hay cambios importantes</i>
11
+
<i>Parte actualizada el 13 de Febrero de 2024</i>
12
+
- <i>Jest reemplazado con el test runner integrado de Node</i>
0 commit comments