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
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.
89
89
90
90
The fix is easy. Let us also reformulate the route code a bit:

86
86
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ä.
88
88
89
89
Korjaus on onneksi helppo. Muotoillaan routea muutenkin hieman siistimmäksi:
Copy file name to clipboardExpand all lines: src/content/4/en/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: en
8
8
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
-
<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>
Copy file name to clipboardExpand all lines: src/content/4/en/part4a.md
+40-69Lines changed: 40 additions & 69 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -504,19 +504,13 @@ module.exports = {
504
504
505
505
> 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.
506
506
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.
508
509
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.
510
511
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.
512
512
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:
520
514
521
515
```bash
522
516
{
@@ -529,62 +523,43 @@ Let's define the <i>npm script _test_</i> to execute tests with Jest and to repo
529
523
"deploy:full":"npm run build:ui && npm run deploy",
530
524
"logs:prod":"fly logs",
531
525
"lint":"eslint .",
532
-
"test":"jest --verbose"// highlight-line
526
+
"test":"node --test"// highlight-line
533
527
},
534
528
//...
535
529
}
536
530
```
537
531
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
-
```
548
532
549
533
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:
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.
586
561
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_:
@@ -596,55 +571,59 @@ Individual test cases are defined with the _test_ function. The first parameter
596
571
() => {
597
572
constresult=reverse('react')
598
573
599
-
expect(result).toBe('tcaer')
574
+
assert.strictEqual(result, 'tcaer')
600
575
}
601
576
```
602
577
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.
604
579
605
580
As expected, all of the tests pass:
606
581
607
-

582
+

608
583
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>.
610
585
611
-
Jest has excellent error messages, let's break the test to demonstrate this:
586
+
Let's break the test:
612
587
613
588
```js
614
589
test('reverse of react', () => {
615
590
constresult=reverse('react')
616
591
617
-
expect(result).toBe('tkaer')
592
+
assert.strictEqual(result, 'tkaer')
618
593
})
619
594
```
620
595
621
596
Running this test results in the following error message:
622
597
623
-

598
+

624
599
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>.
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.
694
673
695
674
#### 4.3: Helper Functions and Unit Tests, step 1
696
675
@@ -709,13 +688,15 @@ module.exports = {
709
688
Verify that your test configuration works with the following test:
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).
753
734
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.
763
736
764
737
#### 4.5*: Helper Functions and Unit Tests, step 3
765
738
@@ -775,8 +748,6 @@ The value returned by the function could be in the following format:
775
748
}
776
749
```
777
750
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
-
780
751
Write the tests for this exercise inside of a new <i>describe</i> block. Do the same for the remaining exercises as well.
781
752
782
753
#### 4.6*: Helper Functions and Unit Tests, step 4
0 commit comments