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

elmer-fudd

v1.3.1

Published

mini test runner + mocking

Downloads

152

Readme

elmer-fudd

A test runner for smallish node projects with an opinionated take on mocking and very few dependencies. It helps you hunt for Bugs... get it?

|Package|Why| |:------|:--| |pirates| for patching require | |stack-trace| for identifying call sites |

Installation

$ npm install elmer-fudd

Minimal Example Project

package.json
src/
  multiply.js
  scale.js
test/
  multiply.test.js
{
  "elmer-fudd": {
    "ext": "test.js",
    "root": "test",
    "alias": {
      "@src": "src"
    }
  },
  "scripts": {
    "test": "elmer-fudd"
  },
}
const scale = require('./scale');
module.exports = (value) => value * scale;
module.exports = 10;
const { test, assert } = require('elmer-fudd');

test({
  name: 'Multiply without mocking',
  unit: '@src/multiply',
  spec: (multiply) => {
    assert.equal(multiply(5), 50);
  }
});

test({
  name: 'Multiply with mocked scale',
  unit: '@src/multiply',
  mock: [
    ['@src/scale', 2]
  ],
  spec: (multiply) => {
    assert.equal(multiply(5), 10);
  }
});

Testing Api

test

test takes a “test object” as an input, which allows you to specify a name, the unit you wish to test, any mocks you want to provide for dependencies, and a “spec” that runs assertions. Below are a few examples to get you started:

const { test, assert } = require('elmer-fudd');

test({
  name: 'Test with just a spec',
  spec: () => {
    assert.ok(true);
  }
});
const { test, assert } = require('elmer-fudd');

test({
  name: 'Unit testing a module',
  unit: './path/to/double.js'
  spec: (double) => {
    assert.equal(double(2), 4);
  }
});
const { test, assert } = require('elmer-fudd');

test({
  name: 'Unit testing a module',
  unit: './path/to/unit.js',
  mock: [
    ['.path', { fake: true }],
  ],
  spec: (double) => {
    assert.equal(double(2), 4);
  }
});
const { test, assert } = require('elmer-fudd');

test({
  name: 'Using a spec object',
  unit: './path/to/sum.js',
  spec: [
    { given: [1, 2], expect: 3 },
    { given: [1, 2, 3], expect: 6 },
  ]
});

assert

assert wraps node’s core assert.strict library so that failures can be grouped with tests. For detailed information see the docs. Here are a few examples to give you some ideas:

TODO examples that build...

mockFn

mockFn returns a mock function you can use in your specs. Here is how you might use a mock function.

const { mockFn } = require('elmer-fudd');

const fn = mockFn();

fn.returns(2);

fn(); // returns 2

It is not a comprehensive solution, but if you need something more robust there is no reason you cannot use something like sinon instead. Mock functions have the following methods and properties:

  • fn.returns(value) makes the mock return a specific value
  • fn.implementation(fn) adds an implementation to the mock
  • fn.calledWith(...args) returns true if the mock has been called with these args
  • fn.throws(err) when the mock is called, this error is thrown
  • fn.resolves(value) the mock returns a promise that resolves this value
  • fn.rejects(err) the mock returns a promise that rejects with this err
  • fn.reset() resets the mock function
  • fn.calls a getter that returns all the calls
  • fn.count a getter that returns how many times the mock has been called

Tips & Tricks

Test driven?

You are allowed to mock the unit itself, which pared with "spec objects" creates a neat way you can write tests first without annoying failures. Then when you want to try the real unit you can comment out the test.

const { test, mockFn } = require('elmer-fudd')

test({
  name: 'test first?',
  unit: './my-busted-fn',
  mock: [
    ['./my-busted-fn', mockFn().will([
      { given: [2], output: 'foo' },
      { given: [2, { extra: true }], output: { bar: 2 } },
    ])]
  ],
  spec: [
    { given: [2], output: 'foo' },
    { given: [2, { extra: true }], output: { bar: 2 } }
  ],
})