What is uvu?
The uvu npm package is a highly performant and lightweight test runner/framework for JavaScript, designed to execute tests quickly and support both Node.js and browser environments. It provides a simple API for defining and running tests, as well as assertions to validate code.
What are uvu's main functionalities?
Test Definition and Execution
This feature allows you to define and run tests. The code sample demonstrates how to define a simple test suite with uvu to test the Math.sqrt function.
const { test } = require('uvu');
const assert = require('uvu/assert');
test('Math.sqrt()', () => {
assert.is(Math.sqrt(4), 2);
assert.is(Math.sqrt(144), 12);
assert.is(Math.sqrt(2), Math.SQRT2);
});
test.run();
Asynchronous Test Support
uvu supports asynchronous tests, allowing you to test asynchronous operations like fetching data from an API. The code sample shows an asynchronous test that awaits the resolution of a fetchData function.
const { test } = require('uvu');
const assert = require('uvu/assert');
test('async test', async () => {
const data = await fetchData();
assert.is(data, expectedData);
});
test.run();
Hooks
uvu provides hooks like before, after, before.each, and after.each to set up preconditions and clean up after tests. The code sample illustrates how to use before.each and after.each hooks in a test suite.
const { test, suite } = require('uvu');
const assert = require('uvu/assert');
const mathSuite = suite('math');
mathSuite.before.each(() => {
// setup code before each test
});
mathSuite.after.each(() => {
// cleanup code after each test
});
mathSuite('Math.pow()', () => {
assert.is(Math.pow(2, 3), 8);
});
mathSuite.run();
Other packages similar to uvu
mocha
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple. It's more heavyweight compared to uvu and offers a wider range of built-in features like test retries and flexible reporting.
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works out of the box for any React project and provides a more integrated testing solution with features like snapshot testing, global setup/teardown, and a built-in coverage reporter. Jest is more opinionated and comprehensive than uvu, which is minimalistic and focused on performance.
ava
AVA is a test runner for Node.js with a concise API, detailed error output, and process isolation that lets you develop with confidence. AVA is similar to uvu in its minimalism and focus on performance, but it also offers unique features like concurrent test execution and built-in support for TypeScript.
tape
Tape is a tap-producing test harness for Node.js and browsers. It's minimal and flexible, with only a small set of core methods. Tape is similar to uvu in its simplicity and ease of use, but it produces TAP output, which can be consumed by any TAP reporter for more detailed results.
uvu is an extremely fast and lightweight test runner for Node.js and the browser
Ultimate
Velocity,
Unleashed
Features
- Super lightweight
- Extremely performant
- Individually executable test files
- Supports
async
/await
tests - Supports native ES Modules
- Browser-Compatible
- Familiar API
Install
$ npm install --save-dev uvu
Usage
Check out /examples
for a list of working demos!
import { test } from 'uvu';
import * as assert from 'uvu/assert';
test('Math.sqrt()', () => {
assert.is(Math.sqrt(4), 2);
assert.is(Math.sqrt(144), 12);
assert.is(Math.sqrt(2), Math.SQRT2);
});
test('JSON', () => {
const input = {
foo: 'hello',
bar: 'world'
};
const output = JSON.stringify(input);
assert.snapshot(output, `{"foo":"hello","bar":"world"}`);
assert.equal(JSON.parse(output), input, 'matches original');
});
test.run();
Then execute this test file:
$ uvu -r esm tests
$ node -r esm tests/demo.js
View the uvu
CLI documentation
Assertions
The uvu/assert
module is completely optional.
In fact, you may use any assertion library, including Node's native assert
module! This works because uvu
relies on thrown Errors to detect failures. Implicitly, this also means that any uncaught exceptions and/or unhandled Promise
rejections will result in a failure, which is what you want!
API
Module: uvu
View uvu
API documentation
The main entry from which you will import the test
or suite
methods.
Module: uvu/assert
View uvu/assert
API documentation
A collection of assertion methods to use within your tests. Please note that:
- these are browser compatible
- these are completely optional
Benchmarks
via the /bench
directory with Node v10.21.0
Below you'll find each test runner with two timing values:
- the
took ___
value is the total process execution time – from startup to termination - the parenthesis value (
(___)
) is the self-reported execution time, if known
Each test runner's stdout
is printed to the console to verify all assertions pass.
Said output is excluded below for brevity.
~> "ava" took 594ms ( ??? )
~> "jest" took 962ms (356 ms)
~> "mocha" took 209ms ( 4 ms)
~> "tape" took 122ms ( ??? )
~> "uvu" took 72ms ( 1.3ms)
License
MIT © Luke Edwards