a_mock Mocking Framework

2.0.5 · active · verified Sun Apr 19

a_mock is a focused JavaScript mocking framework designed to be used with any JavaScript testing framework. It enables the creation of both partial and strict mocks, offering granular control over function behavior for testing purposes. Developers can define specific expectations for arguments (single, multiple, arrays, and complex objects), specify return values, configure exceptions to be thrown, and manage call repetition. The library provides capabilities for ignoring arguments or entire calls when precision isn't necessary. It is currently at version 2.0.5 and includes TypeScript type definitions, facilitating its use in TypeScript projects. While a_mock emphasizes explicit expectation setup and strict argument matching, particularly for object arguments, its core utility lies in providing a clear, declarative API for creating test doubles without imposing a specific test runner or assertion library. The release cadence for a_mock is not explicitly detailed in the provided documentation, but it presents as a stable, utility-focused library for fundamental mocking scenarios.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating both strict and partial mocks, showing how to define expected arguments, handle multiple calls, and observe strict matching behavior with unexpected inputs.

import { mock } from 'a_mock';

// Minimal declaration for a_mock to satisfy TypeScript for quickstart
// In a real project, these types would be automatically available from 'a_mock'
declare module 'a_mock' {
    interface MockExpectation {
        return(value: any): MockExpectation;
        throw(error: Error): MockExpectation;
        repeat(count: number): MockExpectation;
        repeatAny(): MockExpectation;
        whenCalled(callback: (...args: any[]) => any): MockExpectation;
    }

    interface MockInstance extends Function {
        expect(...args: any[]): MockExpectation;
        ignore(): MockInstance;
        ignoreAll(): { return(value: any): MockExpectation; };
    }

    function mock(originalFunction?: Function): MockInstance;
}

// Mocking a function that expects multiple arguments
const myMock = mock();

myMock.expect('firstArg1', 'secondArg1').return('fake1');
myMock.expect('firstArg2', 'secondArg2').return('fake2');

console.log('Call 1:', myMock('firstArg1', 'secondArg1')); // Expected: fake1
console.log('Call 2:', myMock('firstArg2', 'secondArg2')); // Expected: fake2

try {
  myMock('foo');
} catch (e: any) {
  console.error("Error on single arg call:", e.message); // Expected: throws unexpected arguments
}

try {
  myMock('foo', 'bar');
} catch (e: any) {
  console.error("Error on unexpected args:", e.message); // Expected: throws unexpected arguments
}

// Demonstrate a partial mock
const originalAdder = (a: number, b: number) => `Original sum: ${a + b}`;
const partialAdderMock = mock(originalAdder);

partialAdderMock.expect(10, 20).return('Mocked 10+20');

console.log('Partial mock call 1:', partialAdderMock(10, 20)); // Expected: Mocked 10+20
console.log('Partial mock call 2:', partialAdderMock(5, 5));   // Expected: Original sum: 10 (falls back)

view raw JSON →