jest-mock-functions
v3.3.4
Published
Mock all functions inside an array or object literal
Downloads
11
Maintainers
Readme
jest-mock-functions
Ever needed to mock all the functions of an object but nothing else?
This package mocks all functions inside a arrays, objects, Maps, Sets and class instances and copys overs everything else into a new object.
Note: This package is specifically meant for use with Jest
Installation
npm install jest-mock-functions
or
yarn add --dev jest-mock-functions
How to use it
Objects
You can mock objects:
import mockFunctions from 'jest-mock-functions';
const mockedObject = mockFunctions({
aInteger: 1,
// This is replaced with jest.fn()
aFunction: () => {},
// This is replaced with jest.fn()
anotherFunction: function() {}
});
Arrays
You can mock the contents of arrays:
import mockFunctions from 'jest-mock-functions';
// Will return [1, jest.fn(), jest.fn()];
const mockedArray = mockFunctions([
1,
() => {},
function() {},
]);
Sets
Class instances
Mocking an instance of class will add mocked functions for all the instance's functions and all of it's prototype's functions.
class Example {
constructor() {
this.aFunction = () => { return 1; }
}
anotherFuntion() {
return 2;
}
}
const mocked = mockFunctions(new Example());
// Has been mocked
mocked.aFunction();
// Has been mocked
mocked.anotherFunction();
const notMocked = new Example();
// Returns 1
notMocked.aFunction();
// Returns 2
notMocked.anotherFunction();
Options
You can pass options into mockFunctions
.
E.g. mockFunctions(aValue, { recursive: true })
recursive: boolean | RecursionOptions
You can mock functions inside arrays inside objects. With { recursive: true }
.
import mockFunctions from 'jest-mock-functions';
// Will return { test: [ jest.fn(), 1] }
const mockedThing = mockFunctions({
test: [() => {}, 1]
}, { recursive: true });
or the other way around
import mockFunctions from 'jest-mock-functions';
// Will return [ { test: jest.fn() }, { aInt: 1 }]
const mockedThing = mockFunctions(
[ { test: () => {} }, { aInt: 1 }],
{ recursive: true }
);
go crazy with it
import mockFunctions from 'jest-mock-functions';
// Will return [[{ this: { is: [{ rediculous: jest.fn() }]}}]]
const mockedThing = mockFunctions(
[[{ this: { is: [{ rediculous: () => {}}]}}]],
{ recursive: true }
);
Class instances
By default, class instances are not mocked recursively. If you wish to mock class instances add the option: { recursive: { classInstances: true } }
.
onMockedFunction: (mockedFunction, originalValue) => any
Use this if you want to do something with the mocked functions. E.g. Return a specific value:
import mockFunctions from 'jest-mock-functions';
const mockedObject = mockFunctions({
test: () => 2,
test2: () => 3
}, { onMockedFunction: (fn, ogVal) => fn.mockImplementation(() => ogVal * 2 });
// Returns 4
mockedObject.test();
// Returns 6
mockedObject.test2();