Giter VIP home page Giter VIP logo

elmer-fudd's Introduction

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?

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

Test with just a spec
const { test, assert } = require('elmer-fudd');

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

test({
  name: 'Unit testing a module',
  unit: './path/to/double.js'
  spec: (double) => {
    assert.equal(double(2), 4);
  }
});
Unit testing a module with mocked dependencies
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);
  }
});
Unit testing a module with a spec object
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 } }
  ],
})

elmer-fudd's People

Contributors

skiano avatar

Watchers

 avatar  avatar  avatar

elmer-fudd's Issues

return map?

Not sure what to call this, but I want something like

const mock = mockFn();

mock.will([
  { output: 3, given: [1, 2]  },
  { output: 4, given: [2, 2]  },
])

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.