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
+31-26Lines changed: 31 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -202,13 +202,13 @@ Let's write a few more tests:
202
202
constassert=require('node:assert')
203
203
// ...
204
204
205
-
test('there are two notes', async () => {
205
+
test('all notes are returned', async () => {
206
206
constresponse=awaitapi.get('/api/notes')
207
207
208
208
assert.strictEqual(response.body.length, 2)
209
209
})
210
210
211
-
test('the first note is about HTTP methods', async () => {
211
+
test('a specific note is within the returned notes', async () => {
212
212
constresponse=awaitapi.get('/api/notes')
213
213
214
214
constcontents=response.body.map(e=>e.content)
@@ -223,7 +223,7 @@ Both tests store the response of the request to the _response_ variable, and unl
223
223
We could simplify the second test a bit, and use the [assert](https://nodejs.org/docs/latest/api/assert.html#assertokvalue-message) itself to verify that the note is among the returned ones:
224
224
225
225
```js
226
-
test('the first note is about HTTP methods', async () => {
226
+
test('a specific note is within the returned notes', async () => {
227
227
constresponse=awaitapi.get('/api/notes')
228
228
229
229
constcontents=response.body.map(e=>e.content)
@@ -275,10 +275,15 @@ Our tests are already using the [after](https://nodejs.org/api/test.html#afterfn
275
275
Let's initialize the database <i>before every test</i> with the [beforeEach](https://nodejs.org/api/test.html#beforeeachfn-options) function:
@@ -340,7 +344,7 @@ test.only('notes are returned as json', async () => {
340
344
.expect('Content-Type',/application\/json/)
341
345
})
342
346
343
-
test.only('there are two notes', async () => {
347
+
test.only('all notes are returned', async () => {
344
348
constresponse=awaitapi.get('/api/notes')
345
349
346
350
assert.strictEqual(response.body.length, 2)
@@ -368,7 +372,7 @@ npm test -- tests/note_api.test.js
368
372
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:
369
373
370
374
```js
371
-
npm test ----test-name-pattern="the first note is about HTTP methods"
375
+
npm test ----test-name-pattern="a specific note is within the returned notes"
372
376
```
373
377
374
378
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:
@@ -582,16 +586,16 @@ The module defines the _notesInDb_ function that can be used for checking the no
582
586
Our tests can now use the helper module and be changed like this:
The reason for this is that _strictEqual_ uses the method [Object.is](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) to compare similarity, i.e. it compares whether the objects are the same. In our case, it is enough to check that the contents of the objects, i.e. the values of their fields, are the same. For this purpose _deepStrictEqual_ is suitable.
766
+
The reason for this is that _strictEqual_ uses the method [Object.is](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) to compare similarity, i.e. it compares whether the objects are the same. In our case, we want to check that the contents of the objects, i.e. the values of their fields, are the same. For this purpose _deepStrictEqual_ is suitable.
764
767
765
768
The tests pass and we can safely refactor the tested routes to use async/await:
Copy file name to clipboardExpand all lines: src/content/4/fi/osa4b.md
+32-28Lines changed: 32 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -199,13 +199,13 @@ Tehdään pari testiä lisää:
199
199
constassert=require('node:assert')
200
200
// ...
201
201
202
-
test('there are two notes', async () => {
202
+
test('all notes are returned', async () => {
203
203
constresponse=awaitapi.get('/api/notes')
204
204
205
205
assert.strictEqual(response.body.length, 2)
206
206
})
207
207
208
-
test('the first note is about HTTP methods', async () => {
208
+
test('a specific note is within the returned notes', async () => {
209
209
constresponse=awaitapi.get('/api/notes')
210
210
211
211
constcontents=response.body.map(e=>e.content)
@@ -220,7 +220,7 @@ Molemmat testit sijoittavat pyynnön vastauksen muuttujaan _response_. Toisin ku
220
220
Jälkimmäistä testiä on vielä mahdollista yksinkertaistaa hiukan tekemällä vertailu suoraan [assert](https://nodejs.org/docs/latest/api/assert.html#assertokvalue-message):illa:
221
221
222
222
```js
223
-
test('the first note is about HTTP methods', async () => {
223
+
test('a specific note is within the returned notes', async () => {
224
224
constresponse=awaitapi.get('/api/notes')
225
225
226
226
constcontents=response.body.map(e=>e.content)
@@ -273,12 +273,14 @@ Päätetään alustaa tietokanta ennen <i>jokaisen testin suoritusta</i>, eli fu
@@ -338,7 +341,7 @@ test.only('notes are returned as json', async () => {
338
341
.expect('Content-Type',/application\/json/)
339
342
})
340
343
341
-
test.only('there are two notes', async () => {
344
+
test.only('all notes are returned', async () => {
342
345
constresponse=awaitapi.get('/api/notes')
343
346
344
347
assert.strictEqual(response.body.length, 2)
@@ -364,7 +367,7 @@ npm test -- tests/note_api.test.js
364
367
Parametrin [--tests-by-name-pattern](https://nodejs.org/api/test.html#filtering-tests-by-name) avulla voidaan suorittaa testejä nimen perusteella:
365
368
366
369
```js
367
-
npm test ----test-name-pattern="the first note is about HTTP methods"
370
+
npm test ----test-name-pattern="a specific note is within the returned notes"
368
371
```
369
372
370
373
Parametri voi viitata testin tai describe-lohkon nimeen. Parametrina voidaan antaa myös nimen osa. Seuraava komento suorittaisi kaikki testit, joiden nimessä on sana <i>notes</i>:
@@ -578,15 +581,16 @@ Moduuli määrittelee funktion _notesInDb_, jonka avulla voidaan tarkastaa sovel
Syynä tälle on se, että _strictEqual_ käyttää metodia [Object.is](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) samuuden vertailuun, eli se vertaa ovatko kyseessä samat olioit. Meidän tapauksessamme taas riittää tarkistaa että olioiden sisältö, eli niiden kenttien arvot olisivat samat. Tähän tarkoitukseen sopii _deepStrictEqual_.
761
+
Syynä tälle on se, että _strictEqual_ käyttää metodia [Object.is](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) samuuden vertailuun, eli se vertaa ovatko kyseessä samat oliot. Meidän tapauksessamme taas on tarkoitus tarkistaa, että olioiden sisältö eli niiden kenttien arvot olisivat samat. Tähän tarkoitukseen sopii _deepStrictEqual_.
760
762
761
763
Testit menevät läpi, joten voimme turvallisesti refaktoroida testatut routet käyttämään async/awaitia:
0 commit comments