When using jest.mock("fs") and the new promise namespace inside of it. the mock doesn't contain the promise namespace.
Steps to reproduce the behavior:
- Write code with fs.promise.function.
- Write test with import fs and mock fs.
I except that the promise namespace is usable when fs is mocked.
I _think_ this is due to us not following getters. The reasoning is that getters are functions, so accessing them can throw. I'm not sure how to deal with this
Maybe we can simply access getters too and when there are some errors, then simply show proper error message for that cases?
Do we want to error, though?
I guess we could define our own getters and dynamically call the underlying ones to figure out their type. If they throw, ask users to mock it manually?
jest.mock('thing', () => ({
...jest.genMockFromModule('thing'),
duynamicThing: jest.fn(),
}));
What if the getter triggers a side-effect, though? We don't invoke functions we just check _if_ they're functions.
Maybe we can always throw inside getters asking people to mock it manually since Jest was unable to figure out its type? Not sure.
I forgot about side effects.
Is it possible to return Proxy for that cases. As we cannot access getters then maybe Proxy allows to act as any shaped mocks?
I fixed the problem with fs.promises.function = jest.fn() for the time being.
Hopefully this issue will get fixed.
Most helpful comment
I fixed the problem with fs.promises.function = jest.fn() for the time being.
Hopefully this issue will get fixed.