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

@scalvert/bin-tester

v2.1.1

Published

A test harness to invoke a CLI in a tmp directory

Downloads

480

Readme

@scalvert/bin-tester

CI Build npm version License Dependabot Volta Managed Code Style: prettier

Provides a test harness for node CLIs that allow you to run tests against a real project.

Install

npm add @scalvert/bin-tester --save-dev

# or

yarn add @scalvert/bin-tester --dev

Usage

@scalvert/bin-tester uses two libraries to provide the test harness:

  • fixturify-project: Allows you to dynamically create test fixtures using real directories and files in a tmp directory
  • execa: A better replacement for child_process.exec

It combines the above and provides an API for running a binary with a set of arguments against a real project structure, thus mimicking testing a real environment.

import { createBinTester } from '@scalvert/bin-tester';

describe('Some tests', () => {
  let project;
  let { setupProject, teardownProject, runBin } = createBinTester({
    binPath: 'node_modules/.bin/someBin',
    staticArgs: ['--some-arg'], // pass some args to the bin that will be used for each invocation
  });

  beforeEach(() => {
    project = await setupProject();
  });

  afterEach(() => {
    await teardownProject();
  });

  // Run the bin and do something with the result
  test('a test', async () => {
    const result = await runBin();

    expect(result.stdout).toBe('Did some stuff');
  });

  test('another test', async () => {
    // Write a file with contents to the tmp directory
    await project.writeDirJSON({
      'some/file.txt': 'some content',
    });

    // pass some args to the bin that will be used for only this invocation
    const result = await runBin('--path', 'some/file.txt');

    expect(result.stdout).toBe('Read "some/file.txt"');
  });
});

API

Classes

Functions

BinTesterProject

Kind: global class

new BinTesterProject(name, version, cb)

| Param | Type | Default | Description | | --- | --- | --- | --- | | name | string | "fake-project" | The name of the project. Used within the package.json as the name property. | | version | string | | The version of the project. Used within the package.json as the version property. | | cb | function | | An optional callback for additional setup steps after the project is constructed. |

binTesterProject.gitInit() ⇒ *

Kind: instance method of BinTesterProject
Returns: * - {execa.ExecaChildProcess}

binTesterProject.chdir()

Kind: instance method of BinTesterProject

binTesterProject.dispose() ⇒ void

Kind: instance method of BinTesterProject

createBinTester(options) ⇒ CreateBinTesterResult.<TProject>

Kind: global function
Returns: CreateBinTesterResult.<TProject> -

| Param | Type | Description | | --- | --- | --- | | options | BinTesterOptions.<TProject> | An object of bin tester options |

createBinTester~runBin(...args) ⇒ execa.ExecaChildProcess.<string>

Kind: inner method of createBinTester
Returns: execa.ExecaChildProcess.<string> - An instance of execa's child process.

| Param | Type | Description | | --- | --- | --- | | ...args | RunBinArgs | Arguments or execa options. |

createBinTester~setupProject()

Kind: inner method of createBinTester

createBinTester~setupTmpDir()

Kind: inner method of createBinTester

createBinTester~teardownProject()

Kind: inner method of createBinTester