npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unit

v1.4.3-0

Published

The JavaScript unit testing framework for perfectionists

Downloads

1,106

Readme

🧪 Unit

The JavaScript unit testing framework for perfectionists.

Installation

Run the following command to integrate Unit into your existing project:

$ npm install --save-dev unit

Syntax

Unit's syntax is designed to be elegant, flexible and simple. It is heavily inspired by the C# unit testing framework XUnit among others.

The use of decorators to define units and tests is preferred and encouraged.

Suggested practices

Below are some suggested practices to follow when using Unit for unit testing.

  1. Declare one unit per file

  2. Use anonymous classes for the units:

@Unit("My Unit")
default class { // Anonymous class declaration
    //
}
  1. Suffix unit files with ".unit" (ex. utils.unit.ts or fun.unit.js)

  2. Import all units and run tests from a single file

  3. Use @Feed to provide in-line data whenever possible

  4. Do not export unit classes (There's no need!)

Examples

1. Your First Unit

Let's create a simple test to determine whether 'test' equals 'test'. For this, we'll be importing the Assert class.

import {Unit, Test, Assert, Runner} from "unit";

@Unit("My Unit")
default class {
    @Test("'test' should equal 'test'")
    public shouldEqual(): void {
        Assert.equals("test", "test");
    }
}

// Run tests
Runner.test();

Our output should be:

  [My Unit]
    √ 'test' should equal 'test'

  1/1 {100%} passing

2. Feeding tests with in-line data

Instead of writing many assert statements, we can use the clever @Feed decorator. It's job is to provide (thus "feed") the test method with in-line data.

For simplicity's sake, the import statements have been ommitted.

@Unit("My Unit")
default class {
    @Test("Should determine if entities are equal")
    @Feed("hello", "hello")
    @Feed("world", "world")
    public shouldEqual(entity1: any, entity2: any): void {
        Assert.equals(entity1, entity2);
    }
}

As you can see, this makes the process a whole lot easier. You can, of course, provide as much in-line data as your heart desires.

3. Mocking

In simple terms, mocking is the process of replacing or modifying existing functionality with custom implementations with the purpose of debugging and/or simplifying certain processes that would otherwise make our tests fragile, and dependent of the environment.

Fortunately, Unit provides elegant mocking utilities built with simplicitly in mind.

Mocking return values

import {Mock} from "unit";

let existingFn = (): number => 0;

In this simple example, we would like to mock the function existingFn so thats it returns 1 instead of 0 the next time it is called.

We can easily accomplish this functionality using the returnOnce helper function:

...

existingFn = Mock.fn(existingFn) // Prepare the function to be mocked.
    .returnOnce(1) // We specify that we want the function to return '1' the next time it is called.
    .invoker; // Finally we replace the function with our mock invoker.

console.log(existingFn()); // 1
console.log(existingFn()); // 0

Interestingly, the second call to the existingFn function returns 0, which is what we would expect.

Mocking implementations

In some cases, we may need to not only mock a function's return value, but it's implementation.

This can be achived using the once helper function:

let square = (num: number): number => num ** 2;

square = Mock.fn(square)
    // Implement the target once.
    .once((num: number): number => num * 2)

    // Assign our invoker.
    .invoker;

console.log(square(4)); // 16
console.log(square(4)); // 8

Why use Unit?

What makes Unit different from the other various JavaScript testing frameworks, and why should I consider using it?

  • Simple, elegant decorator-based syntax which makes writing tests a breeze.
  • Built-in mocking utilities.
  • Broad range of useful assertion utilities.
  • Chainable methods with simple names; Less writing, more doing.
  • Cleverly self-tested codebase.
  • Full TypeScript support (It's written in it!).