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
There are many different ways of testing React applications. Let's take a look at them next.
11
11
12
-
Tests will be implemented with the same [Jest](http://jestjs.io/)testing library developed by Facebook that was used in the previous part.
12
+
The course previously used the [Jest](http://jestjs.io/) library developed by Facebook to test React components. We are now using the new generation of testing tools from Vite developers called [Vitest](https://vitest.dev/). Apart from the configurations, the libraries provide the same programming interface, so there is virtually no difference in the test code.
13
13
14
-
In addition to Jest, we also need another testing library that will help us render components for testing purposes. The current best option for this is [react-testing-library](https://github.com/testing-library/react-testing-library) which has seen rapid growth in popularity in recent times.
14
+
Let's start by installing Vitest and the [jsdom](https://github.com/jsdom/jsdom) library simulating a web browser:
15
+
16
+
```
17
+
npm install --save-vitest vitest jsdom
18
+
```
19
+
20
+
In addition to Vitest, we also need another testing library that will help us render components for testing purposes. The current best option for this is [react-testing-library](https://github.com/testing-library/react-testing-library) which has seen rapid growth in popularity in recent times. It is also worth extending the expressive power of the tests with the library [jest-dom](https://github.com/testing-library/jest-dom).
15
21
16
22
Let's install the libraries with the command:
17
23
18
24
```js
19
-
npm install --save-dev @testing-library/react @testing-library/jest-dom jest jest-environment-jsdom @babel/preset-env @babel/preset-react
@@ -104,28 +125,57 @@ We can use the object [screen](https://testing-library.com/docs/queries/about#sc
104
125
expect(element).toBeDefined()
105
126
```
106
127
128
+
The existence of an element is checked using Vitest's [expect](https://vitest.dev/api/expect.html#expect) command. Expect generates an assertion from its parameter, the validity of which can be tested using various condition functions. Now we used [toBeDefined](https://vitest.dev/api/expect.html#tobedefined) which tests whether the _element_ parameter of expect exists.
129
+
107
130
Run the test with command _npm test_:
108
131
109
132
```js
110
133
$ npm test
111
134
112
135
> notes-frontend@0.0.0 test
113
-
> jest
136
+
> vitest
137
+
138
+
139
+
DEV v1.3.1/Users/mluukkai/opetus/2024-fs/part3/notes-frontend
Eslint complains about the keywords _test_ and _expect_ in the tests. The problem can be solved by installing [eslint-plugin-vitest-globals](https://www.npmjs.com/package/eslint-plugin-vitest-globals):
125
154
126
-
**NB:** the console may issue a warning if you have not installed Watchman. Watchman is an application developed by Facebook that watches for changes that are made to files. The program speeds up the execution of tests and at least starting from macOS Sierra, running tests in watch mode issues some warnings to the console, that can be removed by installing Watchman.
Instructions for installing Watchman on different operating systems can be found on the official Watchman website: <https://facebook.github.io/watchman/>
159
+
and enable the plugin by editing the _.eslint.cjs_ file as follows:
@@ -140,8 +190,6 @@ I do not like this way of storing tests and application code in the same directo
140
190
The react-testing-library package offers many different ways of investigating the content of the component being tested. In reality, the _expect_ in our test is not needed at all:
@@ -164,8 +212,6 @@ Test fails if _getByText_ does not find the element it is looking for.
164
212
We could also use [CSS-selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) to find rendered elements by using the method [querySelector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) of the object [container](https://testing-library.com/docs/react-testing-library/api/#container-1) that is one of the fields returned by the render:
@@ -195,8 +241,6 @@ We typically run into many different kinds of problems when writing our tests.
195
241
Object _screen_ has method [debug](https://testing-library.com/docs/dom-testing-library/api-debugging#screendebug) that can be used to print the HTML of a component to the terminal. If we change the test as follows:
There are a few interesting things related to this test. The event handler is a [mock](https://jestjs.io/docs/mock-functions) function defined with Jest:
354
+
There are a few interesting things related to this test. The event handler is a [mock](https://vitest.dev/api/mock) function defined with Vitest:
315
355
316
356
```js
317
-
constmockHandler=jest.fn()
357
+
constmockHandler=vi.fn()
318
358
```
319
359
320
360
A [session](https://testing-library.com/docs/user-event/setup/) is started to interact with the rendered component:
@@ -332,12 +372,14 @@ await user.click(button)
332
372
333
373
Clicking happens with the method [click](https://testing-library.com/docs/user-event/convenience/#click) of the userEvent-library.
334
374
335
-
The expectation of the test verifies that the <i>mock function</i> has been called exactly once.
375
+
The expectation of the test uses [toHaveLength](https://vitest.dev/api/expect.html#tohavelength) to verify that the <i>mock function</i> has been called exactly once:
336
376
337
377
```js
338
378
expect(mockHandler.mock.calls).toHaveLength(1)
339
379
```
340
380
381
+
The calls of the mock function are saved to the array [mock.calls](https://vitest.dev/api/mock#mock-calls) within the mock function object.
382
+
341
383
[Mock objects and functions](https://en.wikipedia.org/wiki/Mock_object) are commonly used [stub](https://en.wikipedia.org/wiki/Method_stub) components in testing that are used for replacing dependencies of the components being tested. Mocks make it possible to return hardcoded responses, and to verify the number of times the mock functions are called and with what parameters.
342
384
343
385
In our example, the mock function is a perfect choice since it can be easily used for verifying that the method gets called exactly once.
test('<NoteForm /> updates parent state and calls onSubmit', async () => {
508
-
constcreateNote=jest.fn()
547
+
constcreateNote=vi.fn()
509
548
constuser=userEvent.setup()
510
549
511
550
render(<NoteForm createNote={createNote} />)
@@ -528,6 +567,31 @@ The method [type](https://testing-library.com/docs/user-event/utility#type) of t
528
567
The first test expectation ensures that submitting the form calls the _createNote_ method.
529
568
The second expectation checks that the event handler is called with the right parameters - that a note with the correct content is created when the form is filled.
530
569
570
+
It's worth noting that the good old _console.log_ works as usual in the tests. For example, if you want to see what the calls stored by the mock-object look like, you can do the following
571
+
572
+
````js
573
+
test('<NoteForm /> updates parent state and calls onSubmit', async() => {
We can easily find out the [coverage](https://jestjs.io/blog/2020/01/21/jest-25#v8-code-coverage) of our tests by running them with the command.
801
+
We can easily find out the [coverage](https://vitest.dev/guide/coverage.html#coverage) of our tests by running them with the command.
738
802
739
803
```js
740
-
npm test ----coverage--collectCoverageFrom='src/**/*.{jsx,js}'
804
+
npm test -- --coverage
741
805
```
742
806
807
+
The first time you run the command, Vitest will ask you if you want to install the required library _@vitest/coverage-v8_. Install it, and run the command again:
808
+
743
809

744
810
745
-
A quite primitive HTML report will be generated to the <i>coverage/lcov-report</i> directory.
811
+
AHTML report will be generated to the <i>coverage</i> directory.
746
812
The report will tell us the lines of untested code in each component:
747
813
748
-

814
+

749
815
750
816
You can find the code for our current application in its entirety in the <i>part5-8</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-8).
751
817
@@ -788,7 +854,7 @@ We chose to concentrate on making end-to-end tests to test the whole application
788
854
789
855
### Snapshot testing
790
856
791
-
Jest offers a completely different alternative to "traditional" testing called [snapshot](https://jestjs.io/docs/snapshot-testing) testing. The interesting feature of snapshot testing is that developers do not need to define any tests themselves, it is simple enough to adopt snapshot testing.
857
+
Vitest offers a completely different alternative to "traditional" testing called [snapshot](https://vitest.dev/guide/snapshot) testing. The interesting feature of snapshot testing is that developers do not need to define any tests themselves, it is simple enough to adopt snapshot testing.
792
858
793
859
The fundamental principle is to compare the HTML code defined by the component after it has changed to the HTML code that existed before it was changed.
0 commit comments