Finally, I found a workable solution thanks to the contribution of @nbkhope.
So, the following code works as expected, that is, it mocks the code and then restores the original behavior:
const spy = jest.spyOn( fs, 'writeFile' ).mockImplementation((filePath,data) => { ... }) ... spy.mockRestore()
source share