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

@amilajack/joker

v0.0.4

Published

Simple and powerful testing for command-line apps

Downloads

27

Readme

logo

Build Status NPM version npm codecov

A modern and intuitive testing library for command-line apps

Installation

$ npm install --save @amilajack/joker

How it looks

const path = require('path');
const assert = require('assert');
const { default: Joker } = require('@amilajack/joker');

(async () => {
  await new Joker()
    .run('echo hello')
    .expect((result) => {
      assert(result.stdout === 'hello');
    })
    .code(0)
    .end();
})();

API

See full API docs

Features

Formatting options

Joker can strip new line characters and colors. You can tell it to do so by passing an object that looks like this:

const options = {
  colors: false,
  newLines: false
};

new Joker(options);

Custom expectations

While Joker comes with built-in expectations, you can use your own too.

await new Joker()
  .run('unicorns')
  .expect(result => {
    if (result.stdout !== 'unicorns') {
      return new Error('NO!');
    }
  })
  .end();

Custom middlewares

You can register as many before and after middlewares as you wish.

await new Joker()
  .before(setupDatabase)
  .before(runMigrations)
  .run(cmd)
  .after(downgradeCron)
  .after(deleteDatabase)
  .end();

Middleware order

The Middleware execution order is very simple - "before" middlewares always run before everything else, "after" middlewares always run after everything else. The other middlewares will match the order that you have specified.

await new Joker()
  .before(before1)
  .before(before2)
  .after(after1)
  .after(after2)
  .writeFile(file, '')
  .run(cmd)
  .unlink(file)
  .end();

// Execution order:
// before1, before2, writeFile, cmd, unlink, after1, after2

Plugins

Joker has primitive support for plugins. You can register any expectation or/and any middleware by calling joker.register.

const fn = () => {};
new Joker().register('foo', fn);

Or you may want to register many functions at once.

const fn = () => {};
const fn1 = () => {};
joker.register({ baz: fn, bar: fn1 });

Usage with a test runner

Joker plays nice with any test runner out there.

Jest

Here is a minimal example how you could use it with Jest using async/await:

describe('todo add', () => {
  it('adds a new todo item', async () => {
    const result = await new Joker()
      .run('todo add')
      .stdout('A new todo has been added')
      .end();
    expect(result.stdout).toMatchSnapshot();
  });
});

Mocha

Here is a minimal example how you could use it with Mocha using callbacks:

describe('todo add', () => {
  it('adds a new todo item', done => {
    new Joker()
      .run('todo add')
      .stdout('A new todo has been added')
      .end(done);
  });
});

Usage without a test runner

While using a test runner is recommended Joker is completely 'nodeable'. Here is a simple example how you could accomplish that:

const assert = require('assert');

function refute(err) {
  assert(!err);
}

new Joker()
  .run(cmd)
  .end(refute);

new Joker()
  .run(anotherCmd)
  .end(refute);

Responding to interactive prompts

Joker can respond to apps that run interactively using the on() and respond() functions.

await new Joker()
  .run(cmd)
  .on('Your name: ')
  .respond('Joe User\n')
  .end();

See test/prompt.test.ts for more examples.

Templates

Every Joker instance can be cloned, which allows you to build "templates" for tests. Here's some examples:

const template = new Joker()
  .cwd(path.join(__dirname, 'fixtures'))
  .run('echo test');

const test1 = await template
  .clone()
  .stdout(/test/)
  .end();

const test2 = await template
  .clone()
  .stdout('test')
  .end();

Credits

Special thanks to:

Support

Do you like this project? Star the repository, spread the word - it really helps. You may want to follow me on Twitter and GitHub. Thanks!

If this project is saving you (or your team) time, please consider supporting it on Patreon 👍 thank you!