Skip to content

Commit 66d046a

Browse files
committed
changes part 4
1 parent a79c054 commit 66d046a

File tree

16 files changed

+421
-509
lines changed

16 files changed

+421
-509
lines changed

src/content/3/en/part3d.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ When validating an object fails, we return the following default error message f
8585
![postman showing error message](../../images/3/50.png)
8686

8787
We notice that the backend has now a problem: validations are not done when editing a note.
88-
The [documentation](https://github.com/blakehaswell/mongoose-unique-validator#find--updates) addresses the issue by explaining that validations are not run by default when <i>findOneAndUpdate</i> and related methods are executed.
88+
The [documentation](https://mongoosejs.com/docs/validation.html#update-validators) addresses the issue by explaining that validations are not run by default when <i>findOneAndUpdate</i> and related methods are executed.
8989

9090
The fix is easy. Let us also reformulate the route code a bit:
9191

src/content/3/fi/osa3d.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Validoinnin epäonnistuessa palautetaan validaattorin oletusarvoinen virheviesti
8484

8585
![Luotaessa muistiinpano jonka kenttä content on liian lyhyt, seurauksena on virheilmoituksen sisältävä JSON](../../images/3/50.png)
8686

87-
Huomaamme kuitenkin että sovelluksessa on pieni ongelma, validaatiota ei suoriteta muistiinpanojen päivityksen yhteydessä. [Dokumentaatio](https://github.com/blakehaswell/mongoose-unique-validator#find--updates) kertoo mistä on kyse, validaatiota ei suoriteta oletusarvoisesti metodin <i>findOneAndUpdate</i> suorituksen yhteydessä.
87+
Huomaamme kuitenkin että sovelluksessa on pieni ongelma, validaatiota ei suoriteta muistiinpanojen päivityksen yhteydessä. [Dokumentaatio](https://mongoosejs.com/docs/validation.html#update-validators) kertoo mistä on kyse, validaatiota ei suoriteta oletusarvoisesti metodin <i>findOneAndUpdate</i> suorituksen yhteydessä.
8888

8989
Korjaus on onneksi helppo. Muotoillaan routea muutenkin hieman siistimmäksi:
9090

src/content/4/en/part4.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lang: en
88

99
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.
1010

11-
<i>Part updated 22nd Jan 2023</i>
12-
- <i>No major changes</i>
11+
<i>Part updated 13th Feb 2024</i>
12+
- <i>Jest replaced with Node enbedded testrunner</i>
1313

1414
</div>

src/content/4/en/part4a.md

Lines changed: 40 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -504,19 +504,13 @@ module.exports = {
504504
505505
> The _average_ function uses the array [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) method. If the method is not familiar to you yet, then now is a good time to watch the first three videos from the [Functional Javascript](https://www.youtube.com/watch?v=BMUiFMZr7vk&list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84) series on YouTube.
506506
507-
There are many different testing libraries or <i>test runners</i> available for JavaScript. In this course we will be using a testing library developed and used internally by Facebook called [jest](https://jestjs.io/), which resembles the previous king of JavaScript testing libraries [Mocha](https://mochajs.org/).
507+
There are a large number of test libraries, or <i>test runners</i>, available for JavaScript.
508+
The old king of test libraries is [Mocha](https://mochajs.org/), which was replaced a few years ago by [Jest](https://jestjs.io/). A newcomer to the libraries is [Vitest](https://vitest.dev/), which bills itself as a new generation of test libraries.
508509
509-
Jest is a natural choice for this course, as it works well for testing backends, and it shines when it comes to testing React applications.
510+
Nowadays, Node also has a built-in test library [node:test](https://nodejs.org/docs/latest/api/test.html), which is well suited to the needs of the course.
510511
511-
> <i>**Windows users:**</i> Jest may not work if the path of the project directory contains a directory that has spaces in its name.
512512
513-
Since tests are only executed during the development of our application, we will install <i>jest</i> as a development dependency with the command:
514-
515-
```bash
516-
npm install --save-dev jest
517-
```
518-
519-
Let's define the <i>npm script _test_</i> to execute tests with Jest and to report about the test execution with the <i>verbose</i> style:
513+
Let's define the <i>npm script _test_</i> for the test execution:
520514
521515
```bash
522516
{
@@ -529,62 +523,43 @@ Let's define the <i>npm script _test_</i> to execute tests with Jest and to repo
529523
"deploy:full": "npm run build:ui && npm run deploy",
530524
"logs:prod": "fly logs",
531525
"lint": "eslint .",
532-
"test": "jest --verbose" // highlight-line
526+
"test": "node --test" // highlight-line
533527
},
534528
//...
535529
}
536530
```
537531
538-
Jest requires one to specify that the execution environment is Node. This can be done by adding the following to the end of <i>package.json</i>:
539-
540-
```js
541-
{
542-
//...
543-
"jest": {
544-
"testEnvironment": "node"
545-
}
546-
}
547-
```
548532
549533
Let's create a separate directory for our tests called <i>tests</i> and create a new file called <i>reverse.test.js</i> with the following contents:
550534
551535
```js
536+
const { test } = require('node:test')
537+
const assert = require('node:assert')
538+
552539
const reverse = require('../utils/for_testing').reverse
553540

554541
test('reverse of a', () => {
555542
const result = reverse('a')
556543

557-
expect(result).toBe('a')
544+
assert.strictEqual(result, 'a')
558545
})
559546

560547
test('reverse of react', () => {
561548
const result = reverse('react')
562549

563-
expect(result).toBe('tcaer')
550+
assert.strictEqual(result, 'tcaer')
564551
})
565552

566-
test('reverse of releveler', () => {
567-
const result = reverse('releveler')
553+
test('reverse of saippuakauppias', () => {
554+
const result = reverse('saippuakauppias')
568555

569-
expect(result).toBe('releveler')
556+
assert.strictEqual(result, 'saippuakauppias')
570557
})
571558
```
572559
573-
The ESLint configuration we added to the project in the previous part complains about the _test_ and _expect_ commands in our test file since the configuration does not allow <i>globals</i>. Let's get rid of the complaints by adding <i>"jest": true</i> to the <i>env</i> property in the <i>.eslintrc.js</i> file.
574-
575-
```js
576-
module.exports = {
577-
'env': {
578-
'commonjs': true,
579-
'es2021': true,
580-
'node': true,
581-
'jest': true, // highlight-line
582-
},
583-
// ...
584-
}
585-
```
560+
The test defines the keyword _test_ and the library [assert](https://nodejs.org/docs/latest/api/assert.html), which is used by the tests to check the results of the functions under test.
586561
587-
In the first row, the test file imports the function to be tested and assigns it to a variable called _reverse_:
562+
In the next row, the test file imports the function to be tested and assigns it to a variable called _reverse_:
588563
589564
```js
590565
const reverse = require('../utils/for_testing').reverse
@@ -596,55 +571,59 @@ Individual test cases are defined with the _test_ function. The first parameter
596571
() => {
597572
const result = reverse('react')
598573

599-
expect(result).toBe('tcaer')
574+
assert.strictEqual(result, 'tcaer')
600575
}
601576
```
602577
603-
First, we execute the code to be tested, meaning that we generate a reverse for the string <i>react</i>. Next, we verify the results with the [expect](https://jestjs.io/docs/expect#expectvalue) function. Expect wraps the resulting value into an object that offers a collection of <i>matcher</i> functions, that can be used for verifying the correctness of the result. Since in this test case we are comparing two strings, we can use the [toBe](https://jestjs.io/docs/expect#tobevalue) matcher.
578+
First, we execute the code to be tested, meaning that we generate a reverse for the string <i>react</i>. Next, we verify the results with the the method [strictEqual](https://nodejs.org/docs/latest/api/assert.html#assertstrictequalactual-expected-message) of the [assert](https://nodejs.org/docs/latest/api/assert.html) library.
604579
605580
As expected, all of the tests pass:
606581
607-
![terminal output from npm test with all tests passing](../../images/4/1x.png)
582+
![terminal output from npm test with all tests passing](../../images/4/1new.png)
608583
609-
Jest expects by default that the names of test files contain <i>.test</i>. In this course, we will follow the convention of naming our tests files with the extension <i>.test.js</i>.
584+
The library node:test expects by default that the names of test files contain <i>.test</i>. In this course, we will follow the convention of naming our tests files with the extension <i>.test.js</i>.
610585
611-
Jest has excellent error messages, let's break the test to demonstrate this:
586+
Let's break the test:
612587
613588
```js
614589
test('reverse of react', () => {
615590
const result = reverse('react')
616591

617-
expect(result).toBe('tkaer')
592+
assert.strictEqual(result, 'tkaer')
618593
})
619594
```
620595
621596
Running this test results in the following error message:
622597
623-
![terminal output shows failure from npm test](../../images/4/2x.png)
598+
![terminal output shows failure from npm test](../../images/4/2new.png)
624599
625-
Let's add a few tests for the _average_ function, into a new file <i>tests/average.test.js</i>.
600+
Let output from the npm test with _average_ function, into a new file <i>tests/average.test.js</i>.
626601
627602
```js
603+
const { test, describe } = require('node:test')
604+
605+
// ...
606+
628607
const average = require('../utils/for_testing').average
629608

630609
describe('average', () => {
631610
test('of one value is the value itself', () => {
632-
expect(average([1])).toBe(1)
611+
assert.strictEqual(average([1]), 1)
633612
})
634613

635614
test('of many is calculated right', () => {
636-
expect(average([1, 2, 3, 4, 5, 6])).toBe(3.5)
615+
assert.strictEqual(average([1, 2, 3, 4, 5, 6]), 3.5)
637616
})
638617

639618
test('of empty array is zero', () => {
640-
expect(average([])).toBe(0)
619+
assert.strictEqual(average([]), 0)
641620
})
642621
})
643622
```
644623
645624
The test reveals that the function does not work correctly with an empty array (this is because in JavaScript dividing by zero results in <i>NaN</i>):
646625
647-
![terminal output showing empty array fails with jest](../../images/4/3.png)
626+
![terminal output showing empty array fails](../../images/4/3new.png)
648627
649628
Fixing the function is quite easy:
650629
@@ -670,17 +649,17 @@ describe('average', () => {
670649
})
671650
```
672651
673-
Describe blocks can be used for grouping tests into logical collections. The test output of Jest also uses the name of the describe block:
652+
Describe blocks can be used for grouping tests into logical collections. The test output also uses the name of the describe block:
674653
675-
![screenshot of npm test showing describe blocks](../../images/4/4x.png)
654+
![screenshot of npm test showing describe blocks](../../images/4/4new.png)
676655
677656
As we will see later on <i>describe</i> blocks are necessary when we want to run some shared setup or teardown operations for a group of tests.
678657
679658
Another thing to notice is that we wrote the tests in quite a compact way, without assigning the output of the function being tested to a variable:
680659
681660
```js
682661
test('of empty array is zero', () => {
683-
expect(average([])).toBe(0)
662+
assert.strictEqual(average([]), 0)
684663
})
685664
```
686665
@@ -690,7 +669,7 @@ test('of empty array is zero', () => {
690669
691670
### Exercises 4.3.-4.7.
692671
693-
Let's create a collection of helper functions that are meant to assist in dealing with 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.
672+
Let's create a collection of helper functions that are metest showing described blocksh 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.
694673
695674
#### 4.3: Helper Functions and Unit Tests, step 1
696675
@@ -709,13 +688,15 @@ module.exports = {
709688
Verify that your test configuration works with the following test:
710689
711690
```js
691+
const { test, describe } = require('node:test')
692+
const assert = require('node:assert')
712693
const listHelper = require('../utils/list_helper')
713694

714695
test('dummy returns one', () => {
715696
const blogs = []
716697

717698
const result = listHelper.dummy(blogs)
718-
expect(result).toBe(1)
699+
assert.strictEqual(result), 1)
719700
})
720701
```
721702
@@ -744,22 +725,14 @@ describe('total likes', () => {
744725

745726
test('when list has only one blog, equals the likes of that', () => {
746727
const result = listHelper.totalLikes(listWithOneBlog)
747-
expect(result).toBe(5)
728+
assert.strictEqual(result), 5)
748729
})
749730
})
750731
```
751732
752733
If defining your own test input list of blogs is too much work, you can use the ready-made list [here](https://github.com/fullstack-hy2020/misc/blob/master/blogs_for_test.md).
753734
754-
You are bound to run into problems while writing tests. Remember the things that we learned about [debugging](/en/part3/saving_data_to_mongo_db#debugging-node-applications) in part 3. You can print things to the console with _console.log_ even during test execution. It is even possible to use the debugger while running tests, you can find instructions for that [here](https://jestjs.io/docs/en/troubleshooting).
755-
756-
**NB:** if some test is failing, then it is recommended to only run that test while you are fixing the issue. You can run a single test with the [only](https://jestjs.io/docs/api#testonlyname-fn-timeout) method.
757-
758-
Another way of running a single test (or describe block) is to specify the name of the test to be run with the [-t](https://jestjs.io/docs/en/cli.html) flag:
759-
760-
```js
761-
npm test -- -t 'when list has only one blog, equals the likes of that'
762-
```
735+
You are bound to run into problems while writing tests. Remember the things that we learned about [debugging](/en/part3/saving_data_to_mongo_db#debugging-node-applications) in part 3. You can print things to the console with _console.log_ even during test execution.
763736
764737
#### 4.5*: Helper Functions and Unit Tests, step 3
765738
@@ -775,8 +748,6 @@ The value returned by the function could be in the following format:
775748
}
776749
```
777750
778-
**NB** when you are comparing objects, the [toEqual](https://jestjs.io/docs/en/expect#toequalvalue) method is probably what you want to use, since the [toBe](https://jestjs.io/docs/en/expect#tobevalue) tries to verify that the two values are the same value, and not just that they contain the same properties.
779-
780751
Write the tests for this exercise inside of a new <i>describe</i> block. Do the same for the remaining exercises as well.
781752
782753
#### 4.6*: Helper Functions and Unit Tests, step 4

0 commit comments

Comments
 (0)