Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,050 --> 00:00:05,470
So automated testing is a nice feature.
2
00:00:05,470 --> 00:00:07,280
How does it work?
3
00:00:07,280 --> 00:00:10,070
As I mentioned, attached to the previous lecture
4
00:00:10,070 --> 00:00:12,560
you found this starting project
5
00:00:12,560 --> 00:00:14,810
which is a standard react project,
6
00:00:14,810 --> 00:00:18,333
which I did create with create react app.
7
00:00:19,390 --> 00:00:22,960
Now I didn't really change the component code too much.
8
00:00:22,960 --> 00:00:26,280
I just cleaned up the index JS file a little bit
9
00:00:26,280 --> 00:00:30,820
and I did not delete the app test JS file,
10
00:00:30,820 --> 00:00:33,890
which we did not use up to this point in the course,
11
00:00:33,890 --> 00:00:36,060
and the setup test JS file
12
00:00:36,060 --> 00:00:38,453
because we will need those files now.
13
00:00:39,830 --> 00:00:42,720
Now set up tests JS, as the name implies,
14
00:00:42,720 --> 00:00:45,060
just does some setup work
15
00:00:45,060 --> 00:00:48,770
and we don't need to do anything else in this file.
16
00:00:48,770 --> 00:00:52,360
But the app test JS file is quite interesting.
17
00:00:52,360 --> 00:00:56,950
This is a file that contains some testing code
18
00:00:56,950 --> 00:00:58,920
and it is there out of the box.
19
00:00:58,920 --> 00:01:00,430
Later in this module
20
00:01:00,430 --> 00:01:04,450
we will also write our own tests from scratch.
21
00:01:04,450 --> 00:01:08,470
This is a file which is there to test this app component
22
00:01:08,470 --> 00:01:11,340
because the convention is to name your testing files
23
00:01:11,340 --> 00:01:13,100
like your component files,
24
00:01:13,100 --> 00:01:15,650
just with the word test in the file name.
25
00:01:15,650 --> 00:01:20,650
So dot test dot JS as an extension to be precise.
26
00:01:20,760 --> 00:01:24,030
And in this app test JS file.
27
00:01:24,030 --> 00:01:26,350
We have this test function
28
00:01:26,350 --> 00:01:30,210
which we execute here, which takes two arguments.
29
00:01:30,210 --> 00:01:33,410
The first argument is a description of the test.
30
00:01:33,410 --> 00:01:34,640
This is up to you.
31
00:01:34,640 --> 00:01:38,310
It helps you identify this test in the testing output
32
00:01:38,310 --> 00:01:40,770
which we'll see in a couple of minutes.
33
00:01:40,770 --> 00:01:42,270
And it's especially helpful
34
00:01:42,270 --> 00:01:45,650
if you have an app with more than one test, of course.
35
00:01:45,650 --> 00:01:48,140
The second argument is a function
36
00:01:48,140 --> 00:01:51,180
an anonymous function, which is the point here
37
00:01:51,180 --> 00:01:53,543
which contains the actual test and code.
38
00:01:54,410 --> 00:01:57,040
So that's the code which will be executed
39
00:01:57,040 --> 00:01:59,750
once we run our test.
40
00:01:59,750 --> 00:02:02,330
And in there, we do a couple of things
41
00:02:02,330 --> 00:02:04,970
which we don't fully understand yet
42
00:02:04,970 --> 00:02:07,120
but we will soon understand it.
43
00:02:07,120 --> 00:02:10,850
We rendered the app component in the end with help
44
00:02:10,850 --> 00:02:14,630
of that render function imported from the testing library.
45
00:02:14,630 --> 00:02:16,640
So from that third party package
46
00:02:17,600 --> 00:02:22,600
then we get hold of some element on that virtual screen.
47
00:02:22,690 --> 00:02:26,370
So to say, so in that simulated browser.
48
00:02:26,370 --> 00:02:30,520
So to say, into which this app is rendered,
49
00:02:30,520 --> 00:02:32,550
we get hold of some element in there
50
00:02:32,550 --> 00:02:34,490
and we identify an element
51
00:02:34,490 --> 00:02:37,740
by the text that is rendered inside of it.
52
00:02:37,740 --> 00:02:39,590
Here we are looking for the text,
53
00:02:39,590 --> 00:02:43,510
learn react in a case insensitive way.
54
00:02:43,510 --> 00:02:47,000
This is our regular expression in case you're wondering.
55
00:02:47,000 --> 00:02:48,540
And then we just check
56
00:02:48,540 --> 00:02:52,160
if that element actually is in the document.
57
00:02:52,160 --> 00:02:56,680
So if it exists in the document. And this test will fail
58
00:02:56,680 --> 00:03:00,720
if this element is not found and it will succeed
59
00:03:00,720 --> 00:03:01,773
if it is found.
60
00:03:02,800 --> 00:03:05,560
And in app JS, we do have
61
00:03:05,560 --> 00:03:08,890
this, content here
62
00:03:08,890 --> 00:03:09,900
and we do have this
63
00:03:09,900 --> 00:03:13,370
learn react text in this anchor tag.
64
00:03:13,370 --> 00:03:14,500
And so if we look
65
00:03:14,500 --> 00:03:19,490
for an element with a text of learn react case insensitive,
66
00:03:19,490 --> 00:03:23,370
so casing does not matter, then this test should succeed
67
00:03:23,370 --> 00:03:26,093
because such an element clearly exist here.
68
00:03:27,530 --> 00:03:29,930
But that's all nice and theory.
69
00:03:29,930 --> 00:03:32,480
How do we run this test?
70
00:03:32,480 --> 00:03:34,150
For this, we got a script
71
00:03:34,150 --> 00:03:38,177
just as we got a script for running the development server.
72
00:03:38,177 --> 00:03:39,893
We run the development server.
73
00:03:39,893 --> 00:03:43,050
So that we as a human can preview
74
00:03:43,050 --> 00:03:45,020
our app and interact with it.
75
00:03:45,020 --> 00:03:49,470
We run the automated tests to see if those work and for that
76
00:03:49,470 --> 00:03:52,970
in packaged dot Json, we have this test script here
77
00:03:53,970 --> 00:03:57,860
and we can run this test script from inside the terminal.
78
00:03:57,860 --> 00:04:00,250
And here I'm using the terminal integrated
79
00:04:00,250 --> 00:04:04,990
into visual studio code by running NPM test.
80
00:04:04,990 --> 00:04:09,230
So just as NPM start, started the development server
81
00:04:09,230 --> 00:04:13,040
NPM tests, will run our automated tests.
82
00:04:13,040 --> 00:04:15,860
And here, if I now hit enter
83
00:04:15,860 --> 00:04:18,870
this will start up the testing.
84
00:04:18,870 --> 00:04:22,800
And actually, it does not run the tests right now.
85
00:04:22,800 --> 00:04:25,320
Instead, we now have to press 8
86
00:04:25,320 --> 00:04:27,570
to run all the tests it finds
87
00:04:27,570 --> 00:04:29,460
and it will automatically look
88
00:04:29,460 --> 00:04:33,620
for files that end with dot test dot JS
89
00:04:33,620 --> 00:04:36,210
and then run all the tests that are defined
90
00:04:36,210 --> 00:04:38,720
in there with that test function.
91
00:04:38,720 --> 00:04:41,020
So in this case, this one test, which we have.
92
00:04:41,890 --> 00:04:43,780
So if I now run, 8 here.
93
00:04:43,780 --> 00:04:47,990
If I press 8, you see it executed my tests.
94
00:04:47,990 --> 00:04:50,253
And then we get this test output here.
95
00:04:51,140 --> 00:04:55,930
We see that it ran one test in total.
96
00:04:55,930 --> 00:04:58,330
And that one test passed.
97
00:04:58,330 --> 00:04:59,910
I'll come back to the difference between
98
00:04:59,910 --> 00:05:02,280
tests suites, and tests.
99
00:05:02,280 --> 00:05:03,230
A little bit later
100
00:05:03,230 --> 00:05:06,880
here we see that it ran one test overall,
101
00:05:06,880 --> 00:05:08,850
and that test passed.
102
00:05:08,850 --> 00:05:11,290
And here we then see all the tests also,
103
00:05:11,290 --> 00:05:12,980
all the tests that passed
104
00:05:12,980 --> 00:05:16,320
in this case with the green check mark next to it.
105
00:05:16,320 --> 00:05:19,430
And here we see that description text again
106
00:05:19,430 --> 00:05:20,850
which we entered here.
107
00:05:20,850 --> 00:05:24,990
So that is what helps us identify which test succeeded
108
00:05:24,990 --> 00:05:25,823
or failed.
109
00:05:26,960 --> 00:05:29,000
And for example, if I now go
110
00:05:29,000 --> 00:05:31,230
to app JS here
111
00:05:31,230 --> 00:05:35,600
and I remove this text, learn react
112
00:05:35,600 --> 00:05:38,570
and I instead say, learn more.
113
00:05:38,570 --> 00:05:42,060
You see if I save us the tests automatically rerun
114
00:05:42,060 --> 00:05:43,360
because by default,
115
00:05:43,360 --> 00:05:46,650
it watches your files and reruns the tests.
116
00:05:46,650 --> 00:05:48,700
Whenever you save changes.
117
00:05:48,700 --> 00:05:51,943
And now you see that we get a failed test here.
118
00:05:52,976 --> 00:05:55,340
And if I scroll up, we get more information.
119
00:05:55,340 --> 00:05:58,480
We see that this test failed
120
00:05:58,480 --> 00:06:02,340
we see that by that red cross here.
121
00:06:02,340 --> 00:06:05,920
And we also get an explanation of why it failed.
122
00:06:05,920 --> 00:06:09,853
Unable to find an element with the text, learn react.
123
00:06:10,930 --> 00:06:14,500
Then we get an output off the actual content
124
00:06:14,500 --> 00:06:18,600
which had rendered where it tried to find that text
125
00:06:18,600 --> 00:06:20,790
and where it failed to do so.
126
00:06:20,790 --> 00:06:23,960
Of course, that is the content of app JS here.
127
00:06:23,960 --> 00:06:25,590
Then we see the testing code
128
00:06:25,590 --> 00:06:29,770
and the line that failed in this case get by text.
129
00:06:29,770 --> 00:06:31,940
That's the line that failed.
130
00:06:31,940 --> 00:06:34,690
And that is why desk test failed.
131
00:06:34,690 --> 00:06:39,690
And now as a developer, we could either adjust our test.
132
00:06:39,720 --> 00:06:41,880
If we want to have learned more here
133
00:06:41,880 --> 00:06:44,180
or go back to learn react
134
00:06:44,180 --> 00:06:47,550
to make sure that this test passes again.
135
00:06:47,550 --> 00:06:50,100
And of course that's a rather dumb example.
136
00:06:50,100 --> 00:06:52,050
It's just a basic example.
137
00:06:52,050 --> 00:06:56,810
But that's the idea behind testing and how testing works.
138
00:06:56,810 --> 00:06:59,290
Now, of course, we can always quit this testing mode
139
00:06:59,290 --> 00:07:01,450
by hitting control C here
140
00:07:01,450 --> 00:07:04,410
and then our files will not be watched anymore.
141
00:07:04,410 --> 00:07:06,690
And we need to restart testing
142
00:07:06,690 --> 00:07:10,030
with NPM test, if we want to do that.
143
00:07:10,030 --> 00:07:12,200
So these are the basics.
144
00:07:12,200 --> 00:07:14,143
Now let's dive a bit deeper.
11076
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.