The Frontend Hitchhiker's Guide: Testing
Tags: testing, webdev, javascript, codenewbie
Published: 2021-06-27 01:21:49

# Introduction
Tests give you the confidence to make changes to code whether its yours or someone else's. Tested code also brings peace of mind and is the calling card of a mature developer. 

The following is an overview of some of the popular tooling for testing front end applications.

![testing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awfh13b7zrbwnyntjcx0.gif)
 
Testing tools typically provide some combination of the following features:

1. Test running & reporting
2. Assertion Syntax for writing test code
3. Browser Automation
4. Page auditing & profiling

The following are common types of tests

| Type of Testing | Definition                                                                                                                |
|-----------------|---------------------------------------------------------------------------------------------------------------------------|
| Unit            | Checking the functionality of individual components by comparing their inputs and output.                                 |
| Integration     | Verifying the functionality of                                                                                            |
| End to End      | Automating the browser to verify functionality from the UI, filling forms clicking buttons and checking network requests. |
| Performance     | Auditing a web page's performance such as load times and core web vitals                                                 |
| Accessibility   | Audit the accessibility of a page                                                                                     |

# Testing Tools

* Libraries
  * [Jasmine](#jasmine)
  * [Jest](#jest)
  * [Chai](#chai)
  * [Mocha](#mocha)
  * [Testing Library](#testing-library)
* Browser Automation
  * [Puppeteer](#puppeteer)
  * [Web Driver IO](#web-driver-io)
  * [Selenium Web Driver](#selenium-web-driver)
  * [Nightwatch](#nightwatch)
  * [Cypress](#cypress)
* Page Auditing
  * [Lighthouse](#lighthouse)
  * [Webpage Test](#web-page-test)
  * [WAVE](#wave)

## Frameworks/Libraries

### [Jasmine](https://jasmine.github.io/)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdtsnb1wsycyuwd2ovfi.png)
Jasmine is the default testing library of angular but it can be used with any frontend framework. Jasmine allows you to write unit tests using the [Behavior-Driven Development](https://en.wikipedia.org/wiki/Behavior-driven_development) (BDD) style.


### [Jest](https://jestjs.io/)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyfdthxwuaelt8s3ljll.png)
Jest is another popular framework, jest boasts excellent exceptions that can provide more context when test cases fail. The Jest core team's goal is to make testing delightful.
 
### [Chai](https://www.chaijs.com/)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fg515v43eisa68ofs6yb.png) Chai is an assertion library, which means it can provide additionally testing syntax that you may want on top of your existing test library. Allow developers to write more specific and readable test cases.

### [Mocha](https://mochajs.org/)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qoyyzc6hotk30k96ff09.png)
Mocha is a testing framework for writing tests but also lets your run the tests and view the report via the CLI. Mocha is often paired with Chai.

### [Testing Library](https://testing-library.com/docs/react-testing-library/intro/)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yc2stcoyb9lx1siredza.png)

Testing Library describes itself as a set of utilities that encourage good testing practices. It includes various libraries that allow you to write tests no matter what you use on the frontend such as the [angular](https://testing-library.com/docs/angular-testing-library/intro), [React](https://testing-library.com/docs/react-testing-library/intro) and [vue](https://testing-library.com/docs/vue-testing-library/intro). Some of which are recommended by the corresponding UI library itself such as [vue](https://reactjs.org/docs/testing.html) and [react](https://reactjs.org/docs/testing.html)

## Browser Automation

### [Puppeteer](https://developers.google.com/web/tools/puppeteer)

Puppeteer is a fun nodejs package that lets you automate interactions on a browser's webpage. This is usually paired with a testing framework such as mocha to perform End-To-End testcase. For example with this combination you can test to verify if a form submits properly or if a table has a particular structure.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qlswfnuhxc0th0rhmd0.png)

### [Web Driver IO](https://webdriver.io/)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pa0krhhz9p3ysos8nd96.png)

Web driver IO is another automation tool with the advantage of being designed with UI Libraries such as React and Vue in mind. Web Driver IO's syntax lets you directly select react/vue components on the page. This allows you to write tests specific to the source code as opposed to the build output that puppeteer would require.

### [Selenium Web Driver](https://www.selenium.dev/documentation/en/webdriver/)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7iy9aw2zpgze8yeyyuif.png)

Selenium Web Driver is another browser automation tool with more support for web browsers than puppeteer including Safari, Opera and even Internet Explorer.
 
### [Nightwatch](https://nightwatchjs.org/)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbu1w6basop3kbyakq3p.png)

Nightwatch is a fully integrated End-to-End testing tool that adheres to the [W3C](https://www.w3.org/TR/webdriver/) API. It also works with [Browser Stack](https://www.browserstack.com/) to perform cloud testing.

### [Cypress](https://www.cypress.io/)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rj7p0hk1owrswaaz2l5n.png)

Cypress is another fully integrated End-to-End testing tool which lets you write, run and record tests in an interactive dashboard. Cypress has a really nice developer experience and much loved by its userbase.
 
## Page Auditing

### [Lighthouse](https://developers.google.com/web/tools/lighthouse)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cghr8j4m7g9gmj776g8c.png) 

Lighthouse audits performance according to the [core web vitals](https://web.dev/vitals/). Additionally, lighthouse can audit for Accessibility, Best Practices, Search Engine Optimization and whether your page is a Progressive Web App. You can also automate lighthouse audits via its [API](https://github.com/GoogleChrome/lighthouse-ci). Its a great tool to ensure your app is fast and does well with google search.

### [Web Page Test](https://www.webpagetest.org/)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ha3j71308j9wexv1mrs.png)

Web page test is a website that allows you to analyze a web page's performance. It records checks for compression, caching effective use CDN and load times among other things. You can create automated performance tests using its [API](https://github.com/marcelduran/webpagetest-api)

### [WAVE](https://wave.webaim.org/)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/astr39fel6buo716az3y.png)
The Web Accessibility Evaluation Tool with audit your page for accessibility issues. This includes improper use of html elements, bad tab navigation, improper use of [aria attributes](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA), font sizes and colour contrasts. Accessibility (A11y) is unfortunately an afterthought sometimes for developers but paying just a little attention to it can go a long way for making a web for everyone.

# Conclusion

This stop was a hefty one. As you can see there many tools available for testing (this is no way all of them) and this is just on the frontend side of things. We will be arriving at stop 6 next week and it promises to be very insightful. Can you guess what it might be?
 

# Resources

* [Front End Testing](https://www.testim.io/blog/front-end-testing-complete-overview/)
* [A comprehensive Guide to Front End Testing](https://www.perfecto.io/blog/comprehensive-guide-front-end-testing)
* [LinkWe Podcast: Testing](https://www.facebook.com/linkwepodcast/videos/1176353916126186)
* [CSS Tricks: Frontend testing is for everyone](https://css-tricks.com/front-end-testing-is-for-everyone/)
* [Frontend Masters: Testing](https://frontendmasters.com/books/front-end-handbook/2018/tools/testing.html)