You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, in mock, functions are mocked like this:
exportconstgetCalendar=()=>"gregorian";// the calendar identifier you want
...
So every mock is just a simple function, not e.g. a jest mock
It makes it really hard to overwrite values of those mocks, cause one is unable to write a code like this:
import{getCalendar}from`react-native-localize';describe('Test how `getCalendar` values influence my function foo', () => { beforeEach(jest.resetAllMocks); test('If `getCalendar` returns 'gregorian', we return "cool", () => { getCalendar.mockImplementation(() => "cool"); expect(foo()).toEqual("awesome"); }); test('If `getCalendar` returns'chinese',wereturn"nice",()=>{getCalendar.mockImplementation(()=>"chinese");expect(foo()).toEqual("nice");});});
The issue here is that getCalendar is not a mock, but a function, so getCalendar.mockImplementation will not exist in runtime. In our project, we've found that we should either split two tests above to each be in a separate file, and use jest.mock at that file top-level (which you imagine is a bit weird way to write collocated tests), or that we need to overwrite provided mock with code like this put at the top of the file:
and then proceed as above, which is also not desired (although it works)
Why don't we simply wrap every mock function in jest.fn?
I can see that you maybe don't want to have a dependency on jest and allow people to use other test runners as well -- is that the reason?
Possible implementation
That would be really simple -- we'd just need to make changes like this across mock/index.ts
If there's a support for this, I can submit a PR
--exportconstgetCurrencies=()=>["USD","EUR"];// can be empty array++exportconstgetCurrencies=jest.fn(()=>["USD","EUR"]);// can be empty array
This was also mentioned in another issue -- see here
Code sample
Seeabove
The text was updated successfully, but these errors were encountered:
Why it is needed?
Currently, in
mock
, functions are mocked like this:So every mock is just a simple function, not e.g. a
jest
mockIt makes it really hard to overwrite values of those mocks, cause one is unable to write a code like this:
The issue here is that
getCalendar
is not a mock, but a function, sogetCalendar.mockImplementation
will not exist in runtime. In our project, we've found that we should either split two tests above to each be in a separate file, and usejest.mock
at that file top-level (which you imagine is a bit weird way to write collocated tests), or that we need to overwrite provided mock with code like this put at the top of the file:and then proceed as above, which is also not desired (although it works)
Why don't we simply wrap every mock function in
jest.fn
?I can see that you maybe don't want to have a dependency on
jest
and allow people to use other test runners as well -- is that the reason?Possible implementation
That would be really simple -- we'd just need to make changes like this across
mock/index.ts
If there's a support for this, I can submit a PR
This was also mentioned in another issue -- see here
Code sample
The text was updated successfully, but these errors were encountered: