3

I'm coming to you with mocking problem.

What I intend to achieve is possibility to mock several modules outside tested file.

I assume that first answer will be:

You can mock modules inside your tested file, although you are trying to do it differently.

I understand that it would work. However I really don't want to mock these modules in each test..

I have a setup file that looks like this:

const setup = (initialState, component) => {
  const store = TestState.getStore(initialState) // getting redux's state
  mockModules(initialState); // mocking modules
  const mounted = mount(
   <Provider store={store}>{component}</Provider>
  )
  return { store, mounted }
}

const mockModules = (initialState) => {
 jest.resetModules();
 jest.doMock(pathToModule, () => ({
   mockedFunction: () => ({ value: initialState.value }) // mocking function 
 }))
}

and this is how I'm using setup function in my test files:

describe('My test', () => {
  
  const setupTest = (initialState = {}) => {
     const {mounted, store} = setup(initialState, <MyComponent />);

     return {mounted, store}
  }

  it('first test', () => {
   const {mounted} = setupTest();
   expect(mounted.exists()).toBe(true);
  })
})

My problem is that these modules that I've mocked by mockModules function, they aren't mocked..

I've tried a lot of "workarounds" and neither of them worked. Any ideas?

qwerty1234567
  • 195
  • 1
  • 8
  • Please, provide listings as text, not images. If I understood the problem correctly, it's because jest.mock is useless inside tests if a module isn't re-imported. It's always jest.resetModules + jest.mock + require. – Estus Flask Nov 26 '20 at 17:39
  • so... instead of using `jest.mock` I should use `jest.resetModules` + `jest.mock` + `require`? – qwerty1234567 Nov 26 '20 at 17:42
  • Yes, in this order. See https://stackoverflow.com/a/64675642/3731501 or https://stackoverflow.com/a/63763277/3731501 for example – Estus Flask Nov 26 '20 at 17:47
  • Unfortunately, it doesn't work. I intend to mock function / module that is used by redux-saga. Everything works ( redux / react-redux / create the store / retrieving values from the store ) except redux-saga modules mocking :x – qwerty1234567 Nov 26 '20 at 19:28
  • I wanted to do something similar to this https://github.com/eidoo/redux-saga-mock#setup-with-a-store-and-saga-middleware – qwerty1234567 Nov 26 '20 at 19:28
  • The whole hierarchy of modules that rely on mocked module needs to be reimported, this way it will be workable. Consider updating the question with your current attempt. See the comment above, images aren't suitable to post the code, they don't help your question, https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – Estus Flask Nov 26 '20 at 20:03
  • Ok. I'm updating my question – qwerty1234567 Nov 27 '20 at 09:06
  • You didn't reimport. There''s no way how jest.mock can affect anything without that. There should be `require` for all modules that rely on pathToModule, and you need to use these imported values instead of those that you imported at top level. – Estus Flask Nov 27 '20 at 10:19
  • But I'm using these mocked modules in my sagas :( I can't just use different values, because I don't have an access to saga in my tests. – qwerty1234567 Nov 27 '20 at 10:44
  • What's the problem with the access? Any way, that's how it is. Any module that depends on mocked module should be reimported. – Estus Flask Nov 27 '20 at 11:27
  • The problem is that I simulating click on my button and this button get some data from the API, so I'm not calling sagas directly, but by the DOM elements. – qwerty1234567 Nov 27 '20 at 11:30
  • If it's a component that relies on a saga that relies on mocked module, it's component module that needs to be reimported. This will cause the whole hierarchy of modules to be reimported for the test, this is what jest.resetModules is responsible for. – Estus Flask Nov 27 '20 at 11:35
  • I used `jest.resetModules` and it didn't help – qwerty1234567 Nov 27 '20 at 11:43
  • I've just explained why. It's jest.mock and jest.resetModules are useless if you don't reimport modules. You should reimport the whole hierarchy of modules that depend on mocked module with `require` inside a test that needs test-specific module mocking, or you cannot use this functionality at all. – Estus Flask Nov 27 '20 at 11:51
  • Okay. Thanks a lot! I guess I should move it to end-to-end tests. – qwerty1234567 Nov 27 '20 at 12:11

0 Answers0