Issue
I'm using Jest and Typescript. I have a async function that returns nothing (void). How do I mock returning void? I tried the below
const myMockFn = jest.fn().mockImplementationOnce(() => Promise.resolve(void));
jest.mock('../config', () => ({
setup: async () =>
myMockFn(),
}));
but I get the compilation error
Expression expected.
related to "Promise.resolve(void)".
Solution
void
used here in value
position is javascript operator. It is a unary operator i.e. it accepts a single argument. Thus the error.
void
as a typescript type should be used in type
position to express that function return value will not be observed.
const myMockFn = jest.fn().mockImplementationOnce(() => Promise.resolve());
That's enough to mock a function with Promise<void>
return type.
Answered By - aleksxor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.