Jest: Namespace undefined when using jest.mock()

Created on 26 Feb 2019  路  6Comments  路  Source: facebook/jest

馃悰 Bug Report

When using jest.mock("fs") and the new promise namespace inside of it. the mock doesn't contain the promise namespace.

To Reproduce

Steps to reproduce the behavior:
- Write code with fs.promise.function.
- Write test with import fs and mock fs.

Expected behavior

I except that the promise namespace is usable when fs is mocked.

Bug Confirmed Help Wanted

Most helpful comment

I fixed the problem with fs.promises.function = jest.fn() for the time being.
Hopefully this issue will get fixed.

All 6 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

withinboredom picture withinboredom  路  3Comments

StephanBijzitter picture StephanBijzitter  路  3Comments

kgowru picture kgowru  路  3Comments

mmcgahan picture mmcgahan  路  3Comments

samzhang111 picture samzhang111  路  3Comments