net

(Brent) #1

FEATURES
25 JavaScript tools


PROJECT TESTING


Above Jest can generate code coverage reports to help identify any gaps in test suites

Above The Jest object provides plenty of useful
behaviours, such as mocks and fake timers

Jest
facebook.github.io/jest
Jest is a test runner created
by Facebook. Its main aim is
to be as easy to set up and
use as possible, with no
configuration required to get started. Any
files inside a “__tests__” directory or
ending in “.spec.js” or “.test.js” will be
picked up automatically.
Tests run quickly and can run on each
change to make sure that everything
works correctly. Jest can even pick
up what has been updated since the
last commit and will only run tests
on elements that have been affected.
Customisable command line options
make sure the right tests are running at
the right time, which is ideal for keeping
continuous integration tools fast.
One standout feature of Jest is snapshot
tests. While not testing a specific value,
Jest will capture the structure of what
is under test and will compare against
that in future tests. If anything has
changed, Jest will highlight the change
and it can either be fixed or confirmed
as intentional. This works great for React
components but can also be used for any
kind of serialised value.

JSDOM is configured by default, which
makes testing browser-based projects
easier by enabling them to run through
Node. Jest also comes with basic test
functionality that includes mocks, spies
and assertions. While these will work,
other tools such as Enzyme and Sinon can
be brought in to make writing tests an
easier process.
While Jest is often used to test React
applications, in actuality it can be used
to test a project that was written for
any framework or language. Premade
setup files can take the hassle out of
building the right testing environment,
for example making sure that Angular is
defined globally.

Mocha
mochajs.org
Mocha is often the go-to for
developers to write a range
of tests in a project because
it is simple to get started.
Each test can run through Node or in the
browser, making it easy to see where
issues are occurring. Mocha also works
with any assertion library, which makes
it highly customisable to suit the needs of
different teams.

Puppeteer
github.com/GoogleChrome/puppeteer
Puppeteer provides a way to
control Chrome with Node,
which makes repeatable UI
tests a lot easier to perform.
It can check visual elements in the
browser, such as the page title, as well as
create screenshots to help track any
regressions. Test runners like Jest or
Mocha can run Puppeteer and can fail
tests based on its results.

Protractor
protractortest.org
Protractor is a specialised
testing tool for all versions
of Angular. It uses Selenium
to run end-to-end tests in
real-world environments and browsers.
Protractor will do all the set up required
and can automatically wait for the page to
load, which helps avoid brittle tests and
decreases the amount that subsequently
needs to be written.

Nightwatch
nightwatchjs.org
For other projects that need
end-to-end testing,
Nightwatch makes a
fantastic alternative. It also
runs Selenium under the hood, which will
enable you to test what the end user will
see. Tests are written as modules that are
run through Node and it is an easy
procedure for them to be extended to
connect to CI platforms or to send their
results to external software.
Free download pdf