Masoud
Masoud

Reputation: 1064

Difference between mock and unmock test in Jest

Recently I've become familiar with Jest library and unit test concepts and everything due to jest's documentation is right in my codes.

But I need to know what's difference between mocking and unmocking concept in Jest and other unit test libraries.

Thanks

Upvotes: 3

Views: 1599

Answers (1)

Andreas Köberle
Andreas Köberle

Reputation: 110972

Mock means to replace an instance with another one. In jest its used to replace the implementation of imported modules with your own one.

jest.mock('yourModule', () => {test: ()=> 'test'})

The main idea behind it, is to isolate your code in unit test, so that you only test one module without the influence of other parts of your application or external code. This has a bunch advantages. First of all if the code in one module breaks, only the test for this part will fail and not a all test for parts that just import this module. Second you can simplify the test itself as you dont need to start up a server that returns with specific data, which would also slow down your code.

The unmock feature is there cause of the automock feature, which was the default in the past. Automocking will replace all imported modules with default mock. As this make sense for some modules but is not wanted for lodash for example you could then unmock the mocking of them. So unmock is mostly needed with automock switched on to get the original implementation if needed.

Upvotes: 5

Related Questions