I need assistance in developing a utility function for my unit tests that can automatically mock all dependencies of a function in a functional programming style using TypeScript.
For instance:
Suppose I have a function like this:
createBreakfast(deps);
Here, deps
represents an interface of type Deps
for the createBreakfast function:
export interface Deps {
scrambleEggs:(numberOfEggs: number, ingredients: Ingredients) => Promise<string>;
makeToast:({wantButter, howManyPieces, personMakingTheToast}:{wantButter:boolean, howManyPieces: number, personMakingTheToast: Person}) => Promise<string>;
}
In order to conduct unit testing effectively, I may want to mock scrambleEggs and personMakingTheToast while customizing the behavior of makeToast.
For example:
const scrambleEggs = jest.fn();
const makeToast = jest.fn();
const personMakingTheToast = jest.fn();
const deps = {scrambleEggs, makeToast, personMakingTheToast}
makeToast.mockReturnValueOnce("It's ready");
const result = await createBreakfast(deps);
I am seeking a helper function that can automate the process of mocking functions, parameters, or any other elements if not explicitly provided:
Sample code snippet:
const makeToast = jest.fn();
makeToast.mockReturnValueOnce("It's ready");
const deps= createDeps<createBreakfast>(makeToast);
//Since scrambleEggs and personMakingTheToast were not passed as arguments,
//I expect createDeps to identify this and automatically mock them by inspecting
//the createBreakfast function
const result = await createBreakfast(deps);
Can someone assist me in writing the implementation for the createDeps
function?
The function should be able to adapt dynamically as the size of the function grows. I intend to reuse this function across all my unit tests to simplify the testing process.