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

cool-runner

v0.0.13

Published

An intentional-minimal, slightly-opinionated test runner for NodeJS.

Downloads

1

Readme

Cool Runner

An intentionally minimal, slightly opinionated test runner for NodeJS (does not work in the browser!)

Cool Runnings Gif

Installation

Local installation:

yarn add cool-runner or npm install cool-runner --save

You may also install globally:

yarn add -g cool-runner or npm install -g cool-runner

Writing Tests

All tests must extend from the TestCase class.

const {TestCase} = require('cool-runner');
const assert = require('assert');

class MyTestCase extends TestCase {

    constructor() {
        super();

        // How long to wait for async operations
        // Default is 2000
        this.setTimeout(2000);
    }

    /*
     * Tests should start or end with the word "test".
     * camelCase, PascalCase, snake_case, etc. are all fine
     */
    testSomeSynchronousFunctionality(done) {
        assert.strictEqual(obj.runLogic(), true);
        done();
    }

    /**
     * `done` must be called for both synchronous and async tests
     * unless using promises ...
     */
    some_async_functionality_test(done) {
        runSomeAsyncLogic.then(response => {
            assert.deepStricEqual(response, mock);
            done();
        });
    }

    /**
     * Alternatively, return a promise that resolves after 
     * everything is finished
     */
    some_async_functionality_test_with_promise() {
        return new Promise((resolve, reject) => {
            runSomeAsyncLogic.then(response => {
                assert.deepStricEqual(response, mock);
                resolve();
            });
        });
    }

    /**
     * not all methods need to be tests
     */
    squareUtilityMethod(param) {
        return param * 2;
    }

    /**
     * Call the utility method from this test case
     */
    test_some_logic_with_utility_method(done) {
        assert.equal(this.squareUtilityMethod(2), 4);
        done();
    }

    /* other helpful methods to know about */

    beforeEach(done) {
        // do something before every test is run
        done();
    }

    /**
     * Same as above, you can call the callback when finished
     * or return a promise that resolves.
     */
    beforeAll() {
        // called once before the entire suite is run.
        return new Promise((resolve, reject) => {
            // do something important
            resolve();
        });
    }

    afterEach(done) {
        // do some cleanup after each test
        done();
    }

    afterAll() {
        // called once after suite is finished
    }
}

module.exports = MyTestCase;

Running Tests

CoolRunner exposes two commands, which recursively search your test directory and run all of the valid test cases.

cool-runner test: run tests once

  • --testDir=tests: Specify the relative path to some other directory besides tests where tests are stored

cool-runner watch: run tests on file change to either src or tests

  • --testDir=tests: Specify the relative path to some other directory besides tests where tests are stored
  • --srcDir=src: Specify the relative path to some other directory besides src where source code is stored

Directory Structure

By default, CoolRunner assumes you're directory structure looks like this:

- node_modules/
- src/     // all source code
- tests/   // all tests
- etc...

If you interleave source code and tests, run the following to recursively search your source directory, running only valid test cases

cool-runner test --testDir=src

(Note, that there is a slight performance benefit to keeping them in separate directories. This benefit could be more important as the size of the tests and source directory grow.)

Why CoolRunner?

There are lots of great test runners available in the JavaScript ecosystem (which I use and enjoy!). However, CoolRunner is designed with a few things in mind:

  • Speed: Tests should run in <1s, preferrably faster.
  • Ease of use: Straightfoward API. Sensible defaults.
  • Easy to wire-up to debuggers for interactive testing + debugging.
  • Avoid any global state.

Contributing

Contributions welcome on Gitbub.

Issues

Submit issues on Github.

Authors