mock-fn
v1.1.0
Published
Simple mockFunction test utility
Downloads
17
Readme
mock-fn
Simple mockFunction
test utility.
Install
npm i --save-dev mock-fn
Usage
import { strict as assert } from "node:assert";
import mockFunction from "mock-fn";
it("should mock void function", () => {
//given
const voidFnMock = mockFunction();
//when
voidFnMock();
//then
assert.deepEqual(voidFnMock.times, 1);
});
it("should assert input params", () => {
//given
const fnMock = mockFunction((a, b) => {
assert.deepEqual(a, 1);
assert.deepEqual(b, 2);
return 123;
});
//when
const result = fnMock(1, 2);
//then
assert.deepEqual(result, 123);
assert.deepEqual(fnMock.times, 1);
});
it("should return different results depending on number of calls", () => {
//given
const fnMock = mockFunction(() => {
if (fnMock.times === 1) return 1;
if (fnMock.times === 2) return 2;
return 0;
});
//when & then
assert.deepEqual(fnMock(), 1);
assert.deepEqual(fnMock(), 2);
assert.deepEqual(fnMock(), 0);
assert.deepEqual(fnMock.times, 3);
});