- [Instructor] The Jest framework can be used
to functionally test an application.
The results can be included in a code coverage report,
which is really useful.
As a reminder, functional testing literally tests
the functionality of an application.
The test cases are based
on the specifications of the software.
Unlike unit tests, a functional test is based
on business requirements like the ability to perform a task.
In that way, a functional test should read
like an acceptance test used by a QA team.
Functional tests describe what a system does, not how.
There's a number of different functional testing tools
and this is not an exhaustive list.
Selenium WebDriver from selenium.dev
automates actual browsers using a standalone server
and is fantastic for end-to-end testing.
It's now recommended by the W3C for doing browser testing.
Two projects that use selenium are Nightwatch.js
from nightwatchjs.org and WebdriverIO from webdriver.io.
A more lightweight option is SuperAgent
from visionmedia.github.io.
SuperAgent is a small progressive client-side
HTTP request library, and node.js module.
A project that uses SuperAgent is SuperTest,
which makes assertions for testing node.js HTTP servers.
Rather than exhaustively comparing each,
we'll take a more direct approach.
In this course, we're going to be using SuperTest.
It's easy to get started.
Just install the module dependency.
SuperTest doesn't require external software
such as a server that controls a browser.
It also doesn't require an application server to be running,
which is amazing.
Instead, it can just accept the Express app
and run the test on that.
It can also use a running application server if you want.
In this way, SuperTest is a lightweight and flexible,
which are excellent characteristics.
Finally, it's compatible with Jest
without any additional plugins or work.
After this course, I recommend exploring some
of the other options as they offer more powerful
functionality that you might want,
and also they're compatible with Jest.
SuperTest is quite easy to use.
For setup, it accepts an HTTP.server or a function.
Practically, Express returns that,
so we can just give it an Express application.
If the server is not listening on a port,
SuperTest will pick a random port.
Don't worry, you do not need to specify a port
in your tests.
SuperTest uses HTTP verbs to interact,
so .get, .post, and so forth.
SuperTest includes optional expectations
which can be used to validate the response.
Finally, after each request,
it returns the response that can also be tested
with uTest framework of choice.
In the context of our application,
let's write functional tests from basic user stories.
As a user, I want to see a reservation form
in order to book a table.
As a user, I want to submit a valid reservation
and be thanked upon success.
Finally, as a user, I want to submit an invalid reservation
and be informed that there's a problem.
Let's start by installing the SuperTest module.
Switch over to VS code.
Open up a terminal.
From the root directory, I'm going to use NPM
to install SuperTest to the devDependencies dash capital D.
Press enter.
That's all we need.
We can close the terminal now.
Are you ready to write a functional test with test doubles?