JavaScript Testing with Mocha and Chai
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.
JavaScript testing is something not always talked about.
Some developers stand by it 100%, while others have varying degrees of support of its implementation into projects.
Depending on what you are attempting to test, JavaScript testing can be hard to implement and costly with both time & money.
Most common types of tests are:
- Unit Tests, which we will discuss further below.
- Integration Tests — These include testing integration between two classes, testing integration with the production environment, etc. Integration tests should cover the behaviour of cross-module functionality.
- UI Test — Also known as Functional Tests. Testing scenarios on the product itself, by controlling the browser or the website, regardless of the internal structure to ensure expected behaviour. They mimic user interactions by simulating click, typing, scrolling, etc.
To create and run these tests, there are many different options that offer a wide choice of functionalities, such as:
- A testing structure, or the organizing of your tests. Tests are often structured in a behaviour-driven development fashion.
- Providing assertion functions, like what we use with Chai. These are functions that make sure that tested variables contain the expected value.
- Generate, display, and watch test results when running tests automatically.
- Providing mocks (faking modules or behaviours to test different parts of a process), spies (how many times a function was called, what cases and where in your application), and stubs (or stubbing, replaces selected functions with selected functions to check on their behaviour).
- Generate code coverage reports. Code coverage, or test coverage, is a measure used to describe the degree that source code is executed when tests are run.
- Providing a browser or browser-like environment with a control on their scenarios execution. There options of testing browsers are jsdom (a pure JavaScript environment that simulates a real browser and has no UI), a Headless Browser Environment (a browser that runs without a UI for the purpose of making the browser respond faster), and a Real Browser Environment (an actual GUI browser that opens and runs your tests).
Alternative Options
Here at MonetizeDesign we use Mocha and Chai but there are many other options out there that can deliver the features listed above, such as:
- Jsdom — https://github.com/jsdom/jsdom
- Jest — https://facebook.github.io/jest/
- Istanbul — https://istanbul.js.org/
- Karma — https://karma-runner.github.io/2.0/index.html
- Sinon.JS — http://sinonjs.org/
- Testdouble.js — https://github.com/testdouble/testdouble.js
- Wallaby — https://wallabyjs.com/
- Cucumber — https://github.com/cucumber/cucumber-js
- Jasmine — https://github.com/jasmine/jasmine
- AVA — https://github.com/avajs/ava
- Tape — https://github.com/substack/tape
- Selenium — https://github.com/SeleniumHQ/selenium
- Appium — https://github.com/appium/appium
- Protractor — https://github.com/angular/protractor
- WebdriverIO — http://webdriver.io/
- Nightwatch — http://nightwatchjs.org/
- TestCafe — https://devexpress.github.io/testcafe/
- Cypress — https://www.cypress.io/
- Puppeteer — https://github.com/GoogleChrome/puppeteer
- Nightmare — https://github.com/segmentio/nightmare
- Casper — https://github.com/casperjs/casperjs
- CodeceptJS — https://github.com/codeception/codeceptjs/
What is Mocha and Chai?
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.
From https://mochajs.com
Mocha comes with tons of great features. Including:
- Simple async support, including promises
- Async test timeout support
- Before, after, before each, and after each hooks
- Use any assertion library
- Test specific timeouts
- Test duration reports
What is Unit Testing?
Unit testing is a common part of the development process where the smallest testable parts of an application are checked for proper functionality. It is a component of test-driven development (TDD) where a failing test is written first then the feature is built and refactored until the test passes.
The core idea is that you test a functions behaviour to be as it is expected. You call it with a certain set of parameters, and check that correct result was returned. This process can be done manually but is often done automatically with software and testing libraries: such as Mocha and Chai.
Why we use Mocha and Chai
We use Mocha and Chai to test our code because it allows us to grow our application with confidence and the knowledge that code is always being checked. Changing already tested code becomes far less anxiety-inducing.
Testing with Mocha and Chai really helps to facilitates growth and scalability of our applications by ensuring that functions in our code always work as expected and will continue to do so as we build and refactor.
We are able to develop quickly without having to manually go through every single part of an application before pushing to production. Manual testing is a waste of time and unreliable.
Finding bugs and broken functions early helps to eliminate errors early.
What do JavaScript testing methods do you use?