Introduction
Kava has been powering accurate test suites for Node.js and Web Browsers since 2012.
What is Kava?
Kava is a npm package that provides your Node.js and Web Browser code with test suite capabilities.
Outside of development, kava is a narcotic sedative drink made in Polynesia from the crushed roots of a plant of the pepper family.
Why Kava?
Kava is built upon TaskGroup which offers all the same benefits as the other testing libraries, but with these superior benefits:
It accurately associates tests with suites. This is something which other test utilities merely guess, which leads them to false positives, especially with asynchronous tests.
It doesn't need a special runtime setup. Your tests can be run just like any other script you write. Allowing you to use your existing knowledge to write and debug your tests.
In general, it can be stated that kava produces a state of calmness, relaxation and well-being without diminishing cognitive performance.
Getting Started
Installation
To make use of Kava for your testing, you must first install it:
npm install --save-dev kavaUsage
Using Kava is as simple as requiring it.
Let's say we are writing a package called myfirstpackage, generally we would write a test.js file which makes use of Kava like so:
To run our tests with Kava, we simply run our tests:
Which would produce the following:
Architecture
Kava under the hood is powered by the robust TaskGroup flow control library.
Tests in Kava are extensions of the TaskGroup's Task class. Suites in Kava (a collection of tests) are extensions of TaskGroup's TaskGroup class.
This makes the Kava codebase incredibly simple yet incredibly powerful thanks to the existing robustness of TaskGroup, which already offers flow control, error reporting, and optional completion callbacks. All Kava needs to add, is the syntactic sugar and reporting that we come to expect from a modern testing framework.
Tests
Tests can be written like so:
Which will output:
Tests can also become asynchronous by accepting a completion callback like so:
Which will output:
Test Examples
Here is an example of a synchronous read file test:
Here is an example of an asynchronous read file test:
Suites
A suite is a collection of tests. They can be written like so:
The receiving of the suite and test arguments, and their usage within, creates proper nesting structures, such that children are actually children and are treated as such, in regards to reporting and flow execution, as seen by the output of the above:
As you would notice from the above, by default Suites execute their items serially, that is one after the other, waiting for the current item to complete before starting the next. This is a sensible default, and is inherited from Suites inheritance of TaskGroup.
Suite Concurrency
As Suites do inherit from TaskGroup, and as TaskGroup allows us to configure the concurrency via the concurrency option, we could have our suite execute in parallel by doing:
Which will alter the output to be the following:
Suite Initialisation
Suites can also create tasks asynchronously by the use of a completion callback for the suite initialiser.
This is because under the hood, the suite initialiser is just a TaskGroup Task, that Kava configures with the suite and test arguments, and the TaskGroup pops on the optional completion callback and executes it accordingly.
Suite Example
Here is an example making use of all that we learned above:
Hooks
Kava gives us support for before and after hooks for our tests and suites, such that we can perform synchronous and asynchronous actions before or after a specific test or suite, or all tests or suites within a particular suite.
To add before and after hooks to a specific Suite or Test, we would pass our listeners into the before and after options like so:
To add before and after hooks for every item within a suite, we can use event listeners like so:
Reporters
Kava also supports different reporters. Reporters listen to the status of suites and tests and output their results to you.
The default reporter is the Console reporter. However, there is another builtin reporter called the List reporter which outputs only the results of tests, rather than their commencement.
You can select the reporter you wish to use via the KAVA_REPORTER environment variable like so:
Summary
This page covers everything you need to utilise Kava for your testing purposes.
Last updated
Was this helpful?