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

testrun

v0.7.1

Published

A module to support a test

Downloads

19

Readme

testrun Build Status Build Status

A module to support a test.

NPM

Install

Install with npm

$ npm install testrun --save-dev

Usage

1. Create test cases.

(sample.js)
const testrun = require('testrun').mocha;

function testfn(testcase) {  // `testcase` is a property of the 3rd argument of `testrun`
  if (typeof testcase.in === 'string') {
    return testcase.in;
  } else {
    throw new TypeError();
  }
}

testrun('test name', testfn, [
  {
    name: 'test case group',
    cases: [  // `cases` is the keyword for specifying test case group.
      {
        name: 'test for input : ${testcase.in} => ${testcase.expected}',
        in: 'aaa',
        expected: 'aaa',   // `expected` is the keyword for specifying expected value.
      },
      {
        name: 'test for in:${testcase.in} => ${testcase.error}',
        in: ['a', 'b', 'c'],
        error: TypeError,  // `error` is the keyword for specifying expected error.
      },
      {
        name: 'sub test case group',
        cases: [ ... ],
      },
      {
        name: 'skip this test case',
        skip: true,        // `skip` is the keyword to skip a test case or a test case group.
        ...
      },
      /*
      {
        name: 'only run this test case',
        only: true,        // 'only' is the keyword to run only a test case or a test case group.
        ...
      },
      */
    ],
  },
]);

2. Run the test cases.

$ mocha sample.js

   test case group
     ✓ test for in: 'aaa' => 'aaa'
     ✓ test for in:[ 'a', 'b', 'c' ] => TypeError

   0 passing (16ms)

Supporting test frameworks

  • mocha

    const testrun = require('testrun').mocha;
  • lab

    const testrun = require('testrun').lab(exports);

    NOTICE: If using lab on node version 0.10 or 0.12, you have to use lab version 6.2.

APIs

testrun(testname, testfn, testcases) => void

runs the specified test function for each test cases.

Parameters:
  • testname [string] : a test name.

  • testfn [function] : a test function. (see below.)

  • testcases [array] : an array of test cases.

    testfn (testcase [, done]) => any

    is a function to execute a testcase. The argument testcase is each property in testcases passed to testrun function.

    Parameters:
    • testcase [object] : a test case object.
    • done [function] : a callback to end.
      • If this argument is not specified, testfn is required to return a result or to throw an error.
      • If this argument is specified, testfn is required to evaluate a testcase in own way and execute this argument to end it.

    testcase

    Reserved words for properties of testcase:
    • expected [any] : set an expected value of a test case.
    • error [Error] : set an error type which a test case throws
    • cases [array] : set a test case group.
    • skip [boolean] : set true if you want to skip a test case or a test case group.
    • only [boolean] : set true if you want to run only a test case or a test case group.

testrun.byPlatform(valuesByPlatforms) => any

returns a value for the current platform.

Parameters:
  • valuesByPlatforms [plain-object | any] : values by platforms.
Example:
testrun.byPlatform({win32: 123, otherwise: 999 });
// => 123  (on Windows)
// => 999  (on other platform)

testrun.byPlatform('abc');
// => 'abc'

testrun.scriptrun(templateFile, testDir) => function

returns a function which executes javascript based on content of templateFile on child process.

Parameters:
  • templateFile [string] : a path string of a javascript template file for a test case.
  • testDir [string] : a path string of a directory to output a javascript file for a test case.
Example:
(template.js)
const assert = require('assert');
var testcase = ${testcase};
assert.strictEqual(testcase.input.toUpperCase(), testcase.expected);
const testfn = testrun.scriptrun(templateFile, testDir);

testrun('convert letter case', testfn, [
  { name: 'convert ${testcase.input} => ${testcase.expected}', input: 'abc', expected: 'ABC' },
  ...
]);

License

Copyright (C) 2016 Takayuki Sato

This program is free software under MIT License. See the file LICENSE in this distribution for more details.