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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mach.js

v0.9.4

Published

Simple mocking framework for JavaScript inspired by CppUMock and designed for readability

Readme

mach.js

Simple mocking framework for JavaScript inspired by CppUMock and designed for readability

Mocking a Function

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalled().when(function() {
  f();
});

Naming a Mocked Function

const mach = require('mach.js');

// Mocked function is now given as 'f' in error messages
const f = mach.mockFunction('f');

Mocking an Object

const mach = require('mach.js');

const someObject = {
  foo: function() {},
  bar: function() {}
};

mockedObject = mach.mockObject(someObject);

mockedObject.foo.shouldBeCalled().when(function() {
  mockedObject.foo();
});

Naming a Mocked Object

const mach = require('mach.js');

const someObject = {
  foo: function() {},
  bar: function() {}
};

// Mocked fields are now given as 'someObject.foo' and 'someObject.bar' in error messages
mockedObject = mach.mockObject(someObject, 'someObject');

Requiring Arguments

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalledWith(1, 2).when(function() {
  f(1, 2);
});

Ignoring Single Arguments

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalledWith(1, mach.any, 3).when(function() {
  f(1, 'whatever', 3);
});

Ignoring All Arguments

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalledWithAnyArguments().when(function() {
  f(1, 2);
});

Ignoring Other Calls

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalled().andOtherCallsShouldBeIgnored().when(function() {
  f();
  f(1);
});
const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalled().withOtherCallsIgnored().when(function() {
  f();
  f(1);
});

Returning Values

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalled().andWillReturn(4).when(function() {
  expect(f()).toBe(4);
});

Throwing Values

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalled().andWillThrow(Error('error')).when(function() {
  try {
    f();
  }
  catch(e) {
    console.log(e.message);
  }
});

Requiring Multiple Calls

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalledWith(2).andWillReturn(1).multipleTimes(3).when(() => {
  f(2);
  f(2);
  f(2);
});

Making Multiple Expectations

const mach = require('mach.js');

const f1 = mach.mockFunction();
const f2 = mach.mockFunction();

f1.shouldBeCalled()
  .andAlso(f2.shouldBeCalled())
  .when(function() {
    f1();
    f2();
  });

Making Optional Expectations

const mach = require('mach.js');

const f = mach.mockFunction();

f.mayBeCalled().when(function() {});

Using Optional Ordering

const mach = require('mach.js');

const f = mach.mockFunction();

// Use andThen or then when order is important
f.shouldBeCalledWith(1)
  .andThen(f.shouldBeCalledWith(2))
  .when(function() {
    f(2); // Error, out of order call
    f(1);
  });

f.shouldBeCalledWith(1)
  .then(f.shouldBeCalledWith(2))
  .when(function() {
    f(2); // Error, out of order call
    f(1);
  });

// Use andAlso or and when order is unimportant
f.shouldBeCalledWith(1)
  .andAlso(f.shouldBeCalledWith(2))
  .when(function() {
    f(2); // No error, order is not fixed when 'andAlso' is used
    f(1);
  });

f.shouldBeCalledWith(1)
  .and(f.shouldBeCalledWith(2))
  .when(function() {
    f(2); // No error, order is not fixed when 'and' is used
    f(1);
  });

Using Mixed Ordering

const mach = require('mach.js');

const f = mach.mockFunction();

f.shouldBeCalledWith(1)
  .and(f.shouldBeCalledWith(2))
  .then(f.shouldBeCalledWith(3))
  .and(f.shouldBeCalledWith(4))
  .when(function() {
    f(2);
    f(1);
    f(4);
    f(3);
  });

Matching Arguments Using Deep Compare

const mach = require('mach.js');

const f = mach.mockFunction();

// mach.match can also be used
f.shouldBeCalledWith(mach.same([1, 2, 3]))
  .when(function() {
    f([1, 2, 3]);
  });

Matching Arguments Using a Custom Matcher

const mach = require('mach.js');

const customMatcher = function(a, b) {
  return a[0] === b[0]
};

const f = mach.mockFunction();

f.shouldBeCalledWith(mach.same([1, 2, 3], customMatcher))
  .when(function() {
    f([1, 4, 9]);
  });

Ignoring Mocked Calls

const mach = require('mach.js');

const f = mach.mockFunction();

mach.ignoreMockedCallsWhen(function() {
  f();
});

Flexible Syntax

const mach = require('mach.js');

const f1 = mach.mockFunction();
const f2 = mach.mockFunction();

function somethingShouldHappen() {
  return f1.shouldBeCalled();
}

function anotherThingShouldHappen() {
  return f2.shouldBeCalledWith(1, 2, 3);
}

function theCodeUnderTestRuns() {
  f1();
  f2(1, 2, 3);
}

// Actual test.
somethingShouldHappen()
  .and(anotherThingShouldHappen())
  .when(theCodeUnderTestRuns);

Handy Error messages

const mach = require('mach.js');

const f1 = mach.mockFunction('f1');
const f2 = mach.mockFunction('f2');
const f2 = mach.mockFunction('f3');

f1.shouldBeCalledWith(1)
  .and(f2.shouldBeCalledWith(2))
  .when(function() {
    f1(1);
    f3(3);
  });
Unexpected function call f(3)
Completed calls:
  f1(1)
Incomplete calls:
  f2(2)

Testing asynchronous code

describe('Foo', () => {
  const Foo = require('./Foo.js');

  let mockTemplate = {
    sync: () => {},
    callback: () => {},
    promise: () => {}
  }

  let foo;
  let mock;

  beforeEach() => {
    mock = mach.mockObject('mock', mockTemplate);
    foo = new Foo(mock);
  };

  it('should return a promise for callback code', (done) => {
    mock.callback.shouldBeCalled().when(() => {
      return new Promise((resolve) => {
        foo.bar(() => {
          resolve();
        });
      });
    }).catch((error) => {
      fail(error);
    }).then(() => {
      done();
    };
  });

  it('should return a promise for promise code', (done) => {
    mock.promise.shouldBeCalled().when(() => {
      return foo.baz().then((value) => {
        return value;
      });
    }).catch((error) => {
      fail(error);
    }).then((value) => {
      expect(value).toEqual(something); // value will be return value of foo.baz();
      done();
    });
  });
});