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: Taking Advantage of the Module System #335

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

Mocking with Jest: Taking Advantage of the Module System #335

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

Comments

@utterances-bot
Copy link

Mocking with Jest: Taking Advantage of the Module System

Using Jest to reimplement certain modules when you don't want their usual behavior during testing.

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

Copy link

zhao-li commented Apr 3, 2022

Thank you again for writing this up. This and the first article is a life-saver for someone who got mocks to work but never quite understood when to use which method of mocking.

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:

  • the getSillyWalk method is a bit harder to grok than just returning a Math.random()
  getSillyWalk(numberOfSteps) {
    const steps = []
    for (let i = 0; i < numberOfSteps; i++) {
      if (steps[steps.length - 1] !== 'left') {
        steps.push('left')
      } else {
        steps.push('right')
      }
    }
    // shuffle
    return steps.sort(() => 0.5 - Math.random())
  • it was an extra step for me to figure out, but maybe it was left out to save space. In case it helps others:
    it('gets the real meaning of life', () => {
      jest.dontMock('./monty-python')
      const RealMontyPython = require('./monty-python')
      // ... [filling out this part that was omitted for brevity; not sure if it's what was intended or not]
      const mathRandomSpy = jest.spyOn(Math, 'random')
      mathRandomSpy.mockImplementation(() => '¯\_(ツ)_/¯')
      const montyPython = new RealMontyPython()
      expect(montyPython.getTheMeaningOfLife()).toBe('¯\_(ツ)_/¯')
      mathRandomSpy.mockRestore()
      jest.resetModules()
    })
  • the section on manual mocks required me to fill in some blanks on how the test actually looked like. It was do-able, but not sure if you want to just lay it out for the next newbie that comes along.

Also, your explanation of how mocks are hoisted was super helpful. I ran into that error and conflated it with a bunch of other things. Your explanation clarified everything up.

Thank you again for writing all of this out so simply and succinctly. 🙏

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!

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

3 participants