Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Each mock should be wrapped in jest.fn #266

Closed
erheron opened this issue Jul 25, 2024 · 3 comments
Closed

Each mock should be wrapped in jest.fn #266

erheron opened this issue Jul 25, 2024 · 3 comments
Assignees
Labels
feature request New feature or request

Comments

@erheron
Copy link
Contributor

erheron commented Jul 25, 2024

Why it is needed?

Currently, in mock, functions are mocked like this:

export const getCalendar = () => "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', we return "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:

jest.mock('react-native-localize', () => {
   const actualMock = jest.requireActual('react-native-localize/mock');
   return {
      ...actualMock,
      getCalendar: jest.fn()
   }
}

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

-- export const getCurrencies = () => ["USD", "EUR"]; // can be empty array
++ export const getCurrencies = jest.fn(() => ["USD", "EUR"]); // can be empty array

This was also mentioned in another issue -- see here

Code sample

See above
@erheron erheron added the feature request New feature or request label Jul 25, 2024
@erheron
Copy link
Contributor Author

erheron commented Jul 25, 2024

Hey @zoontek btw, I think we've met in Paris last year, for drinks with Matthias and your female friend from work 😆

@zoontek
Copy link
Owner

zoontek commented Jul 25, 2024

No problem, open a PR, I will release it.

@erheron
Copy link
Contributor Author

erheron commented Jul 25, 2024

Hey @zoontek please see #267

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants