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

kequtest

v1.0.3

Published

A simple test utility

Downloads

10

Readme

A lightweight unit test runner using no dependencies. It's meant to run fast and be useful for testing small projects, plugins, things like that quickly.

You don't need to configure anything to begin testing just run kequtest.

Install

npm i -D kequtest

Add the following script to package.json for easier access:

{
  "scripts": {
    "test": "kequtest"
  }
}

Features

  • Supports async tests
  • Use any mechanism for thowing errors
  • Supports Typescript
  • Runs all tests
  • Displays logs on failed tests
  • Displays errors

Use

It finds all .test.js/.test.ts files anywhere in the current directory.

describe()

it()

Containers are defined using describe() and tests are defined with it(), a test will fail if an error is thrown. An easy way to throw errors is by using Node's assert.

Example

// /my-project/somewhere/my-lib.test.js

const assert = require('assert');
const myLib = require('./my-lib.js');

it('counts nearby offices', function () {
  const result = myLib();
  assert.strictEqual(result, 42);
});

Output will look like this.

kequc@kequ4k:~/my-project$ npm t

STARTING

> /home/kequc/my-project
Found 1 test file...

/somewhere/my-lib.test.js
· counts nearby offices ✓

FINISHED
1/1 passing, 0 failures

kequc@kequ4k:~/my-project$

Advanced

You may specify a file or directory as a parameter.

kequtest somewhere/my-lib.test.js

Hooks

before()

beforeEach()

afterEach()

after()

They run in conjunction with the current container, using beforeEach() will run once for each it() inside.

let count = 0;

beforeEach(function () {
  count++;
});

it('uses hooks', function () {
  // count ~= 1
});

Spies

util.logger()

Generate a console-like object where each method error, warn, info, http, verbose, debug, silly, and log is a spy.

util.spy()

Takes a function to spy on (or empty). Values that pass through are available as an array on the calls attribute.

const mySpy = util.spy(() => 'hi there');
const result = mySpy('hello?', 1);
// result ~= 'hi there'
// mySpy.calls ~= [['hello?', 1]]

mySpy.reset();
// mySpy.calls ~= []

Mocks

util.mock()

Called with a target and desired return value, mocks must be defined before their targets are imported. Targets are relative to your test.

// /my-project/src/main-lib.js

module.exports = require('./my-data.js').getUser();
// /my-project/tests/main-lib.test.js

util.mock('../src/my-data.js', {
  getUser: () => ({ id: 'fake-id', name: 'peter' })
});

const { id } = require('../src/main-lib.js');

it('mocks', function () {
  // id ~= 'fake-id'
});

util.mock.stop()

Stops mocking a specific target. Mocks are automatically stopped at the end of the current container.

util.mock.stopAll()

Stops mocking all targets.

Uncache

util.uncache()

Clear a module from the cache, this will force the module to be loaded again the next time it is required.

Modules are automatically uncached at the end of the current container. This could be used manually if you wanted to uncache between, or during tests.

let mainLib;

beforeEach(function () {
  mainLib = require('../src/main-lib.js');
});

afterEach(function () {
  util.uncache('../src/main-lib.js');
});

Eslint

Tip if you aren't using TypeScript and want to avoid no-undef warnings add overrides to your eslint config.

{
  "overrides": [
    {
      "files": ["*.test.js"],
      "globals": {
        "describe": "readonly",
        "it": "readonly",
        "util": "readonly",
        "before": "readonly",
        "beforeEach": "readonly",
        "afterEach": "readonly",
        "after": "readonly"
      }
    }
  ]
}

TypeScript

If you want to test a TypeScript project or your tests are written in TypeScript. Use kequtest with the --ts flag, this will automatically register ts-node and look for .test.ts files.

Ensure you have both typescript and ts-node installed in your project.

{
  "scripts": {
    "test": "kequtest --ts"
  }
}

To enjoy types you should import side effects of kequtest at the top of your test files.

import 'kequtest';

Contribute

If you know how to better implement TypeScript so globals like beforeEach can be used via intellisense in tests I'd love to hear from you. I feel the current implementation requiring an additional import statement is not the best.

A better TypeScript implementation is warmly welcomed. Especially one which can be localized to only adds globals to .test.ts files throughout the app.