Given a JS file like this:
export const uid = () => Math.random().toString();
export const doubleUid = () => [uid(),uid()].join('_');
How can you use Jest to mock uid
as () => 'foo'
such that the result of doubleUid()
is foo_foo
? Is it possible to do so without modifying the file above?
I'm not aware of any way to do this using mocks.
You have to mock each exported function separately. Mocking the exported version of the first function doesn't change the original implementation of the second exported function.
See this answer I gave to a related sort of question here: https://stackoverflow.com/a/72192912/3084820