Module Mocking Is A Bad Way To Test Data Fetching

[!INFO]-
topic: 🧩 React
links:


Module mocking doesn't test the module you are replacing.

Module mocking describes the process of overwriting some module, that you would normally import into your test. For example, when you want to test an API, the real request might introduce undesired side effects to your test. Module mocking allows you to mock some dummy code, that will be used instead of the real code. While this makes testing easy, this means that the real code will never be tested, which is why this is not the ideal way.

Instead, use a Request Handler.

Here is an example of module mock, but it is using a hook that does an API request, which is why it should be avoided.

jest.mock('../hooks/useRepositories', () => { //instead of using the code inside this module, use this code
return () => {
	return {
		data: [
			{ name: 'react' },
			{ name: ' javascript'}
		]
	}
}

});