Skip to content

Commit 86cdd4f

Browse files
committed
Make part4 code examples clearer and more consistent
1 parent 621217a commit 86cdd4f

File tree

2 files changed

+63
-54
lines changed

2 files changed

+63
-54
lines changed

src/content/4/en/part4b.md

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ Let's write a few more tests:
202202
const assert = require('node:assert')
203203
// ...
204204

205-
test('there are two notes', async () => {
205+
test('all notes are returned', async () => {
206206
const response = await api.get('/api/notes')
207207

208208
assert.strictEqual(response.body.length, 2)
209209
})
210210

211-
test('the first note is about HTTP methods', async () => {
211+
test('a specific note is within the returned notes', async () => {
212212
const response = await api.get('/api/notes')
213213

214214
const contents = response.body.map(e => e.content)
@@ -223,7 +223,7 @@ Both tests store the response of the request to the _response_ variable, and unl
223223
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:
224224

225225
```js
226-
test('the first note is about HTTP methods', async () => {
226+
test('a specific note is within the returned notes', async () => {
227227
const response = await api.get('/api/notes')
228228

229229
const contents = response.body.map(e => e.content)
@@ -275,10 +275,15 @@ Our tests are already using the [after](https://nodejs.org/api/test.html#afterfn
275275
Let's initialize the database <i>before every test</i> with the [beforeEach](https://nodejs.org/api/test.html#beforeeachfn-options) function:
276276

277277
```js
278-
// highlight-start
279-
const { test, after, beforeEach } = require('node:test')
280-
const Note = require('../models/note')
281-
// highlight-end
278+
279+
const assert = require('node:assert')
280+
const { test, after, beforeEach } = require('node:test') // highlight-line
281+
const mongoose = require('mongoose')
282+
const supertest = require('supertest')
283+
const app = require('../app')
284+
const Note = require('../models/note') // highlight-line
285+
286+
const api = supertest(app)
282287

283288
// highlight-start
284289
const initialNotes = [
@@ -293,8 +298,6 @@ const initialNotes = [
293298
]
294299
// highlight-end
295300

296-
// ...
297-
298301
// highlight-start
299302
beforeEach(async () => {
300303
await Note.deleteMany({})
@@ -306,6 +309,7 @@ beforeEach(async () => {
306309
await noteObject.save()
307310
})
308311
// highlight-end
312+
309313
// ...
310314
```
311315

@@ -316,7 +320,7 @@ Let's modify the test that checks the number of notes as follows:
316320
```js
317321
// ...
318322

319-
test('there are two notes', async () => {
323+
test('all notes are returned', async () => {
320324
const response = await api.get('/api/notes')
321325

322326
assert.strictEqual(response.body.length, initialNotes.length) // highlight-line
@@ -340,7 +344,7 @@ test.only('notes are returned as json', async () => {
340344
.expect('Content-Type', /application\/json/)
341345
})
342346

343-
test.only('there are two notes', async () => {
347+
test.only('all notes are returned', async () => {
344348
const response = await api.get('/api/notes')
345349

346350
assert.strictEqual(response.body.length, 2)
@@ -368,7 +372,7 @@ npm test -- tests/note_api.test.js
368372
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:
369373

370374
```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"
372376
```
373377

374378
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
582586
Our tests can now use the helper module and be changed like this:
583587

584588
```js
585-
const { test, after, beforeEach } = require('node:test')
586589
const assert = require('node:assert')
587-
const supertest = require('supertest')
590+
const { test, after, beforeEach } = require('node:test')
588591
const mongoose = require('mongoose')
589-
const helper = require('./test_helper') // highlight-line
592+
const supertest = require('supertest')
590593
const app = require('../app')
591-
const api = supertest(app)
592-
594+
const helper = require('./test_helper') // highlight-line
593595
const Note = require('../models/note')
594596

597+
const api = supertest(app)
598+
595599
beforeEach(async () => {
596600
await Note.deleteMany({})
597601

@@ -610,17 +614,16 @@ test('notes are returned as json', async () => {
610614
})
611615

612616
test('all notes are returned', async () => {
613-
const response = await api.get('/api/notes')
617+
const notes = await helper.notesInDb() // highlight-line
614618

615-
assert.strictEqual(response.body.length, helper.initialNotes.length) // highlight-line
619+
assert.strictEqual(notes.length, helper.initialNotes.length) // highlight-line
616620
})
617621

618622
test('a specific note is within the returned notes', async () => {
619-
const response = await api.get('/api/notes')
623+
const notes = await helper.notesInDb() // highlight-line
620624

621-
const contents = response.body.map(r => r.content)
622-
623-
assert(contents.includes('Browser can execute only JavaScript'))
625+
const contents = notes.map(n => n.content)
626+
assert(contents.includes('HTML is easy'))
624627
})
625628

626629
test('a valid note can be added ', async () => {
@@ -760,7 +763,7 @@ There is one point worth noting in the first test. Instead of the previously use
760763
assert.deepStrictEqual(resultNote.body, noteToView)
761764
```
762765

763-
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.
764767

765768
The tests pass and we can safely refactor the tested routes to use async/await:
766769

@@ -777,12 +780,14 @@ notesRouter.get('/:id', async (request, response, next) => {
777780
next(exception)
778781
}
779782
})
783+
```
780784

785+
```js
781786
notesRouter.delete('/:id', async (request, response, next) => {
782787
try {
783788
await Note.findByIdAndDelete(request.params.id)
784789
response.status(204).end()
785-
} catch(exception) {
790+
} catch (exception) {
786791
next(exception)
787792
}
788793
})
@@ -1079,7 +1084,7 @@ describe('when there are some notes saved initially', () => {
10791084
const response = await api.get('/api/notes')
10801085

10811086
const contents = response.body.map(r => r.content)
1082-
assert(contents.includes('Browser can execute only JavaScript'))
1087+
assert(contents.includes('HTML is easy'))
10831088
})
10841089

10851090
describe('viewing a specific note', () => {

src/content/4/fi/osa4b.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ Tehdään pari testiä lisää:
199199
const assert = require('node:assert')
200200
// ...
201201

202-
test('there are two notes', async () => {
202+
test('all notes are returned', async () => {
203203
const response = await api.get('/api/notes')
204204

205205
assert.strictEqual(response.body.length, 2)
206206
})
207207

208-
test('the first note is about HTTP methods', async () => {
208+
test('a specific note is within the returned notes', async () => {
209209
const response = await api.get('/api/notes')
210210

211211
const contents = response.body.map(e => e.content)
@@ -220,7 +220,7 @@ Molemmat testit sijoittavat pyynnön vastauksen muuttujaan _response_. Toisin ku
220220
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:
221221

222222
```js
223-
test('the first note is about HTTP methods', async () => {
223+
test('a specific note is within the returned notes', async () => {
224224
const response = await api.get('/api/notes')
225225

226226
const contents = response.body.map(e => e.content)
@@ -273,12 +273,14 @@ Päätetään alustaa tietokanta ennen <i>jokaisen testin suoritusta</i>, eli fu
273273

274274
```js
275275

276-
// highlight-start
277-
const { test, after, beforeEach } = require('node:test')
278-
const Note = require('../models/note')
279-
// highlight-end
276+
const assert = require('node:assert')
277+
const { test, after, beforeEach } = require('node:test') // highlight-line
278+
const mongoose = require('mongoose')
279+
const supertest = require('supertest')
280+
const app = require('../app')
281+
const Note = require('../models/note') // highlight-line
280282

281-
// ...
283+
const api = supertest(app)
282284

283285
// highlight-start
284286
const initialNotes = [
@@ -304,6 +306,7 @@ beforeEach(async () => {
304306
await noteObject.save()
305307
})
306308
// highlight-end
309+
307310
// ...
308311
```
309312

@@ -314,7 +317,7 @@ Muutetaan muistiinpanojen lukumäärää testaavaa testiä vielä seuraavasti:
314317
```js
315318
// ...
316319

317-
test('there are two notes', async () => {
320+
test('all notes are returned', async () => {
318321
const response = await api.get('/api/notes')
319322

320323
assert.strictEqual(response.body.length, initialNotes.length) // highlight-line
@@ -338,7 +341,7 @@ test.only('notes are returned as json', async () => {
338341
.expect('Content-Type', /application\/json/)
339342
})
340343

341-
test.only('there are two notes', async () => {
344+
test.only('all notes are returned', async () => {
342345
const response = await api.get('/api/notes')
343346

344347
assert.strictEqual(response.body.length, 2)
@@ -364,7 +367,7 @@ npm test -- tests/note_api.test.js
364367
Parametrin [--tests-by-name-pattern](https://nodejs.org/api/test.html#filtering-tests-by-name) avulla voidaan suorittaa testejä nimen perusteella:
365368

366369
```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"
368371
```
369372

370373
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
578581
Testit muuttuvat muotoon
579582

580583
```js
581-
const { test, after, beforeEach } = require('node:test')
582584
const assert = require('node:assert')
585+
const { test, after, beforeEach } = require('node:test')
583586
const mongoose = require('mongoose')
584587
const supertest = require('supertest')
585588
const app = require('../app')
586-
const api = supertest(app)
587589
const helper = require('./test_helper') // highlight-line
588590
const Note = require('../models/note')
589591

592+
const api = supertest(app)
593+
590594
beforeEach(async () => {
591595
await Note.deleteMany({})
592596

@@ -604,16 +608,16 @@ test('notes are returned as json', async () => {
604608
.expect('Content-Type', /application\/json/)
605609
})
606610

607-
test('there are two notes', async () => {
608-
const response = await api.get('/api/notes')
611+
test('all notes are returned', async () => {
612+
const notes = await helper.notesInDb() // highlight-line
609613

610-
assert.strictEqual(response.body.length, 2)
614+
assert.strictEqual(notes.length, helper.initialNotes.length) // highlight-line
611615
})
612616

613-
test('the first note is about HTTP methods', async () => {
614-
const response = await api.get('/api/notes')
617+
test('a specific note is within the returned notes', async () => {
618+
const notes = await helper.notesInDb() // highlight-line
615619

616-
const contents = response.body.map(e => e.content)
620+
const contents = notes.map(n => n.content)
617621
assert(contents.includes('HTML is easy'))
618622
})
619623

@@ -629,12 +633,10 @@ test('a valid note can be added ', async () => {
629633
.expect(201)
630634
.expect('Content-Type', /application\/json/)
631635

632-
const response = await api.get('/api/notes')
633-
634-
const contents = response.body.map(r => r.content)
635-
636-
assert.strictEqual(response.body.length, helper.initialNotes.length + 1) // highlight-line
636+
const notesAtEnd = await helper.notesInDb() // highlight-line
637+
assert.strictEqual(notesAtEnd.length, helper.initialNotes.length + 1) // highlight-line
637638

639+
const contents = notesAtEnd.map(n => n.content) // highlight-line
638640
assert(contents.includes('async/await simplifies making async calls'))
639641
})
640642

@@ -648,9 +650,9 @@ test('note without content is not added', async () => {
648650
.send(newNote)
649651
.expect(400)
650652

651-
const response = await api.get('/api/notes')
653+
const notesAtEnd = await helper.notesInDb() // highlight-line
652654

653-
assert.strictEqual(response.body.length, helper.initialNotes.length) // highlight-line
655+
assert.strictEqual(notesAtEnd.length, helper.initialNotes.length) // highlight-line
654656
})
655657

656658
after(async () => {
@@ -756,7 +758,7 @@ Ensimmäisessä testissä on eräs huomionarvoinen seikka. Sen sijaan, että ver
756758
assert.deepStrictEqual(resultNote.body, noteToView)
757759
```
758760

759-
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_.
760762

761763
Testit menevät läpi, joten voimme turvallisesti refaktoroida testatut routet käyttämään async/awaitia:
762764

@@ -773,7 +775,9 @@ notesRouter.get('/:id', async (request, response, next) => {
773775
next(exception)
774776
}
775777
})
778+
```
776779

780+
```js
777781
notesRouter.delete('/:id', async (request, response, next) => {
778782
try {
779783
await Note.findByIdAndDelete(request.params.id)
@@ -1079,7 +1083,7 @@ describe('when there is initially some notes saved', () => {
10791083
const response = await api.get('/api/notes')
10801084

10811085
const contents = response.body.map(r => r.content)
1082-
assert(contents.includes('Browser can execute only JavaScript'))
1086+
assert(contents.includes('HTML is easy'))
10831087
})
10841088

10851089
describe('viewing a specific note', () => {

0 commit comments

Comments
 (0)