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

chawan

v1.0.10

Published

Simple ES module-based testing framework to help you write beautiful code

Downloads

4

Readme

United ES6 test tools

Mocha is great but having asserts, assert plugins and spies spread out as separate projects is a bit frustrating, especially when you have to jump around all of the different documentations to setup test environments.

Additionally, most of these are regular CommonJS modules which makes their usage in a pure ES module awkward.

Chawan goals

  • directly usable as an ES6 module in Node without cross-compiling
  • have test files runnable as regular programs
  • test files should run in separate processes
  • documentation for expectations/spies/tests in ONE place
  • shallow learning curve, quick to start and work with

BDD API

Chawan provides a mocha-like API to structure your tests. It also supports promises and the typical hooks:

  • before
  • beforeEach
  • afterEach
  • after
import { describe, it } from 'chawan';

describe('Array', () => {
    describe('#lastIndexOf', () => {
        it('finds the last occurrence of a given element', () => {
            // ...
        });

        it('returns -1 if no such element exists', () => {
            // ...
        });

        it.skip('is not done yet', () => {
            // ...
        });
    });
});

Expectations

  • expect(a).toBeA(e) - check if a is of type e
  • expect(a).toEqual(e), expect(a).toNotEqual(b) - checks for equality
  • expect(a).toBeTrue(), expect(a).toNotBeTrue()
  • expect(a).toBeFalse(), expect(a).toNotBeFalse()
  • expect(a).toBeEmpty(), expect(a).toNotBeEmpty()
  • expect(a).toExist(), expect(a).toNotExist()
  • expect(a).toBeLessThan(e), expect(a).toBeGreaterThan(e)
  • expect([1,2]).toInclude(1), expect([1,2]).toNotInclude(3)
  • expect(f).toThrow([<Error>|<String>]), expect(f).toNotThrow([<Error>|<String>])
  • expect(s).toMatch(regexp), expect(s).toNotMatch(regexp)
  • expect(a).toDeepEqual(b), expect(a).toNotDeepEqual(b)

for spies:

  • expect(s).toHaveBeenCalled([nTimes])
  • expect(s).toHaveBeenCalledWith(a1, a2)

Spies

import {expect, spy} from 'chawan';

// spy on objects
{
    const obj = {};
    spy(obj, 'funcName');
    obj.funcName();
    expect(obj.funcName).toHaveBeenCalled();
}
// standalone spy
{
    let s = spy();
    s();
    expect(s).toHaveBeenCalled();
}

// spy with specific return value
{
    let s = spy().returns('hello world!');
    expect(s()).toEqual('hello world!');
    expect(s).toHaveBeenCalled(1);
}

// spy overwriting existing function
{
    const obj = {
        myFunc: () => { return 'original'; }
    };
    s(obj, 'myFunc').returns(2);
    expect(obj.myFunc('a', 3, 7)).toEqual(2);
    expect(obj.myFunc).toHaveBeenCalledWith('a', 3);
    expect(obj.myFunc.lastArgs[2]).toEqual(7);
    obj.myFunc.restore();
    expect(obj.myFunc()).toEqual('original');
}

Running tests

In order to execute a test file, simply run it as you would normally do in Node.js:

$ node --experimental-modules --no-warnings ./tests/my-test.mjs

Alternatively, you can also have the test file directly executable:

#!/usr/bin/env -S node --no-warnings --experimental-modules

import { describe, it, expect } from 'chawan';

describe('my test', () => {
    // ...
});

You can then run it like that:

$ sh ./tests/my-test.mjs

If you want to run multiple tests together as one test suite, you can use the chawan binary:

$ chawan ./tests/*.mjs