Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Testing is a major part of JavaScript programming. In many cases, developers programmatically test code and features in their projects to ensure expected behavior. They write special functions called unit tests to check if each part of their program works.
Testing is a major part of
JavaScript programming.
0:00
Testing helps developers avoid introducing
bugs and defects in their projects, which
0:03
if deployed to production could cost them
or their company money in returning users.
0:07
As developers add new
features to a website or
0:11
app, they test that a feature works and
that nothing else broke in the process.
0:14
They might use a combination
of testing methods for this.
0:18
For example, they'll code a small part of
their program, refresh the app or site and
0:21
interact with it to test if
everything works as expected.
0:25
And they'll check for runtime errors and
warning messages in the console.
0:28
This manual testing process works but
it can be slow and error prone.
0:32
And it doesn't always prove
that your logic works.
0:36
There might be times when you're unsure
of what went wrong or how to fix it.
0:38
So how can developers prove that each
piece of their code does what they want?
0:42
In many cases, developers
programmatically test the code and
0:46
features in their project to
ensure expected behavior.
0:49
They write special functions called unit
tests to check if each part of their
0:53
program works, even in edge cases or
extreme operating parameters.
0:57
They first identify the expected
outcomes of every part of their program,
1:01
individual functions for example, and
1:06
write tests against them to make
sure those parts work as intended.
1:08
For instance, they'll write a test
that checks if a function exists, and
1:12
a test that checks a function's behavior
when given a certain set of inputs.
1:16
The tests make sure that each function
gets called with the required arguments
1:20
and returns the correct result.
1:24
Writing JavaScript unit tests from
scratch, especially on big projects,
1:26
tend to be limited, hard to implement,
and time consuming.
1:30
So the wonderful JavaScript
community once again has our backs.
1:34
They've created libraries and frameworks
that let you quickly write automated
1:37
unit tests that detect
problems in your code.
1:41
Mocha is one of the most used
JavaScript testing frameworks.
1:44
It runs on Node and in the browser.
1:48
Mocha is a flexible and
1:50
extendable library that provides the base
structure for setting up your tests.
1:52
It can be easily integrated with
other testing libraries for
1:57
additional functionality, too.
2:00
And Jasmine is another popular
JavaScript unit testing framework.
2:02
The good thing is that Jasmine does not
depend on any other JavaScript framework.
2:05
It comes batteries included,
2:10
providing everything you need to
test your code right out of the box.
2:12
It's also the popular choice for
testing angular applications.
2:16
It makes testing much easier to set up and
run.
2:20
And Jest is a feature-rich testing
library created by Facebook.
2:23
Many React developers test with Jest
2:28
because it's already set up when you
create a project with Create React App.
2:30
But you can test any JavaScript
application with Jest.
2:34
It's built for unit and integration
testing, which means that it can run tests
2:37
that check if all the units of
your app work together correctly.
2:41
And Enzyme is a JavaScript testing utility
for React, created and used by Airbnb.
2:45
It's also built to be compatible
with other testing libraries.
2:51
Writing unit test can help
you improve your code,
2:55
before you even start writing it, and
can serve as valuable documentation for
2:57
how your application is supposed to work.
3:01
You'll have your bugs,
spend less time debugging, and
3:04
a better understand
the problems you do have.
3:06
As JavaScript developer Eric Elliott
stated, tests are your first and
3:09
best line of defense
against software defects.
3:12
You need to sign up for Treehouse in order to download course files.
Sign up