Socket
Socket
Sign inDemoInstall

uvu

Package Overview
Dependencies
5
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    uvu

uvu is an extremely fast and lightweight test runner for Node.js and the browser


Version published
Weekly downloads
2.8M
decreased by-1.58%
Maintainers
1
Install size
505 kB
Created
Weekly downloads
 

Package description

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

Readme

Source
uvu
version CI downloads install size
uvu is an extremely fast and lightweight test runner for Node.js and the browser
Ultimate Velocity, Unleashed

example with suites

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!

// tests/demo.js
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:

# via `uvu` cli, for all `/tests/**` files
$ uvu -r esm tests

# via `node` directly, for file isolation
$ node -r esm tests/demo.js

Note: The -r esm is for legacy Node.js versions. Learn More

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

Keywords

FAQs

Last updated on 03 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc