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

Mocking with Jest: Spying on Functions and Changing their Implementation #334

Open
utterances-bot opened this issue Apr 3, 2022 · 4 comments

Comments

@utterances-bot
Copy link

Mocking with Jest: Spying on Functions and Changing their Implementation

Using Jest to record calls of particular methods and even change their implementation.

https://silvenon.com/blog/mocking-with-jest/functions

Copy link

zhao-li commented Apr 3, 2022

Thank you for writing this up. I had the hardest time figuring out how to use Jest's mocking features. Your article helped me understand how mocks and spies are supposed to be used which then led me to figure out that I did not need to use manual mocks because spies worked sufficiently well.

I went through your article in great detail and wanted to share some issues I ran in to, in case it can help the next person reading your article:

  • mathRandomSpy.restore() should now be mathRandomSpy.mockRestore()
  • this bit of example test code:
      montyPython.callFnWithTheMeaningOfLife(fn)
      expect(fn).toHaveBeenCalledWith(42)

does not have any accompanying code that specifies a callFnWithTheMeaningOfLife function

  • the link to Ministry of Silly Walks video no longer works. It seems like the video has been taken down.
  • I had to Google how to view the results of expect(montyPython.getSillyWalk(6)).toMatchSnapshot(). Not hard to do, but as a newbie, I glossed over it without knowing I had glossed over it.

Sorry, I might have just missed some points and the above might not actually be typo's. Sorry in advance for my mistake.

Copy link
Owner

Thank you so much for you generous feedback! I updated my posts based on what you said 🚀 hopefully I covered everything that you had trouble with, and I'm so happy my posts helped you understand these features!

Copy link

This is a fantastic introduction to Jest's mocking features, especially for beginners intimidated by the concept.

Your breakdown of mock functions, spying, and practical examples makes it approachable and engaging.

I love how you tied the "meaning of life" theme into testing—humor always helps with learning! Mocking truly is a game-changer for robust, insightful tests. 🚀

Regards,
Digital Marketing Course in Delhi

Copy link
Owner

Thank you very much! It's an old couple of posts that are still getting a lot of attention, I hope to make a series of Vitest posts one day. Mocking indeed changed my testing game as well! 💪 It's kind of underdocumented in Jest, that's why I wrote these posts.

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

No branches or pull requests

4 participants